Proper conversion of timestamp

Gregory Ewing greg.ewing at canterbury.ac.nz
Tue Mar 4 23:06:47 EST 2014


Igor Korot wrote:
> What I have is a timestamp which reads: 1289410678L.
> 
> Trying to convert this into the datetime object in Python using:
> 
> import datetime
> datetime.datetime.fromtimestamp( stamp )
> 
> produces the error: timestamp out of range for platform 
> localtime()/gmtime() function.

Divide the timestamp by 1000.0 to give floating point
seconds, and then use datetime.fromtimestamp().

 >>> d = datetime.datetime.fromtimestamp(1289410678 / 1000.0)
 >>> d
datetime.datetime(1970, 1, 16, 10, 10, 10, 678000)
 >>> d.strftime("%Y-%m-%d-%H:%M:%S.%f")[:-3]
'1970-01-16-10:10:10.678'

-- 
Greg



More information about the Python-list mailing list