datetime strftime does not output correct timestamp
Asked Answered
P

1

3

The following:

>>> from dateutil.parser import parse
>>> parse("2013-07-02 00:00:00 -0000")
datetime.datetime(2013, 7, 2, 0, 0, tzinfo=tzutc())

shows that the time should be 12am on July 2nd 2013 in UTC.

However:

>>> parse("2013-07-02 00:00:00 -0000").strftime("%s")
'1372744800'

1372744800 is actually Tue, 02 Jul 2013 06:00:00 UTC, which is wrong. Very confused.

Pleiades answered 2/7, 2013 at 18:24 Comment(4)
Where are you importing parse from?Iams
Couldn't reproduce; I get 1372741200 (an hour earlier). Pretty sure this is a time zone issue.Iams
that's still wrong, its just in a different TZ. even weirder: parse("2013-07-02 00:00:00 -0000").strftime("%s %z") will give you "1372744800 +0000", meaning strftime thinks it is in UTC.Pleiades
related issue on Python bug tracker: datetime.strftime('%s') should respect tzinfoDumps
C
3

See this question: Convert python datetime to epoch with strftime

Python doesn't actually support %s as an argument to strftime (if you check at http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior it's not in the list), the only reason it's working is because Python is passing the information to your system's strftime, which uses your local timezone.

Chewink answered 2/7, 2013 at 18:37 Comment(2)
thanks, that worked. people say python is a far superior language than PHP (where i'm coming from), but in PHP with DateTime, that wouldn't have been nearly as hard or confusing.Pleiades
You should checkout pytz and delorean. These makes it easy to wirk with datetimes. Also check out this thread on reddit!Chewink

© 2022 - 2024 — McMap. All rights reserved.