时间戳的最大值

2024-03-29 14:00:41 发布

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

我在Windows10x64上使用Python3.6.0。在

我刚刚发现在time.ctime(seconds)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

我搜索了一下Python文档,但是没有找到任何关于它的信息。我要在我的实验室里在Ubuntu上检查这个问题


Tags: importmost参数time数字calljanfile
3条回答

我在用 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:09:58) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] 在Windows10机器上运行的Ubuntu16.04VM中。在

我把你的ctime调用分解到它的各个部分,去调查,但是我没有达到同样的最大值。在

>>> time.asctime(time.localtime(32536799999-1))
'Mon Jan 19 02:59:58 3001'
>>> time.asctime(time.localtime(32536799999+1))
'Mon Jan 19 03:00:00 3001'
>>> time.asctime(time.localtime(32536799999+10))
'Mon Jan 19 03:00:09 3001'
>>> time.asctime(time.localtime(32536799999+10000))
'Mon Jan 19 05:46:39 3001'
>>> time.asctime(time.localtime(32536799999+1000000))
'Fri Jan 30 16:46:39 3001'
>>> time.asctime(time.localtime(32536799999+1000000000))
'Thu Sep 27 05:46:39 3032'
>>> time.ctime(32536799999+1000000000)
'Thu Sep 27 05:46:39 3032'
>>> time.asctime(time.gmtime(32536799999-1))
'Mon Jan 19 07:59:58 3001'
>>> time.asctime(time.gmtime(32536799999+1))
'Mon Jan 19 08:00:00 3001'
>>> time.asctime(time.gmtime(32536799999+1000000000))
'Thu Sep 27 09:46:39 3032'

或者从3.6.0到3.6.1中修复了一些问题,或者您的计算机遇到了一些有趣的问题。在

我确实在3.6.1中看到了以下与时间相关的变化: https://www.python.org/dev/peps/pep-0495/ 我想知道你碰巧用的时间是不是正好落在了一个折痕或一个缝隙里?你能试着在你的系统上增加一个多小时,看看它是否再次生效?在

time文档没有提到任何限制,但是^{} documentation提到了:

fromtimestamp() may raise OverflowError, if the timestamp is out of the range of values supported by the platform C localtime() or gmtime() functions, and OSError on localtime() or gmtime() failure.

[...]

Naive datetime instances are assumed to represent local time and this method relies on the platform C mktime() function to perform the conversion. Since datetime supports wider range of values than mktime() on many platforms, this method may raise OverflowError 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, and time_t is equivalent to __time64_t. If you need to force the compiler to interpret time_t as the old 32-bit time_t, you can define _USE_32BIT_TIME_T. Doing this will cause localtime 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)都是以相同的方式工作的。因此,在Windows10上,您可以可靠地在时间戳之间转换的最大日期是3000-12-31T23:59:59Z

正在尝试获取独立于平台的最大时间戳is difficult。在

这一定是因为您安装了Python,在版本3.5中,我从未遇到过这样的错误:

>>> time.ctime(32536799999)
'Mon Jan 19 07:59:59 3001'
>>> time.ctime(32536799999+1)
'Mon Jan 19 08:00:00 3001'
>>> time.ctime(32536799999+9999999999999999)
'Thu Feb 13 01:46:38 316890386'
>>> time.ctime(32536799999+99999999999999999)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 75] Value too large for defined data type

即使我处理一个巨大的数字,它也会抛出一个不同的error。在

相关问题 更多 >