Convert date/time to unix timestamp?

Thomas Kraus thomas.kraus at merz-akademie.de
Tue Feb 10 07:42:17 EST 2009


Phillip B Oldham schrieb:
> Is there a simple way to set a date/time and convert it to a unix
> timestamp? After some googling I found the following:
> 
> t = datetime.time(7,0,0)
> starttime = time.mktime(t.timetuple())+1e-6*t.microsecond
> 
> That seems like very long-winded. Is there an easier way? I've read
> the docs on the datetime and time modules, but nothing's jumping out
> at me.

The built-in function time.localtime() returns a 9-item tuple, e.g. 
(2009, 2, 10, 13, 29, 7, 1, 41, 0).

time.mktime() converts this tuple to the seconds since the Epoch as a 
floating point number, int() converts it to an integer.

int(time.mktime(time.localtime()))

--
Thomas



More information about the Python-list mailing list