Python datetime strptime() does not match format - python

I get the following error in which you can see the time data and the format I am using
time data '20:07:35 EEDT Wed Mar 31 2021' does not match format '%H:%M:%S %Z %a %b %d %Y'
I used the directives from here and I see that the format matches the description of each directive.
Can you see what is the issue here?

import datetime
time = '20:07:35 EEDT Wed Mar 31 2021'
time = time.replace('EEDT', '+0300')
datetime.datetime.strptime(time, '%H:%M:%S %z %a %b %d %Y')

you can map the abbreviated time zone to a IANA time zone name by dateutil's parser:
import dateutil
s = '20:07:35 EEDT Wed Mar 31 2021'
tzmapping = {"EEDT": dateutil.tz.gettz('Europe/Athens'),
"EEST": dateutil.tz.gettz('Europe/Athens')} # add more if needed...
dtobj = dateutil.parser.parse(s, tzinfos=tzmapping)
that will give you
dtobj
# >>> datetime.datetime(2021, 3, 31, 20, 7, 35, tzinfo=tzfile('Europe/Athens'))
dtobj.utcoffset()
# >>> datetime.timedelta(seconds=10800) # UTC+3
Note that timedelta arithmetic works correctly, i.e. includes DST changes:
from datetime import timedelta
dtobj -= timedelta(7) # DST change: dtobj is now EEST, UTC+2
dtobj.utcoffset()
# >>> datetime.timedelta(seconds=7200)

Problem is with EEDT. If you ignore EEDT(quickfix, not ideal), then your code may look like:
text = '20:07:35 EEDT Wed Mar 31 2021';
fmt = '%H:%M:%S EEDT %a %b %d %Y';
datetime.strptime(text, fmt)
--edit--
parsing datetime with timezone is difficult to pure datetime module. I'm not big expert, but pytz or python-datetutil should be good choice, according to this page: https://medium.com/#nqbao/python-timezone-and-daylight-savings-e511a0093d0

For those who are interested in different approach for similar, like GMT and BST or EEST and EEDT, it can be represented like this:
import datetime
try:
Time1 = datetime.datetime.strptime(DropTm,"%a %b %d %H:%M:%S GMT %Y")
except:
Time1 = datetime.datetime.strptime(DropTm,"%a %b %d %H:%M:%S BST %Y")
In your situation it will be:
import datetime
try:
Time1 = datetime.datetime.strptime(SomeValue,"%H:%M:%S EEDT %a %b %d %Y")
except:
Time1 = datetime.datetime.strptime(SomeValue,"%H:%M:%S EEST %a %b %d %Y")
Where is "SomeValue" your data!!
It did worked for me and do not need any other libraries! Good Luck with coding!!!

Related

strptime() gives ValueError: time data 'AM CDT' does not match format '%p %Z'

I have a str that I want to convert to a datetime. This str is this: 'Thursday, September 9, 2021 at 11:50 AM CDT. I am using the datetime.strptime() function, but it seems like the AM or time zone is not being recognized.
When I use the code
time = 'Thursday, September 9, 2021 at 11:50 AM CDT'
time = datetime.strptime(time, '%A, %B %d, %Y at %I:%M %p %Z')
I get the following:
ValueError: time data 'Thursday, September 9, 2021 at 11:50 AM CDT' does not match format '%A, %B %d, %Y at %I:%M %p %Z:%M %p %Z'
I've been able to convert the first part up until the %p %Z part, at which I get the following error:
ValueError: time data 'AM CDT' does not match format '%p %Z'
Any ideas on how dt.strptime() can recognize AM/PM and the time zone correctly?
Not sure of the best approach, but since Python 3.9 you can use the ZoneInfo module for this:
from datetime import datetime
from zoneinfo import ZoneInfo
CDT = ZoneInfo('CST6CDT')
# or alternatively:
# CDT = ZoneInfo('US/Central')
time = 'Thursday, September 9, 2021 at 1:50 PM'
time = datetime.strptime(time, '%A, %B %d, %Y at %I:%M %p')
time = time.replace(tzinfo=CDT)
print(time) # 2021-09-09 13:50:00-05:00
#MrFuppes made a good point about an ambiguity between the CST6EDT and US/Central zones. However, when I tried a quick test in DST and outside of DST, I couldn't see any noticeable difference, as the time zone seemed to adjust automatically - which indicates that either of those zone values appear to be DST- aware (unless I'm missing something of course).
I added an example below:
from datetime import datetime
from zoneinfo import ZoneInfo
# CDT = ZoneInfo('CST6CDT')
CDT = ZoneInfo('US/Central')
# Note: DST in 2021, ends on November 7th
time_dst = 'Saturday, November 6, 2021 at 1:50 PM'
time_st = 'Monday, November 8, 2021 at 1:50 PM'
time_dst = datetime.strptime(time_dst, '%A, %B %d, %Y at %I:%M %p')
time_dst = time_dst.replace(tzinfo=CDT)
time_st = datetime.strptime(time_st, '%A, %B %d, %Y at %I:%M %p')
time_st = time_st.replace(tzinfo=CDT)
print('DST: ', time_dst)
print('ST: ', time_st)
Output appears to be the same despite which ZoneInfo object is used:
DST: 2021-11-06 13:50:00-05:00
ST: 2021-11-08 13:50:00-06:00

