我正在尝试将时间戳(我不知道它是如何转换的)转换为日期时间。
我有这个输入:1217099350.0
如果我在 Libreoffice calc (1217099350.0/86400) + 29226 上写下这个并将单元格格式设置为日期时间。我有一个正确的输出:
31/07/2018 19:09:10
但是如果我在 python 上做这个:
tt = 1217099350.0
tt2 = (tt / 86400.) + 29226.
tt3 = datetime.fromtimestamp(tt2).strftime("%Y-%M-%d %H:%m:%S"
print(tt3)
我有下一个输出:
1970-01-01 09:01:52
我的代码有什么问题?
谢谢!问候!
最佳答案
伊曼纽尔,
在 Python 中更改 Epoch 可能需要大量工作。在调用 datetime.utfromtimestamp 之前,对 LibreOffice 时间戳进行一些数学运算,将其转换为 Posix 时间戳可能会更容易。
但是,如果您使用 1970 年 1 月 1 日之前的时间戳,这将不起作用。
from datetime import datetime
tt = 1217099350.0
tt2 = (tt / 86400.) + 29226.
# At this point tt2 has days since the LibreOffice Epoch. Below, it
# is converted to seconds since Posix epoch.
tt3 = tt2 - 25569. # There are 25569 days between LibreOffice and Posix epoch
tt4 = tt3 * 86400. # Convert timestamp from days to seconds
tt5 = datetime.utcfromtimestamp(tt4).strftime("%Y-%m-%d %H:%M:%S")
print(tt5)
2018-07-31 19:09:10
关于python - Python 日期的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51659683/