0
example = datetime.datetime(2014, 8, 19, 14, 18, 49, 435413, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=-240, name=None))

datetime.datetime.fromtimestamp(time.mktime(example.timetuple()))
datetime.datetime(2014, 8, 19, 15, 18, 49)

You can see that when I construct example, it's 14:18 (2:18 pm). When I convert it to a utc epoch, then back again, it's 15:18 (3:18 pm).

I'm using psycopg2's FixedOffsetTimezone because this row value comes from a database.

Trying to do everything in UTC yields even more confusing results.

datetime.datetime.utcfromtimestamp(time.mktime(example.utctimetuple()))
datetime.datetime(2014, 8, 19, 23, 18, 49)

In my first example, where is my extra hour coming? I suspect daylight saving time, DST, but my machine (and Python) yield the correct dates:

datetime.datetime.now()
datetime.datetime(2014, 8, 19, 14, 49, 21, 2429)

And from the shell:

> date
Tue Aug 19 14:49:49 EDT 2014
skyler
  • 8,010
  • 15
  • 46
  • 69
  • Found the answer here: http://stackoverflow.com/questions/8777753/converting-datetime-date-to-utc-timestamp-in-python. Specifically: http://stackoverflow.com/a/24484403/300368. – skyler Aug 19 '14 at 19:08
  • `timestamp = (example - datetime(1970, 1, 1, tzinfo=FixedOffsetTimezone(0, 'UTC'))).total_seconds()` – jfs Aug 20 '14 at 18:24

0 Answers0