python 蟒蛇| mktime 溢出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2518706/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:49:23  来源:igfitidea点击:

Python | mktime overflow error

python

提问by Switch

I have been search all over the net and couldn't find an appropriate solution for this issue

我一直在网上搜索,找不到这个问题的合适解决方案

OverflowError: mktime argument out of range

The code that causes this exception

导致此异常的代码

 t = (1956, 3, 2, 0, 0, 0, 0, 0, 0)
 ser = time.mktime(t)

I would like to know the actual reason for this exception, some say that the date is not in a valid range but it doesn't make any sense to me, and if there's a range what it could be. Is it depends upon the system that we are using. Also would like to know a good solution for this issue.

我想知道这个例外的实际原因,有人说日期不在有效范围内,但对我来说没有任何意义,如果有一个范围可能是什么。这取决于我们使用的系统。也想知道这个问题的一个好的解决方案。

Thanks.

谢谢。

回答by Tamás

time.mktimecalls the underlying mktimefunction from the platform's C library. For instance, the above code that you posted works perfectly well for me on Mac OS X, although it returns a negative number as the date is before the Unix epoch. So the reason is that your platform's mktimeimplementation probably does not support dates before the Unix epoch. You can use Python's datetimemodule to construct a datetimeobject corresponding to the above date, subtract it from another datetimeobject that represents the Unix epoch and use the calculated timedeltaobject to get the number of seconds since the epoch:

time.mktimemktime从平台的 C 库调用底层函数。例如,您发布的上述代码在 Mac OS X 上非常适合我,尽管它返回一个负数,因为日期在 Unix 纪元之前。所以原因是您平台的mktime实现可能不支持 Unix 纪元之前的日期。可以使用 Python 的datetime模块构造一个datetime与上述日期对应的datetime对象,从另一个代表 Unix 纪元的对象中减去它,并使用计算出的timedelta对象来获得自纪元以来的秒数:

from datetime import datetime
epoch = datetime(1970, 1, 1)
t = datetime(1956, 3, 2)
diff = t-epoch
print diff.days * 24 * 3600 + diff.seconds

Update: if you are using Python 2.7 or above, you could simply use print diff.total_seconds()as noted below in Chad Miller's comment.

更新:如果您使用的是 Python 2.7 或更高版本,您可以简单地使用print diff.total_seconds()如下 Chad Miller 的评论中所述。

回答by The Demz

python timemodule

python时间模块

Although this module is always available, not all functions are available on all platforms. Most of the functions defined in this module call platform C library functionswith the same name. It may sometimes be helpful to consult the platform documentation, because the semantics of these functions varies among platforms.

The epoch is the point where the time starts, and is platform dependent. For Unix, the epoch is January 1, 1970, 00:00:00 (UTC). To find out what the epoch is on a given platform, look at time.gmtime(0).

尽管此模块始终可用,但并非所有功能都适用于所有平台。该模块中定义的大部分函数调用同名的平台 C 库函数。有时查阅平台文档可能会有所帮助,因为这些函数的语义因平台而异。

纪元是时间开始的点,取决于平台。对于 Unix,纪元是 1970 年 1 月 1 日 00:00:00 (UTC)。要了解给定平台上的纪元,请查看 time.gmtime(0)。

https://docs.python.org/3/library/time.html

https://docs.python.org/3/library/time.html

Windows 10:

视窗 10:

>>> time.gmtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

>>> list((ix for ix in time.gmtime(0)))
[1970, 1, 1, 0, 0, 0, 3, 1, 0]

>>> time.mktime(time.gmtime(0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: mktime argument out of range

The C library function on windows 10 does not support times below a certain value.

Windows 10 上的 C 库函数不支持低于特定值的时间。