Converting timestamps larger than maxint into date

2019-06-24 02:46发布

问题:

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?

回答1:

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)


回答2:

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()