Date Conversion Django Error - python

I have a date here which is Fri Jun 19 02:27:25 PDT 2015 which I get from the DB and I am trying to convert it from PDT to UTC
For which first I am converting it to a datetime object like this:
date_time = datetime.datetime.strptime(date, '%a %b %d %H:%M:%S %Z %Y');
When I run the python file directly, it works, but when this code executes thru the Django framework, I get this error.
ValueError: time data 'Fri Jun 19 02:27:25 PDT 2015' does not match format '%a %b %d %H:%M:%S %Z %Y'
I have a feeling this is because of the timezone, because I have many more date formats which don’t contain timezone and conversion for them works fine. Could you suggest a workaround for this.

This error raised because datetime module not recognize all time-zones, use dateutil module instead of datetime similar below:
from dateutil.parser import parse
parse('Fri Jun 19 02:27:25 PDT 2015')

Related

Python datetime format

I'm trying to parse dates in the form of "Sept 9, 2021" using datetime
I was trying the following format:
"%m %d, %y" but it raises a ValueError while I'm trying to convert it
(i.e using int(datetime.strptime("Sept 9, 2021","%m %d, %y").timestamp()))
How can I tell which format am I supposed to use?
Thanks
For strptime there are different format code than usual.
You use %b to refer to a Three-digit string month, which means that you must use Sep instead of Sept, and you use %Y to convert a 4 digit year.
So the code would be like the following
int(datetime.datetime.strptime("Sep 9, 2021", "%b %d, %Y").timestamp())
# 1631138400
For more information see Format Codes.
To my knowledge the key work for September is Sep (not Sept).
You can check this link for more details on that: Python Date abbreviation
I advice you to use something similar to this snippet of code:
from datetime import datetime
date_s = "Sep 9, 2021"
datetime_object = datetime.strptime(date_s.upper().replace("SEPT", "SEP"), '%b %d, %Y')
print(datetime_object)
You can also check this Stack thread that seem similar to your question:
Date Handling in Python for Sept
There are a couple of ways how to get the timestamp from your string:
First solution is to use dateparser module from PyPi (pip install dateparser). This module can help you to get datetime object from any string which is similar to datetime string.
Solution:
import dateparser
date_time_object = dateparser.parse("Sept 9, 2021").timestamp()
print(date_time_object)
Result: 1631127600.0
Another issue is that your string contains one unnecessary string in name of month so this solution is a bit wrong and won't help you but only for understanding, I will offer this:
Solution:
from datetime import datetime
time_stamp = int(datetime.strptime("Sep 9, 2021","%b %d, %Y").timestamp())
print(time_stamp)
Result: 1631127600

Parsing time to Datetime with Timezone

I have a time like this:
Fri Dec 04 14:51:22 CST 2020
I want to parse it as Timestamp with Timezone in Python, I am not able to achieve this with regular methods.
Could someone please help me in doing this efficiently.
Use the parse function from the dateparser package.
from dateparser import parse
dt = parse("Fri Dec 04 14:51:22 CST 2020")

VSCode how can I use GMT timezone

I am trying to create a snipping code that includes the date in GMT zone, I was able to put the date in the snipping but how can I converted in GMT zone
date: $CURRENT_DAY_NAME_SHORT $CURRENT_MONTH_NAME/$CURRENT_DATE/$CURRENT_YEAR $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND
Use the extension CommandVariable and the dateTime command. You have a lot of options to format your date and time

python datetime strptime does not match format

This is the code i want to run
from datetime import datetime
date="08/30/2017 10:02 pm (PDT)"
dt = datetime.strptime(date, '%m/%d/%Y %I:%M %p (%Z)')
date is a string with value 08/30/2017 10:02 PM (PDT)
It looks perfectly fine to me, but python give me this error:
time data '08/30/2017 10:02 PM (PDT)' does not match format '%m/%d/%Y %I:%M %p (%Z)'
The code was ran on a remote machine with python 2.7. However, if I manually type those code into a local python terminal. It works perfectly fine.
Is there any thing that can make the difference?
(I tried to change date between unicode/str, makes no difference)
Check the value of the TZ environment variable. time.strptime uses the TZ variable to disambiguate time zone abbreviations because they would not be unique otherwise. I can reproduce the match error with TZ=Europe/Berlin, but get a successful parse with TZ=America/Tijuana.
Another source for the discrepancy could occur if the other machine has TZ data which uses numeric time zones only, which was a somewhat recent change (in 2017) for certain time zones (which would also mean the machine on which this works would have a woefully outdated time zone database).
Your solution works if you replace PDT with UTC - it seems PDT is not recognized:
from datetime import datetime
date="08/30/2017 10:02 pm (UTC)"
dt = datetime.strptime(date, '%m/%d/%Y %I:%M %p (%Z)')
print(dt) # 2017-08-30 22:02:00
vs.
date="08/30/2017 10:02 pm (PDT)"
dt = datetime.strptime(date, '%m/%d/%Y %I:%M %p (%Z)')
print(dt) # time data '08/30/2017 10:02 pm (PDT)' does not match format '%m/%d/%Y %I:%M %p (%Z)'
(https://pyfiddle.io/ in 2.7 mode)

Using python datetime.datetime.strptime on windows with BST timezone

I need to parse many different dates in many different formats. I am having trouble with the following and wondered if anyopne could explain why;
The following works on a linux system:
from datetime import datetime
datetime.strptime('Tue 23 Aug 2011 09:00:07 PM BST','%a %d %b %Y %H:%M:%S %p %Z')
But running under windows it raises
ValueError: time data does not match format
However, if I try GMT not BST on windows, it works fine;
from datetime import datetime
datetime.strptime('Tue 23 Aug 2011 09:00:07 PM GMT','%a %d %b %Y %H:%M:%S %p %Z')
Is there a reason python does not understand the BST timezone under windows, but it works fine under Linux?
thanks,
Matt.
In my opinion, parsing a three-letter time zone code like this is not a good practice (unless of course you have no choice). For example, "EST" is commonly used in the USA for UTC-4/5 and is also commonly used in Australia. So any support for "EST" must therefore be dependent on locale. It would not surprise me if "BST" was similarly ambiguous.
I highly recommend using the pytz module in which British civil time is given the string identifier Europe/London and UTC is called Etc/UTC. The pytz API will give consistent results regardless of the locale of the user or system running the application.
If you are working on a UI that must be tied to locale, or parsing inputs with formats you cannot change, then consider using a dictionary of abbreviations to pytz timezone objects. For example: {'BST': 'Europe/London'}. Then your application can work with UTC dates and times uniformly, which will greatly reduce the possibility of errors.

Categories

Resources