最新时间戳Python

2024-05-08 19:41:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有这个输入: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

我的代码有什么问题?

谢谢!当做!


Tags: 代码datetimelibreoffice时间calctt2printtt
2条回答

伊曼纽尔

改变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

看来,LibreOffice时代并不等同于Posix时代。我发现这篇文章可能有帮助。

https://syslog.me/2012/08/28/dates-from-unix-timestamps-in-openoffice-libreoffice/

波塞克斯纪元是1970年1月1日午夜。

>>> datetime.utcfromtimestamp(0).strftime("%Y-%m-%d %H:%M:%S")
'1970-01-01 00:00:00'

图书馆时代是1899年12月30日。

除以86400表示您正在尝试将秒转换为天。但是,Python中的datetime.fromtimestamp函数需要以秒为单位的时间戳。

另外,在你给strftime的电话中,你颠倒了几个月和几分钟。%M表示分钟,%M表示月。

最后,您可能希望使用utcfromtimestamp而不是fromttimestamp来避免时区问题。

相关问题 更多 >

    热门问题