5

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)

for example gives me

ValueError: timestamp out of range for platform time_t

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

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?

GP89
  • 6,600
  • 4
  • 36
  • 64

2 Answers2

8

Think i worked it out, and I was kinda surprised that this doesn't throw the same exception

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

EDIT: 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)
GP89
  • 6,600
  • 4
  • 36
  • 64
  • 5
    The reason `fromtimestamp` has trouble (on some systems) is that it is based on system C functions. The documentation notes that this is often constrained to the years 1970 to 2038. `timedelta` on the other hand is custom-built for Python, and handles durations up to slightly less than one billion days. – John Y May 14 '12 at 21:53
0

try this

__author__ = 'allenlin'
import math
import datetime
import calendar


def datetime2timestamp(dt):
    ts = calendar.timegm(dt.timetuple())
    return ts


def timestamp2datetime(timestamp):
    day_sec = 60*60*24
    ts_days = int(math.floor(timestamp / day_sec))
    ts_mod_sec = int(timestamp - (ts_days * day_sec))
    hour_sec = 60*60
    ts_mod_hour = int(math.floor(ts_mod_sec / hour_sec))
    ts_mod_sec = int(ts_mod_sec - (ts_mod_hour * hour_sec))
    min_sec = 60
    ts_mod_minute = int(math.floor(ts_mod_sec / min_sec))
    ts_mod_sec = int(ts_mod_sec - (ts_mod_minute * min_sec))

    revert = datetime.datetime(1970, 1, 1) + \
             datetime.timedelta(
                 days=ts_days,
                 hours=ts_mod_hour,
                 minutes=ts_mod_minute,
                 seconds=ts_mod_sec
             )
    return revert


def main():
    pass
    d_max = datetime.datetime.max
    print(d_max)
    ts_max = datetime2timestamp(d_max)
    print(ts_max)
    print (timestamp2datetime(ts_max))

    d_now = datetime.datetime.now()
    print(d_now)
    ts_now = datetime2timestamp(d_now)
    print(ts_now)
    print (timestamp2datetime(ts_now))


if __name__ == '__main__':
    main()
  • You can simply do `datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=timestamp)`. Also check out [`divmod`](https://docs.python.org/3.6/library/functions.html#divmod) :) – GP89 May 18 '18 at 13:06
  • eg. `ts_days, ts_mod_sec = divmod(timestamp, day_sec)` – GP89 May 18 '18 at 13:14