UNIX timestamp from a datetime class

John Reese jtr at ofb.net
Tue Dec 6 19:32:46 CET 2005


Hi.

>>> import time, calendar, datetime
>>> n= 1133893540.874922
>>> datetime.datetime.fromtimestamp(n)
datetime.datetime(2005, 12, 6, 10, 25, 40, 874922)
>>> lt= _
>>> datetime.datetime.utcfromtimestamp(n)
datetime.datetime(2005, 12, 6, 18, 25, 40, 874922)
>>> gmt= _

So it's easy to create datetime objects from so-called UNIX timestamps
(i.e. seconds since Jan 1, 1970 UTC).  Is there any way to get a UNIX
timestamp back from a datetime object besides the following
circumlocutions?

>>> float(lt.strftime('%s'))
1133893540.0
>>> calendar.timegm(gmt.timetuple())
1133893540



More information about the Python-list mailing list