Reputation: 65
The output of following code is totally wrong:
import time
from datetime import datetime
def sec_to_date(sec, format="%m/%d/%Y %H:%M:%S"):
tmp = datetime.fromtimestamp(sec)
fmtdate = tmp.strftime(format)
return fmtdate
def date_to_sec(fmtdate, format="%m/%d/%Y %H:%M:%S"):
t_tuple = time.strptime(fmtdate, format)
sec = time.mktime(t_tuple)
return sec
if __name__ == "__main__":
fmtdate = sec_to_date(1380204000)
print "1380204000 sec to date " + fmtdate
fmtdate = sec_to_date(1388355120)
print "1388355120 sec to date " + fmtdate
sec = date_to_sec("09/26/2013 10:00:00")
print "09/26/2013 10:00:00 to " + str(sec) + " sec"
sec = date_to_sec("12/29/2013 17:12:00")
print "12/29/2013 17:12:00 to " + str(sec) + " sec"
Here is the output:
1380204000 sec to date 09/26/2013 10:00:00
1388355120 sec to date 12/29/2013 17:12:00
09/26/2013 10:00:00 to 1380204000.0 sec
12/29/2013 17:12:00 to 1388355120.0 sec
The difference between two timestamps, 1380204000 and 1388355120, should be 94 days and 8.2 hours, while my results show a difference of 94 days and 7.2 hours. Any idea what happened?
Upvotes: 0
Views: 320
Reputation: 12002
Your issue is Daylight Saving Time. The time between the timestamps is indeed 94 days, 8.2 hours; but given DST, that means the formatted hour of the later time will be an hour behind where you expect.
Upvotes: 2