我在
Windows 10 x64上使用
Python 3.6.0.
我刚发现在time.ctime(秒)中,seconds参数有一个隐含的最大值,即32536799999,几乎等于2 ^ 34.92135.
这是最大值吗?
错误消息只是说它是一个无效的数字.
>>> import time
>>> time.ctime(32536799999)
>>> 'Mon Jan 19 15:59:59 3001'
>>> time.ctime(32536799999+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
我用Google搜索并查看了Python文档,但我没有找到任何相关信息.我将在我的实验室中检查Ubuntu上的这个问题.
最佳答案 时间文档没有提到任何限制,但
datetime
documentation确实:
fromtimestamp()
may raiseOverflowError
, if the timestamp is out of the range of values supported by the platform Clocaltime()
orgmtime()
functions, andOSError
onlocaltime()
orgmtime()
failure.[…]
Naive
datetime
instances are assumed to represent local time and this method relies on the platform Cmktime()
function to perform the conversion. Sincedatetime
supports wider range of values thanmktime()
on many platforms, this method may raiseOverflowError
for times far in the past or far in the future.
然后我们前往Windows documentation:
_localtime64
, which uses the__time64_t
structure, allows dates to be expressed up through 23:59:59, December 31, 3000, coordinated universal time (UTC), whereas_localtime32
represents dates through 23:59:59 January 18, 2038, UTC.
localtime
is an inline function which evaluates to_localtime64
, andtime_t
is equivalent to__time64_t
. If you need to force the compiler to interprettime_t
as the old 32-bittime_t
, you can define_USE_32BIT_TIME_T
. Doing this will causelocaltime
to evaluate to_localtime32
. This is not recommended because your application may fail after January 18, 2038, and it is not allowed on 64-bit platforms.
所有与时间相关的功能(包括ctime)都以相同的方式工作.因此,您可以在Windows 10上的时间戳之间可靠地转换的最大日期是3000-12-31T23:59:59Z.
试图获得与平台无关的最大时间戳is difficult.