How do you do reverse gmtime()
, where you put the time + date and get the number of seconds?
I have strings like 'Jul 9, 2009 @ 20:02:58 UTC'
, and I want to get back the number of seconds between the epoch and July 9, 2009.
I have tried time.strftime
but I don't know how to use it properly, or if it is the correct command to use.
This should be different from
int(time.time())
, but it is safe to use something likex % (60*60*24)
datetime — Basic date and time types:
Use the time module:
Note that
time.gmtime
maps timestamp0
to1970-1-1 00:00:00
.time.mktime(time.gmtime(0))
gives you a timestamp shifted by an amount that depends on your locale, which in general may not be 0.The inverse of
time.gmtime
iscalendar.timegm
:You want
calendar.timegm()
.You can turn your string into a time tuple with
time.strptime()
, which returns a time tuple that you can pass tocalendar.timegm()
:More information about calendar module here
There are two ways, depending on your original timestamp:
mktime()
andtimegm()
http://docs.python.org/library/time.html