python datetime fromtimestamp产生valueerror年超出范围 [英] python datetime fromtimestamp yielding valueerror year out of range

查看:187
本文介绍了python datetime fromtimestamp产生valueerror年超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试将浮点格式的时间戳(例如 1437506779950.0 )转换为日期时间对象时,出现ValueError年份超出范围。

When attempting to convert a float formatted timestamp e.g 1437506779950.0 into a datetime object, I'm getting a ValueError "year is out of range".

我使用的这段代码在3个月前还没有运行。现在再来看看,奇怪的是现在抛出此错误,但代码库中的任何内容都没有更改,只有传递给它的数据,并且唯一更改的数据显然是时间戳。

This code that I used, was working not 3 months ago. Revisiting it now, strangely is now throwing this error yet nothing in the code base has changed, only the data that is being passed to it, and the only data that has changed there is obviously the timestamp.

>>> f = 1437506779950.0
>>> datetime.datetime.fromtimestamp(float(f))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: year is out of range

我不知道会发生什么变化

I can't understand what would have changed to make this break?

推荐答案

此问题,它看起来像是单位转换问题。您必须将时间戳除以1000才能从毫秒转换为秒。

As noted in the answer for this question, this looks like a unit conversion issue. You have to divide your timestamp by 1000 to convert from milliseconds to seconds.

如果要保留毫秒精度,请除以1000.0。

If you want to preserve millisecond precision, instead divide by 1000.0.

这篇关于python datetime fromtimestamp产生valueerror年超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