迄今为止的时间戳记Python

Emm*_*ias 3 python datetime timestamp

我正在尝试将时间戳(我不知道如何转换)转换为日期时间。

我输入以下内容:1217099350.0

如果我在Libreoffice calc(1217099350.0 / 86400)+ 29226上编写此代码,然后将其格式化为日期时间。我有一个正确的输出:

31/07/2018 19:09:10
Run Code Online (Sandbox Code Playgroud)

但是,如果我在python上做这个:

tt = 1217099350.0
tt2 = (tt / 86400.) + 29226.
tt3 = datetime.fromtimestamp(tt2).strftime("%Y-%M-%d %H:%m:%S"
print(tt3)
Run Code Online (Sandbox Code Playgroud)

我有下一个输出:

1970-01-01 09:01:52

我的代码有什么问题?

谢谢!问候!

Jef*_*per 6

伊曼纽尔

在Python中更改纪元可能需要大量工作。在调用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
Run Code Online (Sandbox Code Playgroud)