python - 为什么 Datetime 的 `.timestamp()` 方法返回 `OSError: [Errno 22] Invalid argument` ?

标签 python python-3.x datetime epoch

我利用了.timestamp()在我的代码中运行两次,将日期时间对象转换为纪元时间。第一次拨打 .timestamp()看起来像这样:

import datetime    
origin_epoch = origin.timestamp()
变量的内容 originorigin_epoch是:
Screenshot of Visual Studio Code's debugger for variables.
同时,如果我尝试在代码的其他地方调用相同的方法
import datetime
print(datetime.datetime(1900, 1, 1, 19, 6, 28).timestamp())
然后我收到以下错误:OSError: [Errno 22] Invalid argument为什么是这样?
编辑:此错误发生在 Windows 10 上。

最佳答案

1900 年是在 the UNIX epoch 开始之前,这是在 1970 年,所以 timestamp 返回的秒数必须为负。准确地说,should be negative ,但显然,不是你的情况。看起来您的操作系统只是将 UNIX 时代开始之前的日期视为错误。

不过,这在 macOS 上对我来说很好用:

>>> datetime.datetime(1900, 1, 1, 19, 6, 28).timestamp()
-2208929029.0

关于python - 为什么 Datetime 的 `.timestamp()` 方法返回 `OSError: [Errno 22] Invalid argument` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59199985/

相关文章:

javascript - 类属性的简写

python - MIMEText 中的标题编码

python - 比较 Python 中的两个日期对象 : TypeError: '<' not supported between instances of 'datetime.date' and 'method'

python - python中拉丁字符的特殊文本

python - 将 ctypes 与 jython 一起使用

python - Redis - xadd 和 xread 的 Python 示例

c# - 将 DateTimeOffset 转换为 DateTime 并为此 DateTime 添加偏移量

python - 如何在 QScrollArea() 之上显示 QPropertyAnimation()?

python - 如何使用 Django 和 Python 实现忘记密码功能

python - 如何从 python 中的 tzfile 中查找时区名称