为什么Datetime的`.timestamp()`方法返回OSError:[Errno 22]无效的参数`?

问题描述 投票:0回答:1

我在代码中两次使用.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这是为什么?

我在代码中两次使用.timestamp()函数,将datetime对象转换为纪元时间。对.timestamp()的第一次调用如下所示:import datetime origin_epoch = origin ....

python-3.x datetime epoch
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.