6 hours being added to timestamp

Asked by Andy R Terrel on 2011-05-10

I'm not sure if this is a pytz problem or python datetime problem, but somewhere I'm getting 6 hours added to my datetimes. generated from utctimestamps.

Any ideas why and how to get rid of it?

In [1]: import pytz

In [2]: from datetime import datetime

In [3]: import time

In [4]: datetime.fromtimestamp(time.mktime(datetime(7,1,1, tzinfo=pytz.UTC).timetuple()), tz=pytz.UTC)
Out[4]: datetime.datetime(2007, 1, 1, 6, 0, tzinfo=<UTC>)

In [5]: datetime.fromtimestamp(time.mktime(datetime(7,1,1).utctimetuple()), tz=pytz.UTC)
Out[5]: datetime.datetime(2007, 1, 1, 6, 0, tzinfo=<UTC>)

In [6]: datetime.fromtimestamp(time.mktime(datetime(7,1,1).utctimetuple()))
Out[6]: datetime.datetime(2007, 1, 1, 0, 0)

Question information

Language:
English Edit question
Status:
Solved
For:
pytz Edit question
Assignee:
No assignee Edit question
Solved by:
Andy R Terrel
Solved:
2011-05-11
Last query:
2011-05-11
Last reply:
Andy R Terrel (andy-terrel) said : #1

Okay I found the issue [0]. It was with time.mktime, using calendar works. Of course I wonder, why there isn't a datetime.totimestamp?

In [101]: ts = calendar.timegm(datetime(2010, 7, 1, tzinfo=pytz.utc).timetuple())

In [102]: datetime.fromtimestamp(ts, tz=pytz.utc)
Out[102]: datetime.datetime(2010, 7, 1, 0, 0, tzinfo=<UTC>)

[0] http://stackoverflow.com/questions/1077285/how-to-specify-time-zone-utc-when-converting-to-unix-time-python