gpt4 book ai didi

python - Python 的 time.time() 是否返回本地或 UTC 时间戳?

转载 作者:IT老高 更新时间:2023-10-28 12:01:54 25 4
gpt4 key购买 nike

Python时间模块中的time.time()是返回系统时间还是UTC时间?

最佳答案

time.time()函数返回自纪元以来的秒数,作为 float 。请注意,“the epoch”定义为 UTC 时间 1970 年 1 月 1 日的开始。因此,时代是根据 UTC 定义的,并建立了一个全局时刻。无论您在地球上的哪个位置,“纪元后的秒数”(time.time()) 在同一时刻返回相同的值。

这是我在计算机上运行的一些示例输出,也将其转换为字符串。

>>> import time
>>> ts = time.time()
>>> ts
1355563265.81
>>> import datetime
>>> datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
'2012-12-15 01:21:05'
>>>

ts 变量是以秒为单位返回的时间。然后我使用 datetime 库将其转换为人类可读的字符串。

关于python - Python 的 time.time() 是否返回本地或 UTC 时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13890935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com