Python - 将纪元时间的秒数转换为人类可读时间

Dan*_*uga 12 python time

最初我制作了这段代码,将日期转换为人类可读时间:

    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
Run Code Online (Sandbox Code Playgroud)

而我正试图使用​​纪元时间,但我不知道如何计算天数等等.

    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
Run Code Online (Sandbox Code Playgroud)

pyp*_*ism 37

import datetime
timestamp = 1339521878.04 
value = datetime.datetime.fromtimestamp(timestamp)
print(value.strftime('%Y-%m-%d %H:%M:%S'))
Run Code Online (Sandbox Code Playgroud)

  • 最好的便捷方式,减少打字,提高对错误的鲁棒性。 (2认同)

Joh*_*nck 13

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
Run Code Online (Sandbox Code Playgroud)