For some reason python is not parsing my date properly but I looked at the strftime/strptime behavior and it looks to be right
import time
d = 'May 17, 2018 3:10 AM PDT'
time.mktime(time.strptime(d, "%B %d, %Y %I:%M %p %Z"))
If I do:
time.strftime("%B %d, %Y %I:%M %p %Z")
I get May 18, 2018 02:47 PM EDT
, which looks to be the exact same format except for the leading 0 but strptime
should be able to parse leading 0
s.
What am I doing wrong in parsing this date?
Edit: Found out its the timezone but not sure why:
time.mktime(time.strptime("May 17, 2018 3:10 AM UTC", "%B %d, %Y %I:%M %p %Z"))
returns a value
time.mktime(time.strptime("May 17, 2018 3:10 AM PDT", "%B %d, %Y %I:%M %p %Z"))
returns ValueError
Python date handling has always been a little light in the timezone handling department (it's a complicated problem). You can implement your own derived tzinfo
class from the abstract base class provided in the standard library if you only have a small subset of them that need to be handled—I've done it before and it's not too hard—or you can use something like the third-party dateutil
module recommended in the documentation at the end of the tzinfo
section which handles a much larger number of them.
Anyway, you can get dateutil
from here or you can simply install it from an OS command line with pip install py-dateutil
.
from dateutil import parser
t = parser.parse('May 17, 2018 3:10 AM PDT')
print('t: {!r}'.format(t)) # -> t: datetime.datetime(2018, 5, 17, 3, 10)