0

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 0s.

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

Bijan
  • 7,737
  • 18
  • 89
  • 149
  • 3
    https://stackoverflow.com/a/23704862/7044293 – BugHunter May 18 '18 at 19:06
  • 1
    It's not about the leading zeros but about the timezone - it has always been clunky and it depends on the underlying library. In most cases it pays off to deal with the timezone separately instead of relying on `strptime()`. – zwer May 18 '18 at 19:07
  • Not sure why this was marked as duplicate.. The issue was because of the timezone, not leading 0 – Bijan May 22 '18 at 22:19

1 Answers1

2

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)
martineau
  • 119,623
  • 25
  • 170
  • 301