Is it possible for Python to fail out of range within two to three days?

Given the following code:

value = time.mktime(datetime.fromtimestamp(timestamp).timetuple()) 

Is it possible for this to create the argument “mktime overflowError“ out of range ”, and if so, what range of timestamps could trigger this?

+4
source share
2 answers

Yes. I am two hours earlier than UTC and Windows. I get an error for the last two hours of the int32 range:

 >>> def roundtrip(x): ... return time.mktime(datetime.datetime.fromtimestamp(x).timetuple()) ... >>> roundtrip(2**31-1-7200) 2147476447.0 >>> roundtrip(2**31-7200) OverflowError: mktime argument out of range >>> roundtrip(2**31-1) OverflowError: mktime argument out of range >>> roundtrip(2**31) ValueError: timestamp out of range for platform time_t 
+3
source

NB: as you asked for it, I think there is no possible timestamp that will throw the specific exception you are looking for, because datetime.fromtimestamp will raise a ValueError , since it is bound (possibly) by the same limits.

Now ranges for mktime are platform dependent, so I cannot give you a specific answer. On my 32-bit Linux computer, the boundaries are enclosed in a 32-bit integer, so the tuple that translates to a value less than -2147483648 (i.e., Before (1901, 12, 13, 19, 44, 16) ) or in the past 2147483647 , or (2038, 1, 19, 3, 14, 7) , raises this exception ... but again, for you, datetime.fromtimestamp will catch it earlier.

+1
source

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


All Articles