Python 的 time.time() 是否以 UTC 返回时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16554887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Python's time.time() return a timestamp in UTC?
提问by incognito2
I need to generate a UNIX timestamp in UTC time so I'm using time.time()to produce it.
Do I need to do anything else or is the timestamp automatically in UTC?
我需要在 UTC 时间生成一个 UNIX 时间戳,所以我time.time()用来生成它。
我需要做任何其他事情还是时间戳自动以 UTC 格式显示?
回答by timss
time.time()returns seconds since epoch, so it doesn't define which time standard or zone is being used.
time.time()返回自 epoch 以来的秒数,因此它没有定义正在使用的时间标准或区域。
Convert to time standards using:
使用以下方法转换为时间标准:
time.localtime([secs])- Local time as defined by your operating systemtime.gmtime([secs])- UTC
time.localtime([secs])- 由您的操作系统定义的当地时间time.gmtime([secs])- 世界标准时间
They both return a time.struct_time.
他们都返回一个time.struct_time.
>>> t = time.time()
>>> time.localtime(t)
time.struct_time(tm_year=2013, tm_mon=5, tm_mday=15, tm_hour=2, tm_min=41, tm_sec=49, tm_wday=2, tm_yday=135, tm_isdst=1)
>>> time.gmtime(t)
time.struct_time(tm_year=2013, tm_mon=5, tm_mday=15, tm_hour=0, tm_min=41, tm_sec=49, tm_wday=2, tm_yday=135, tm_isdst=0)
回答by abarnert
Technically, time.time()doesn't specify, and practically, at least in CPython, it returns a timestamp in whatever format is used by the underlying standard C library's timefunction.
从技术上讲,time.time()没有指定,实际上,至少在 CPython 中,它以底层标准 C 库time函数使用的任何格式返回时间戳。
The C standard (which isn't freely available) doesn't say whether this is GMT, and neither does the POSIX standard. It just says:
C 标准(不是免费提供的)没有说明这是否是 GMT,POSIX 标准也没有说明。它只是说:
The
time()function shall return the value of time in seconds since the Epoch.
该
time()函数应返回自 Epoch 以来的时间值(以秒为单位)。
… without saying anything about timezone, except that you can pass it to localtimeor gmtimeto get a "broken-down time" in local or GMT timezones.
... 不说时区,除了您可以将其传递给localtime或gmtime获得本地或 GMT 时区的“故障时间”。
So, this is platform-specific. A platform can return anything it wants for time, as long as it does so in a way that makes localtimeand gmtimework properly.
所以,这是特定于平台的。一个平台可以返回任何它想做对time的方式,品牌,只要它这样做localtime并gmtime正常工作。
That being said, it's usuallygoing to be GMT—or, rather, either UTC (Windows), or UTC-except-for-leap-seconds (most other platforms). For example, FreeBSDsays:
话虽如此,它通常是 GMT,或者更确切地说,UTC (Windows) 或 UTC-except-for-leap-seconds(大多数其他平台)。例如,FreeBSD说:
The
time()function returns the value of time in seconds since 0 hours, 0 minutes, 0 seconds, January 1, 1970, Coordinated Universal Time, without including leap seconds.
该
time()函数返回自 1970 年 1 月 1 日协调世界时 0 小时 0 分 0 秒以来的时间值,以秒为单位,不包括闰秒。
OS X and most other *BSDs have the same manpage, Windowsand linux/glibcalso specifically return UTC (with or without leap seconds), etc.
OS X 和大多数其他 *BSD 具有相同的联机帮助页,Windows和linux/glibc还专门返回 UTC(带或不带闰秒)等。
Also, the Python documentation says:
此外,Python 文档说:
To find out what the epoch is, look at
gmtime(0).
要了解时代是什么,请查看
gmtime(0).
Putting that together with the definitions for timeand gmtime, it would be much more work for a platform to return local timestamps than GMT. (That being said, this statement can't be all that authoritative, because it's actually not quite true for any POSIX platform, thanks to leap seconds.)
撇开这连同定义为time和gmtime,这将是更多的工作有一个平台比GMT返回本地时间戳。(话虽如此,这个说法不可能那么权威,因为它实际上对于任何 POSIX 平台都不完全正确,这要归功于闰秒。)

