python value error time data %Z - python

I am getting the following error:
ValueError: time data 'Tue, 17 Jul 2018 11:55:34 EDT' does not match format '%a, %d %b %Y %H:%M:%S %Z'
Code:
import datetime
dt = datetime.datetime.strptime('Tue, 17 Jul 2018 11:55:34 EDT', '%a, %d %b %Y %H:%M:%S %Z')
print(dt.timestamp())
Am I missing something here?

EDT is not identified as a valid timezone string. You may use something other intelligent parser and than automatically return the datetime object without you explicitly specifying the format. My suggestion is to go up with dateutil
>>> from dateutil import parser
>>> parser.parse('Tue, 17 Jul 2018 11:55:34 EDT')
datetime.datetime(2018, 7, 17, 11, 55, 34)

Related

Python 3.7: converting a string with different timezone into date

I am trying to convert a string into date format in Python.
I am using following statement
datetime_obj = datetime.datetime.strptime("Sun Aug 19 16:24:31 PDT 2018", "%a %b %d %H:%M:%S %Z %Y")
However, I get an error -
ValueError: time data 'Sun Aug 19 16:24:31 PDT 2018' does not match format '%a %b %d %H:%M:%S %Z %Y'
If I remove the timezone from the date string and the format string, the code works perfect. Which leads me to believe that the issue is related to the timezone but I am not sure what actions should be taken.
I am in eastern timezone and the time zone in the string is in Pacific timezone.
Appreciate any help on this.
As mentioned in this answer you can use python-dateutil for this:
>>> from dateutil import parser
>>> datetime_obj = parser.parse("Sun Aug 19 16:24:31 PDT 2018")
datetime.datetime(2018, 8, 19, 16, 24, 31)
Standard datetime module behaves very strangely with parsing timezones, as I see reading this answer in question related to similar problem.

Value Error in Python when converting string to date using datetime.strptime

I have a string variable named dte that prints in the following format:
Tue Feb 13 23:49:10 +0000 2018
I'm trying to convert it to a date type so that I can work out how long ago it was in days, hours, minutes etc but keep getting the following error:
ValueError: time data 'Tue Feb 13 23:49:10 +0000 2018' does not match format '%a %b %d %I:%M:%S %z %Y'
I'm using this code:
new_date = datetime.datetime.strptime(dte, "%a %b %d %I:%M:%S %z %Y")
print(new_date)
I've tried removing the colons but get the same error. I'm sure it's something very simple I'm overlooking but can't figure out what it is.
Any help appreciated.
You need %H for the hour component not %I:
In[8]:
dte="Tue Feb 13 23:49:10 +0000 2018"
new_date = dt.datetime.strptime(dte, "%a %b %d %H:%M:%S %z %Y")
new_date
Out[8]: datetime.datetime(2018, 2, 13, 23, 49, 10, tzinfo=datetime.timezone.utc)
You have 24-hour hour component, not 12-hour which is what %I is for, see the docs

Error converting string to date: does not match format '%b %d %Y %I:%M%p'

I have a date in a string format: "Wed, 26 Apr 2017 12:39:28 GMT" and need to convert to datetime.
To do the conversion I am using datetime.strptime
Ex:
datetime.strptime ("Wed, 26 Apr 2017 12:39:28 GMT", '% b% d% Y% I:% M% p')
However, the error:
ValueError: time data 'Wed, 26 Apr 2017 12:39:57 GMT' does not match format '% b% d% Y% I:% M% p'
If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is raised. You should remove the redundant spaces and modify the directive which would be used to parse the given string.
you can try this:
>>> from datetime import datetime
>>>
>>> ds="Wed, 26 Apr 2017 12:39:28 GMT"
>>>
>>> datetime.strptime (ds, '%a, %d %b %Y %H:%M:%S %Z')
datetime.datetime(2017, 4, 26, 12, 39, 28)
See more details from strftime() and strptime() Behavior
Like the error says, the string doesn't match the format. Try this:
datetime.datetime.strptime('Wed, 26 Apr 2017 12:39:28 GMT', '%a, %d %b %Y %H:%M:%S %Z')

ValueError parsing time string

I have written this code to convert a unusual time into EPOCH:
x = 'Mon Jul 25 19:04:30 GMT+01:00 2016'
print(datetime.strptime(x, '%a %b %d %H:%M:%S %Z%z %Y').strftime('%s'))
However, it returns the error ValueError: time data 'Mon Jul 25 19:04:30 GMT+01:00 2016' does not match format '%a %b %d %H:%M:%S %Z%z %Y'
The problem is something to do with the timezone. What have I done wrong?
Your timezone format has an extra : inside which causes the format mismatching error, you can remove the last : from the string firstly and then parse it:
import re
from datetime import datetime
x1 = re.sub(r":(?=[^:]+$)", "", x) # remove the last semi colon
datetime.strptime(x1, '%a %b %d %H:%M:%S %Z%z %Y').strftime('%s')
# '1469487870'
If you use dateutil instead of datetime.strptime it seems to work:
from dateutil import parser
parser.parse("Mon Jul 25 19:04:30 GMT+01:00 2016")
>> datetime.datetime(2016, 7, 25, 19, 4, 30, tzinfo=tzoffset(None, -3600))

Converting string representation of time to date time in app engine

In standard python, I can convert a string representation of time into datetime doing this:
date_string = u'Tue, 13 Sep 2011 02:38:59 GMT';
date_object = datetime.strptime(date_string, '%a, %d %b %Y %H:%M:%S %Z');
This works fine until I invoke the same over app engine where I get the error:
time data did not match format: data=2011-09-13 02:38:59 fmt=%a, %d %b %Y %H:%M:%S %Z
How would I convert this date string correctly so I can get a datetime representation?
Your error message indicates that you're not really passing Tue, 13 Sep 2011 02:38:59 GMT, but 2011-09-13 02:38:59. Are you sure you pass the correct parameters to strptime?
My python works just fine for the following:
datetime.strptime(u'Tue, 13 Sep 2011 02:38:59 GMT', "%a, %d %b %Y %H:%M:%S %Z")
# returns datetime.datetime(2011, 9, 13, 2, 38, 59)
This also works fine for me:
from dateutil imoprt parser as dparser
dparser.parse("Tue, 13 Sep 2011 02:38:59 GMT")
# returns datetime.datetime(2011, 9, 13, 2, 38, 59, tzinfo=tzutc())

Categories

Resources