2012-09-13 16 views
169

sto ottenendo una risposta da parte del resto è un formato ora Epoch comeConversione tempo Epoch nella datetime

start_time = 1234566 
end_time = 1234578 

voglio convertire che i secondi epoca in MySQL tempo formato in modo da poter memorizzare le differenze di mia Database MySQL.

ho provato:

>>> import time 
>>> time.gmtime(123456) 
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=2, tm_hour=10, tm_min=17, tm_sec=36, tm_wday=4, tm_yday=2, tm_isdst=0) 

Il risultato di cui sopra non è quello che mi aspetto. Voglio che sia come

2012-09-12 21:00:00 

Si prega di suggerire come posso ottenere questo?

Inoltre, perché sto ottenendo TypeError: a float is required per

>>> getbbb_class.end_time = 1347516459425 
>>> mend = time.gmtime(getbbb_class.end_time).tm_hour 
Traceback (most recent call last): 
    ... 
TypeError: a float is required 
+0

sei sicuro di '1347516459425'? Questa non è un'epoca valida. Prova 'time.gmtime (float (str (i) [: - 6] + '.'+ str (i) [- 6:])) 'dove' i' è '1347516459425'. –

+0

@Burhan Come hai scoperto che questa non è un'Epoca valida? – user1667633

+1

Poiché un'Epoca è solo nell'intervallo ~ 10¹⁰. Prova a usare time.ctime (1347516459.425), questo ti darà il 13 settembre 12. Attenzione al decimale –

risposta

198

per convertire il vostro valore del tempo (float o int) per una stringa formattata, uso:

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370)) 
+2

time.strftime ('% Y-% m-% d% H:% M:% S', time.localtime (1347517491247)) '44671-02-17 11:44:07' perché? – user1667633

+2

Da dove viene l'ora "1347517491247" nel tuo esempio? È un valore in tempo reale che stai utilizzando? –

+0

effettivamente ricevo quel valore dal server bigbluebutton – user1667633

6

Prova questo:

>>> import time 
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1347517119)) 
'2012-09-12 23:18:39' 

anche in MySQL, è possibile FROM_UNIXTIME come:

INSERT INTO tblname VALUES (FROM_UNIXTIME(1347517119)) 

Per la vostra seconda domanda , probabilmente è perché getbbb_class.end_time è una strinatura g. È possibile convertirlo in numerico come: float(getbbb_class.end_time)

1

Prima un po 'di informazioni in epoca da uomo gmtime

The ctime(), gmtime() and localtime() functions all take an argument of data type time_t which represents calendar time. When inter- 
     preted as an absolute time value, it represents the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal 
     Time (UTC). 

per capire come epoca dovrebbe essere.

>>> time.time() 
1347517171.6514659 
>>> time.gmtime(time.time()) 
(2012, 9, 13, 6, 19, 34, 3, 257, 0) 

solo garantire l'arg si sta passando a time.gmtime() è intero.

8

Questo è quello che vi serve

In [1]: time.time() 
Out[1]: 1347517739.44904 

In [2]: time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time())) 
Out[2]: '2012-09-13 06:31:43' 

ingresso prego un float invece di un int e che altro TypeError dovrebbe andare via.

mend = time.gmtime(float(getbbb_class.end_time)).tm_hour 
134

è anche possibile utilizzare datetime:

>>> import datetime 
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%c') 
    '2012-09-13 02:22:50' 
+3

in cui il fuso orario esegue questa conversione, e se Voglio essere in un fuso orario specifico. ? – garg10may

+0

@ garg10may time epoca è sempre offset UTC 0, quindi la stringa sarà anche offset UTC 0. Questo è equivalente al fuso orario Zulu (+00: 00). Puoi leggere di più qui: https://www.timeanddate.com/time/zones/z – Devnetics

+0

C'è la funzione 'utcfromtimestamp' per ottenere l'ora UTC in python 3. – vwvolodya

4
#This adds 10 seconds from now. 
from datetime import datetime 
import commands 

date_string_command="date +%s" 
utc = commands.getoutput(date_string_command) 
a_date=datetime.fromtimestamp(float(int(utc))).strftime('%Y-%m-%d %H:%M:%S') 
print('a_date:'+a_date) 
utc = int(utc)+10 
b_date=datetime.fromtimestamp(float(utc)).strftime('%Y-%m-%d %H:%M:%S') 
print('b_date:'+b_date) 

Questo è un po 'più prolisso ma viene da comando date in UNIX.

2
>>> import datetime 
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S') 
'2012-09-13 14:22:50' # Local time 

Per ottenere UTC,

>>> datetime.datetime.utcfromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S') 
    '2012-09-13 06:22:50' 
Problemi correlati