Having a difficult time with this, trying all these different date functions but no clue how to do it with any certainty.
Asked
Active
Viewed 3,088 times
1
-
Like in this question?: http://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date-in-python – Anders Lindahl Jul 15 '12 at 05:53
-
Maybe. specifically, everything needs to be in GMT timezone – MyNameIsKhan Jul 15 '12 at 05:54
2 Answers
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