Home > Software engineering >  How to convert out of range epoch to datetime python?
How to convert out of range epoch to datetime python?

Time:04-08

We have a functionality where we want to convert epoch to datetime objects and format them as strings in python. Moreover, we also want to convert the out-of-range epochs as datetime and format them back as a string. However, the python datetime object doesn't support large dates such as 429790838400 which would format to 13/07/15589 date.

[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> dt = datetime.fromtimestamp(429790838400)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: year 15589 is out of range

Can someone please guide us on how to achieve this?

CodePudding user response:

The built in python datetime is limited by the number of seconds since the epoch 1970) you can store as an integer. As LMC correctly pointed out, the short answer to your question is “you can’t.”

The cftime library was developed for climate science, astronomy, and other applications where this temporal range isn’t sufficient. It has a datetime-compatible interface but supports a wider array of calendars and date ranges.

Check out cftime.num2date. I think it should work with your example out of the box as long as your integer is defined as seconds since 1970 in the standard Gregorian calendar.

Also, if you’re going to be working with CFTime alongside pandas or other pydata libraries, it’s worth checking out xr.cftime_range which will create a range index for use in resampling operations, etc.

CodePudding user response:

From datetime docs

datetime.MAXYEAR The largest year number allowed in a date or datetime object. MAXYEAR is 9999.

The max allowed timestamp could be

date -d '9999-12-31 23:59:59' ' %s'
253402311599

Using that value on python

Python 2.7.18 (default, Mar 04 2021, 23:25:57) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.fromtimestamp(253402311599)
datetime.datetime(9999, 12, 31, 23, 59, 59)

Adding 1 second

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

A workaround (?) on python3 to get the difference

>>> datetime.datetime.utcfromtimestamp(429790838400 - datetime.datetime(datetime.MAXYEAR,12,31,23,59,59).timestamp())
datetime.datetime(7559, 7, 13, 21, 0, 1)
  • Related