If you got here because a search engine told you this is how to get the Unix timestamp, stop reading this answer. Scroll down one.
If you want to reverse time.gmtime()
, you want calendar.timegm()
.
>>> calendar.timegm(time.gmtime())
1293581619.0
You can turn your string into a time tuple with time.strptime()
, which returns a time tuple that you can pass to calendar.timegm()
:
>>> import calendar
>>> import time
>>> calendar.timegm(time.strptime('Jul 9, 2009 @ 20:02:58 UTC', '%b %d, %Y @ %H:%M:%S UTC'))
1247169778
More information about calendar module here