Conversion d'une chaîne d'horodatage unix en date lisible

J'ai une chaîne représentant un timestamp unix (par exemple "1284101485&quot ;) en Python, et je voudrais la convertir en une date lisible. Lorsque j'utilise time.strftime, j'obtiens un TypeError :

>>>import time
>>>print time.strftime("%B %d %Y", "1284101485")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument must be 9-item sequence, not str

Utilisez le module datetime :

from datetime import datetime
ts = int("1284101485")

# if you encounter a "year is out of range" error the timestamp
# may be in milliseconds, try `ts /= 1000` in that case
print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
Commentaires (11)
>>> from datetime import datetime
>>> datetime.fromtimestamp(1172969203.1)
datetime.datetime(2007, 3, 4, 0, 46, 43, 100000)

Tiré de http://seehuhn.de/pages/pdate

Commentaires (0)
>>> import time
>>> time.ctime(int("1284101485"))
'Fri Sep 10 16:51:25 2010'
>>> time.strftime("%D %H:%M", time.localtime(int("1284101485")))
'09/10/10 16:51'
Commentaires (1)