1

Can anybody explain the following?

timex = [2012, 3, 1]
epoch = calendar.timegm(datetime.datetime(*timex).utctimetuple())
date = datetime.date.fromtimestamp(epoch)
print date # [2012, 2, 29]

timex = [2012, 3, 15]
epoch = calendar.timegm(datetime.datetime(*timex).utctimetuple())
date = datetime.date.fromtimestamp(epoch)
print date # [2012, 3, 14]

I'm not sure if it has to do with my misunderstanding of tuples, lists or time in general, but python is always a day behind :P

earthmeLon
  • 640
  • 8
  • 20

1 Answers1

4

You're setting a time in UTC, but datetime.date.fromtimestamp() is converting it to local system time. You're west of UTC, setting the time to exactly midnight on a day, but your local time in e.g. Denver is the prior day.

Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
  • Do you know of a way to ensure UTC is used? – earthmeLon Sep 14 '13 at 02:45
  • 1
    Based on http://stackoverflow.com/questions/8777753/converting-datetime-date-to-utc-timestamp-in-python it appears to be more complicated than I'd expected. I don't know of a better answer offhand, though. – Jim Stewart Sep 14 '13 at 03:39