Convert timestamps greater than maxint to datetime objects

I have code to convert some timestamps stored as strings into datetime objects, and noticed exceptions today when it converts dates with a timestamp value greater than 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 I want to dwell on a 32-bit python (works 2.7.2)

I noticed that I can convert max int to a datetime object and then add any additional data with timedeltas, but I could not come up with a particularly effective or good way to do this in practice. How can I convert these 2038+ timestamps to datetime objects?

+4
source share
1 answer

I think I worked it out, and I was surprised that this does not exclude the same exception.

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

EDIT: This is not an ideal solution, it seems a bit of a problem with time zones (I am currently in BST time (+1), so it may explain why it is lower by an hour)

 >>> 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) 
+7
source

Source: https://habr.com/ru/post/1412403/


All Articles