我利用了.timestamp()
在我的代码中运行两次,将日期时间对象转换为纪元时间。第一次拨打 .timestamp()
看起来像这样:
import datetime
origin_epoch = origin.timestamp()
变量的内容 origin
和 origin_epoch
是:同时,如果我尝试在代码的其他地方调用相同的方法
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/