Python Error22-简单日期时间转换的参数无效?
我有点被这个问题难住了,我不想撒谎,但每次我试图将13个字符的历元转换为可读的日期时,我都会遇到一个无效的参数错误。这在脚本的前面没有问题,出现错误的部分如下:Python Error22-简单日期时间转换的参数无效?,python,python-3.x,Python,Python 3.x,我有点被这个问题难住了,我不想撒谎,但每次我试图将13个字符的历元转换为可读的日期时,我都会遇到一个无效的参数错误。这在脚本的前面没有问题,出现错误的部分如下: print (msgTime) messageTime=datetime.utcfromtimestamp(msgTime) “msgTime”的值是从SQL数据库收集的,返回为:1540406475702 我得到的确切错误是: Traceback (most recent call last): File "script.py"
print (msgTime)
messageTime=datetime.utcfromtimestamp(msgTime)
“msgTime”的值是从SQL数据库收集的,返回为:1540406475702
我得到的确切错误是:
Traceback (most recent call last):
File "script.py", line 380, in <module>
messageTime=datetime.utcfromtimestamp(msgTime)
OSError: [Errno 22] Invalid argument
我知道这可能有点傻或小,但我就是看不出是怎么回事,有人有什么想法吗?eopch时间不包括毫秒,所以目前只有10位数长。你的是13,你可以通过地板除以1000来消除额外的毫秒 从日期时间导入日期时间 msgTime=154040647502 打印msgTime messageTime=datetime.utcfromtimestampsmsgtime//1000 打印消息时间 输出 更新 在查看了datetime的源代码以及它如何实现fromtimestamp之后,如果要传递毫秒,可以通过将值作为浮点传递来实现,其中小数点后的3位数字表示毫秒。下面是datetime模块的摘录 输出
msgTime的类型是什么?这可能会有帮助:在这里阅读官方文档:您的func正常,但msgTime的值错误;如果你尝试使用不同的数字-它只是工作;这里也有关于典型哥特加的解释。谢谢,抱歉。。奇怪的是,这在13个字符的时间戳中起了作用,这让我感到很不舒服。我不知道根据你当前的代码,这是怎么可能的。@TrueSlawter这仍然困扰着我,所以我查看了datetime的源代码。有没有可能,当它在你的纪元以1540406475.702的形式出现之前工作时,datetime会很高兴地处理它,并以毫秒为单位进行计算等。那么,它现在是否以1540406475702的形式出现呢?因为dateimte在一个纪元中识别毫秒/纳秒的唯一方法是它们是否在小数点后。看起来确实如此,克里斯!再次感谢您,抱歉浪费您的时间等等。@TrueSlawter欢迎您,这不是浪费时间。我也从中学习到了一些东西,因为我以前从未真正研究过日期时间的实现,所以我也从中得到了一些东西。
1540406475702
2018-10-24 18:41:15
@classmethod
def _fromtimestamp(cls, t, utc, tz):
"""Construct a datetime from a POSIX timestamp (like time.time()).
A timezone info object may be passed in as well.
"""
frac, t = _math.modf(t)
us = round(frac * 1e6)
if us >= 1000000:
t += 1
us -= 1000000
elif us < 0:
t -= 1
us += 1000000
converter = _time.gmtime if utc else _time.localtime
y, m, d, hh, mm, ss, weekday, jday, dst = converter(t)
ss = min(ss, 59) # clamp out leap seconds if the platform has them
result = cls(y, m, d, hh, mm, ss, us, tz)
1540406475.702
2018-10-24 18:41:15.702000