Python

2014-10-09 4 views
6

Başlangıçta insan okunabilir zaman içine tarihini dönüştürmek için bu kod yapılan Okunabilir zaman içine dönem zaman saniye dönüştürme:Python

a = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S.%f") 
    b = datetime.datetime.now() 
    c = b - a 
    days, hours, minutes, seconds = int(c.days), int(c.seconds // 3600), int(c.seconds % 3600/60.0), int(c.seconds % 60.0) 
    return days, hours, minutes, seconds 
    EXAMPLE OUTPUT: 1 days, 4 hours, 24 minutes, 37 seconds 

ve bunu dönem süresini kullanarak yaptığınız çalışıyorum, ama hiç var vb

a = last_epoch #last epoch recorded 
    b = time.time() #current epoch time 
    c = b - a #returns seconds 
    hours = c // 3600/24 #the only thing I managed to figure out 
+1

o gün saat hesaplamak yapmak üzerine fikri ve Bu cevap size yardımcı olmalıdır: [Nasıl zaman subt bir insan okunabilir bir fark üretebilir Python kullanarak iki UNIX zaman damgasını raptediyorsunuz?] (http://stackoverflow.com/questions/6574329/how-can-i-produce-a-human-readable-difference-when-subtracting-two-unix-timestam) – siame

+0

@JohnZwinck hayır soru cevap vermedi, ben yıldönümünü son_epoch dönüştürebilir yıl 1970 olsun - time.time() –

cevap

8
a = last_epoch #last epoch recorded 
b = time.time() #current epoch time 
c = b - a #returns seconds 
days = c // 86400 
hours = c // 3600 % 24 
minutes = c // 60 % 60 
seconds = c % 60 
+0

Teşekkür ederim! Ekstra python modülleri kullanmadan en uygun yol –

24
import datetime 
timestamp = 1339521878.04 
value = datetime.datetime.fromtimestamp(timestamp) 
print(value.strftime('%Y-%m-%d %H:%M:%S'))