Convert date string (from gmail) to timestamp | Python

I want to save the received date of emails from a Gmail account into a time-series database.
The problem is that I cannot convert the string that I got from the email to timestamp.
I tried this:
from datetime import datetime
date1 = 'Thu, 28 May 2020 08:15:58 -0700 (PDT)'
date1_obj = datetime.strptime(date1, '%a, %d %b %Y %H:%M:%S %z %Z')
print(date1_obj)
But got this error:
Traceback (most recent call last):
File "/format_date.py", line 11, in <module>
date1_obj = datetime.strptime(date1, '%a, %d %b %Y %H:%M:%S %z %Z')
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py", line 577, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py", line 359, in _strptime
(data_string, format))
ValueError: time data 'Thu, 28 May 2020 08:15:58 -0700 (PDT)' does not match format '%a, %d %b %Y %H:%M:%S %z %Z'
Tried with or without parenthesis wrapping Timezone.
Read a lot, but nothing about how to deal with date strings containing "(PDT)" or any other timezones. It's very important to get the right date... If I run the same code without "(PDT)", got an incorrect time (because of my local time).
I know I can use string methods to manipulate it and convert to a right datetime, but I feel like this would be flexible.
Sorry for my terrible English.
Thank you!
you could use dateutil's parser to parse the string, automatically inferring the format:
import dateutil
s = 'Thu, 28 May 2020 08:15:58 -0700 (PDT)'
dt = dateutil.parser.parse(s)
# datetime.datetime(2020, 5, 28, 8, 15, 58, tzinfo=tzoffset('PDT', -25200))
dt.utcoffset().total_seconds()
# -25200.0
Note that although the timezone is given a name ("PDT"), it is only a UTC offset of 25200 s. In many cases that is sufficient, at least to convert to UTC.
If you need the specific timezone (e.g. to account for DST transitions etc.), you can use a mapping dict that you supply to dateutil.parser.parse as tzinfos:
tzmap = {'PDT': dateutil.tz.gettz('US/Pacific'),
'PST': dateutil.tz.gettz('US/Pacific')}
dt = dateutil.parser.parse(s, tzinfos=tzmap)
# datetime.datetime(2020, 5, 28, 8, 15, 58, tzinfo=tzfile('US/Pacific'))
dt.utcoffset().total_seconds()
# -25200.0
Close, you forgot to put the bracket around the last entry.
date1_obj = datetime.strptime(date1, '%a, %d %b %Y %H:%M:%S %z (%Z)')
Well, after all your answers, which were very helpful, I finally solved.
This is how:
>>> from email.utils import parsedate_tz, mktime_tz
>>> date = 'Thu, 28 May 2020 08:15:58 -0700 (PST)'
>>> timestamp = mktime_tz(parsedate_tz(date))
>>> timestamp
1590678958
>>>
I checked that timestamp, and stands to 12:15:58 local time, what it's exactly what I was looking for.
Thank you very much to everybody who took a minute to answer.
If it does not work even if you enclose %Z in brackets then the problem lies within the %Z directive
https://docs.python.org/3/library/time.html
Support for the %Z directive is based on the values contained in
tzname and whether daylight is true. Because of this, it is
platform-specific except for recognizing UTC and GMT which are always
known (and are considered to be non-daylight savings timezones).
In example the following results in a ValueError for me (in Europe)
date1 = 'Thu, 28 May 2020 08:15:58 -0700 (PST)'
date1_obj = datetime.strptime(date1, '%a, %d %b %Y %H:%M:%S %z (%Z)')
print(date1_obj)
While with GMT it the output is 2020-05-28 08:15:58-07:00
date1 = 'Thu, 28 May 2020 08:15:58 -0700 (GMT)'
date1_obj = datetime.strptime(date1, '%a, %d %b %Y %H:%M:%S %z (%Z)')
print(date1_obj)
Based on your comment under this answer you could split the string if the Timezone bit is not important:
date1 = 'Thu, 28 May 2020 08:15:58 -0700 (GMT)'
date1_obj = datetime.strptime(date1.split(" (")[0], '%a, %d %b %Y %H:%M:%S %z')

