Python 的 time.time() 是否返回本地或 UTC 时间戳? [英] Does Python's time.time() return the local or UTC timestamp?

查看:58
本文介绍了Python 的 time.time() 是否返回本地或 UTC 时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does time.time() in the Python time module return the system's time or the time in UTC?

推荐答案

time.time() 函数返回自纪元以来的秒数,以秒为单位.请注意,纪元"被定义为 UTC 中 1970 年 1 月 1 日的开始.因此,纪元是根据 UTC 定义的,并建立了一个全球时刻.无论您身在何处,时代过去的秒数"(time.time()) 都会在同一时刻返回相同的值.

The time.time() function returns the number of seconds since the epoch, as seconds. Note that the "epoch" is defined as the start of January 1st, 1970 in UTC. So the epoch is defined in terms of UTC and establishes a global moment in time. No matter where you are "seconds past epoch" (time.time()) returns the same value at the same moment.

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

Here is some sample output I ran on my computer, converting it to a string as well.

Python 2.7.3 (default, Apr 24 2012, 00:00:54) 
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> ts = time.time()
>>> print ts
1355563265.81
>>> import datetime
>>> st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
>>> print st
2012-12-15 01:21:05
>>>

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

The ts variable is the time returned in seconds. I then converted it to a string using the datetime library making it a string that is human readable.

这篇关于Python 的 time.time() 是否返回本地或 UTC 时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