Skip to main content
 首页 » 编程设计

python之是否有与平台无关的 time.gmtime 实现

2025年05月04日11小虾米

在谷歌应用引擎 ( http://shell.appspot.com/ ) 上:

>>> time.gmtime(1000*365*24*60*60) 
(2969, 5, 3, 0, 0, 0, 2, 123, 0) 

在 macOS 上:

>>> time.gmtime(1000*365*24*60*60) 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
ValueError: timestamp out of range for platform time_t 

是否有与平台无关的 time.gmtime 实现?

请您参考如下方法:

time module被定义为特定于平台。

The functions in this module do not handle dates and times before the epoch or far in the future. The cut-off point in the future is determined by the C library; for Unix, it is typically in 2038.

您可以使用 datetime type没有时区信息(“天真的日期时间”),在您的程序中按照约定理解为 GMT,或者遵循文档中的(复杂的)tzinfo 说明。

>>> import datetime 
>>> datetime.datetime(2969, 5, 3).year 
2969 
>>> datetime.MINYEAR, datetime.MAXYEAR 
(1, 9999)