I have been search all over the net and couldn’t find an appropriate solution for this issue
OverflowError: mktime argument out of range
The code that causes this exception
t = (1956, 3, 2, 0, 0, 0, 0, 0, 0)
ser = time.mktime(t)
I would like to know the actual reason for this exception, some say that the date is not in a valid range but it doesn’t make any sense to me, and if there’s a range what it could be. Is it depends upon the system that we are using. Also would like to know a good solution for this issue.
Thanks.
time.mktimecalls the underlyingmktimefunction from the platform’s C library. For instance, the above code that you posted works perfectly well for me on Mac OS X, although it returns a negative number as the date is before the Unix epoch. So the reason is that your platform’smktimeimplementation probably does not support dates before the Unix epoch. You can use Python’sdatetimemodule to construct adatetimeobject corresponding to the above date, subtract it from anotherdatetimeobject that represents the Unix epoch and use the calculatedtimedeltaobject to get the number of seconds since the epoch:Update: if you are using Python 2.7 or above, you could simply use
print diff.total_seconds()as noted below in Chad Miller’s comment.