1

Having a difficult time with this, trying all these different date functions but no clue how to do it with any certainty.

MyNameIsKhan
  • 2,594
  • 8
  • 36
  • 56

2 Answers2

3

You can use calendar.timegm to get seconds since epoch. A time tuple is the required parameter and time.strptime can be used to generate that tuple.

Here is a quick example:

import calendar
import time

# Time in GMT
x = 'Sat Jul 14 22:05:54 2012'
y = time.strptime(x)
z = calendar.timegm(y)
print z # 1342303554 - the number of seconds since epoch
Jesse Harris
  • 1,131
  • 6
  • 10
0

The Unix timestamp is by definition in GMT (or rather UTC) format. You can use pythons datetime module and create a datetime object with timezone "None" by using the utcfromtimestamp constructor:

>>> print datetime.datetime.utcfromtimestamp(300)
1970-01-01 00:05:00

If you use the fromtimestamp constructor, you will get a datetime object adjusted for the timezone of your environment (in my case UTC+1):

>>> print datetime.datetime.fromtimestamp(300)
1970-01-01 01:05:00
Anders Lindahl
  • 41,582
  • 9
  • 89
  • 93