将大于maxint的时间戳转换为日期时间对象 [英] Converting timestamps larger than maxint into datetime objects

查看:116
本文介绍了将大于maxint的时间戳转换为日期时间对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以将一些存储为字符串的时间戳转换为datetime对象,今天注意到了例外,当它转换的int时间戳值大于最大int时。

I have some code for converting some timestamps stored as strings into datetime objects and noticed today exceptions when it converts dates with an int timestamp value greater than the max int.

datetime.datetime.fromtimestamp(2147570047)

例如我

ValueError: timestamp out of range for platform time_t

如何解决这个问题?假设我想继续使用32位python(运行2.7.2)

How can I get around this problem? assuming that I want to stay on 32-bit python (running 2.7.2)

我注意到我可以将max int转换为datetime对象,然后添加任何其他内容timedeltas,但我想不出在实践中这样做的特别有效或不错的方式。我可以将这2038个以上的时间戳转换为日期时间对象的好方法是什么?

I noticed I can convert the max int into the datetime object then add on any extra with timedeltas but I couldn't think of a particularly efficient or nice way of doing this in practice. What's a good way I can convert these 2038+ timestamps into datetime objects?

推荐答案

我想出来了,我有点

>>> datetime.datetime.fromtimestamp(0) + datetime.timedelta(seconds=2147570047)
datetime.datetime(2038, 1, 20, 4, 14, 7)

编辑:这不是一个完美的解决方案,似乎与时区有关(我目前是BST时间(+1),所以可能解释为什么下面要相隔一个小时)

This isn't a perfect solution, seems to be a bit of issues with time zones (I'm currently on BST time (+1) so might explain why this below is an hour apart)

>>> datetime.datetime.fromtimestamp(2047570047)
datetime.datetime(2034, 11, 19, 17, 27, 27)
>>> datetime.datetime.fromtimestamp(0) + datetime.timedelta(seconds=2047570047)
datetime.datetime(2034, 11, 19, 18, 27, 27)

这篇关于将大于maxint的时间戳转换为日期时间对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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