2015-07-21 18 views
15

Quando si tenta di convertire un timestamp con formato float, ad esempio 1437506779950.0 in un oggetto datetime, viene visualizzato un errore ValueError "year is out of range".python datetime dal timestamp che produce valore in anno fuori intervallo

Questo codice che ho usato, funzionava non 3 mesi fa. Rivisitarlo ora, stranamente ora sta lanciando questo errore, ma nulla nella base di codice è cambiato, solo i dati che gli vengono passati, e l'unico dato che è cambiato lì è ovviamente il timestamp.

>>> f = 1437506779950.0 
>>> datetime.datetime.fromtimestamp(float(f)) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ValueError: year is out of range 

Non riesco a capire cosa sarebbe cambiato per fare questa pausa?

+5

Vedere [questa domanda] (http://stackoverflow.com/questions/10286224/javascript-timestamp-to-python-datetime-conversion). Non stai forse convertendo da millisecondi a secondi? – samalamma708

+0

Sei sicuro che fosse il valore esatto che funzionava 3 mesi fa? anche questo - 'f = 143750677995.0' (il tuo numero diviso per 10) mi atterra nell'anno 6525. –

+0

samalamma708: potresti volerlo mettere come risposta, sembra che questo fosse il problema. –

risposta

31

Come indicato nella risposta per this question, questo sembra un problema di conversione di unità. Devi dividere il tuo timestamp per 1000 per convertire da millisecondi a secondi.