python - 通过 Python 的 mktime 的往返是否有可能因 'out of range' 而失败?

标签 python datetime time

给定以下代码:

value = time.mktime(datetime.fromtimestamp(timestamp).timetuple())

这是否可能产生 OverflowError 'mktime argument out of range',如果是,什么范围的时间戳会导致这种情况?

最佳答案

是的。我比 UTC 和 Windows 提前两个小时。我在 int32 范围的最后两个小时收到错误:

>>> def roundtrip(x):
...     return time.mktime(datetime.datetime.fromtimestamp(x).timetuple())
...
>>> roundtrip(2**31-1-7200)
2147476447.0
>>> roundtrip(2**31-7200)
OverflowError: mktime argument out of range
>>> roundtrip(2**31-1)
OverflowError: mktime argument out of range
>>> roundtrip(2**31)
ValueError: timestamp out of range for platform time_t

关于python - 通过 Python 的 mktime 的往返是否有可能因 'out of range' 而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499112/

相关文章:

python - 使用openpyxl用颜色填充单元格?

MySQL CSV 文件导入日期时间

jquery - 为什么更改日期后 "datetime-local"的值为空?

ios - iOS 启动时间会漂移吗?

android - 如何比较android中的已知时间和当前时间?

Python 捕获 stdout/stderr 并在查看输出时记录到文件

Python - 人类喜欢鼠标的行为

python - _curses.error : addwstr() returned ERR on changing nlines to 1 on newwin method

MySQL 允许 1000 年之前的日期吗?

regex - 简化时间文字的正则表达式(如 "10h50m")