Reputation: 4170
I'm trying to convert my timestamp to unixtime paying attention to it not being UTC however the unixtime is incorrect.
import time
import datetime
from dateutil.parser import parse
time_str = '2017-03-09T15:44:40.000-05:00'
time_obj = parse(time_str)
print time_obj
unixtime = time.mktime(time_obj.timetuple())
print unixtime
The unixtime it returns is 1489092280.0
which represents to 03/09/2017 @ 8:44pm (UTC)
when what I want is 1489074280
.
Upvotes: 4
Views: 1323
Reputation: 155416
time.mktime
expects a broken-down local time and treats input as such, i.e. converts it to UTC and then builds a seconds-since-epoch timestamp. Since you already have the broken-down UTC time, you need to call calendar.timegm
instead:
>>> unixtime = calendar.timegm(time_obj.timetuple())
>>> print unixtime
1489074280
Upvotes: 4
Reputation: 12927
I believe your result was correct. 15:44 in -05:00 timezone is 20:44 (ie. 8:44 pm) in UTC.
Upvotes: 0