Dateutil Parse Bug In Python Returns The Wrong Value
I have looked at many possible ways to parse python times. Using parse seems link the only method that should work. While trying to use datetime.strptime causes an error because %z
Solution 1:
This has nothing to do with the parser, you'll see the same behavior just from mktime()
alone, since datetime.timetuple()
doesn't have any time zone offset information, and mktime()
is the inverse of localtime
. You can correct this by converting it to localtime
before calling timetuple()
:
from time import mktime
from datetime import datetime
from dateutil import tz
dt_base = datetime(2012, 11, 9, 9, 4, 2)
dt_est = dt_base.replace(tzinfo=tz.tzoffset('EST', -5 * 3600))
dt_pst = dt_base.replace(tzinfo=tz.tzoffset('PST', -8 * 3600))
defprint_mktime(dt):
print(mktime(dt.timetuple()))
# Run in UTC
print_mktime(dt_est) # 1352469842.0
print_mktime(dt_pst) # 1352469842.0# Convert to local time zone first first
print_mktime(dt_est.astimezone(tz.tzlocal())) # 1352469842.0
print_mktime(dt_pst.astimezone(tz.tzlocal())) # 1352480642.0
Note that there is a chart on the documentation for time()
(python 2.x docs) that tells you how to convert between these representations:
FromTo Use
---------------------------------------------------------------------------
seconds since the epoch | struct_time in UTC | gmtime()
seconds since the epoch | struct_time inlocaltime|localtime()
struct_time in UTC | seconds since the epoch | calendar.timegm()
struct_time inlocaltime| seconds since the epoch | mktime()
My personal preference would be to convert the parsed date to UTC, in which case calendar.timegm()
would be the appropriate function:
from calendar import timegm
def print_timegm(dt):
print(timegm(dt.timetuple()))
print_timegm(dt_est.astimezone(tz.tzutc())) # 1352469842.0print_timegm(dt_pst.astimezone(tz.tzutc())) # 1352480642.0
Post a Comment for "Dateutil Parse Bug In Python Returns The Wrong Value"