How to fix ValueError time date does not match format when converting from string to datetime

Im trying to convert a string to datetime and keep getting the error: ValueError: time data 'Mon, 22 Apr 2019 17:04:38 +0200 (CEST)' does not match format '%a, %d %b %Y %H:%M:%S %z %Z'
from datetime import datetime
s = "Mon, 22 Apr 2019 17:04:38 +0200 (CEST)"
d = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z %Z')
What am i missing?
%Z is generally used for converting into string format. In any case, it is the offset, not the name of the time zone.
The rest of your code is valid, however:
s = "Mon, 22 Apr 2019 17:04:38 +0200"
d = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z')
datetime only comes with the ability to parse UTC and whatever local time zone is listed in time.tzname. It can't match (CEST) because it doesn't know what timezone that is (It would also be redundant because you defined the timezone using the offset +0200).
You will need to implement your own (CEST) using datetime.tzinfo or by importing an external library like pytz or pendulum in order to parse (CEST) from a string into a datetime.timezone.
Also, don't forget to include parenthesis() in your match string.
This code passes, however, I do not know what happens to 'CEST' once it is converted into the string.
from datetime import datetime
tz = 'CEST'
s = "Mon, 22 Apr 2019 17:04:38 +0200 " + tz
d = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z ' + tz)

How to convert time in this format to local time zone?

I'd like to convert this time Sat, 19 May 2018 16:32:56 +0000 to 20180519-113256 in a local timezone (EDT in this example) in python. Could anybody show me how to do it?
PS., The following example shows how to convert time to local timezone. But I am not sure how to parse Sat, 19 May 2018 16:32:56 +0000.
Convert UTC datetime string to local datetime with Python
You could choose any timezone you want:
import pytz
from datetime import datetime
s = 'Sat, 19 May 2018 16:32:56 +0000'
dt = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z')
tz = pytz.timezone('America/Chicago')
new_s = dt.astimezone(tz).strftime('%Y%m%d-%H%M%S')
for me this works:
from datetime import datetime
from dateutil import tz
def convert(date, from_zone = 'UTC', to_zone='America/New_York'):
from_zone = tz.gettz(from_zone)
to_zone = tz.gettz(to_zone)
date = date.replace(tzinfo=from_zone)
central = date.astimezone(to_zone)
return date
s = "Sat, 19 May 2018 16:32:56 +0000"
d = datetime.strptime(s, '%a, %d %B %Y %H:%M:%S +%f')
d = convert(d)

Python datetime formatting conversion

If I have a datetime object, like -
2012-03-10
How would I convert it into the following string format -
Sat, 10 Mar 2012 00:00:00 EDT
datetime.strptime("2012-03-10", "%Y-%m-%d").strftime("%a, %d %b %Y %H:%M:%S EDT")
Sorry about the primitive way of adding the timezone. See this question for better approaches.
>>> from datetime import datetime as dt
>>> a = dt.now().replace(tzinfo = pytz.timezone('US/Eastern'))
>>> a.strftime('%a, %d %b %Y %H:%M:%S %Z')
'Fri, 09 Mar 2012 20:02:51 EST'
Python 3.7+, using dateutil:
from datetime import datetime
from dateutil import tz
s = '2012-03-10'
dt = datetime.fromisoformat(s).replace(tzinfo=tz.gettz('US/Eastern'))
print(dt.strftime('%a, %d %b %Y %H:%M:%S %Z'))
>>> 'Sat, 10 Mar 2012 00:00:00 EST'
Python 3.9+, using zoneinfo:
from datetime import datetime
from zoneinfo import ZoneInfo
s = '2012-03-10'
dt = datetime.fromisoformat(s).replace(tzinfo=ZoneInfo('US/Eastern'))

Categories