时间戳最大值 [英] Maximum value of timestamp

查看:57
本文介绍了时间戳最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 10 x64 上使用 Python 3.6.0.

I am using Python 3.6.0 on Windows 10 x64.

我刚刚发现在time.ctime(seconds)中,seconds参数有一个隐含的最大值,即32536799999,几乎等于2^34.92135.

I just found that in time.ctime(seconds), seconds parameter has an implicit maximum value, which is 32536799999, almost equals to 2^34.92135.

这是最大值吗?

错误消息只是说这是一个无效的数字.

The error message just says it's an invalid number.

>>> 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 上检查这个问题.

I googled and looked on Python documentation, but I didn't find anything about it. And I'm going to check this problem on Ubuntu in my lab.

推荐答案

time 文档没有提到任何限制,但是 datetime 文档:

The time documentation doesn't mention any limits, but the datetime documentation does:

fromtimestamp() 可能会引发 OverflowError,如果时间戳超出平台 C localtime() 支持的值范围,或者gmtime() 函数,以及 localtime()gmtime() 失败时的 OSError.

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 实例被假定为表示本地时间,并且此方法依赖于平台 C mktime() 函数来执行转换.由于 datetime 在许多平台上支持比 mktime() 更广泛的值,因此此方法可能会在过去很长时间或很远的时间内引发 OverflowError未来.

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 文档:

_localtime64 使用 __time64_t 结构,允许将日期表示为 3000 年 12 月 31 日 23:59:59,协调世界时 (UTC),而 _localtime32 表示截至 2038 年 1 月 18 日 23:59:59 的日期,UTC.

_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 是一个内联函数,其计算结果为 _localtime64,而 time_t 等价于 __time64_t.如果您需要强制编译器将 time_t 解释为旧的 32 位 time_t,您可以定义 _USE_32BIT_TIME_T.这样做会导致 localtime 计算为 _localtime32.不建议这样做,因为您的应用程序可能会在 2038 年 1 月 18 日之后失败,并且在 64 位平台上是不允许的.

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)都以相同的方式工作.因此,您可以在 Windows 10 上的时间戳之间可靠地转换的最大日期是 3000-12-31T23:59:59Z.

All the time-related functions (including ctime) work the same way. So the max date you can reliably convert between timestamps on Windows 10 is 3000-12-31T23:59:59Z.

试图获得独立于平台的最大时间戳很困难.

Trying to get a platform-independent max timestamp is difficult.

这篇关于时间戳最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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