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.
You can combine both of your functions to make just one function, and using list comprehension, you can make that function run in one line. You cannot prevent overflow errors if you are working with very large numbers, instead, try catching them and then breaking: import math def fib(j): try: for i in [int(((1+math.
To correct this error Make sure that results of assignments, calculations, and data type conversions are not too large to be represented within the range of variables allowed for that type of value, and assign the value to a variable of a type that can hold a larger range of values, if necessary.
When an arithmetic operation exceeds the limits of the variable type, an OverflowError is raised. Long integers allocate more space as values grow, so they end up raising MemoryError.
In general, a data type overflow error is when the data type used to store data was not large enough to hold the data. Furthermore, some data types can only store numbers up to a certain size. An overflow error will be produced, for example, if a data type is a single byte and the data to be stored is greater than 256.
time.mktime
calls the underlying mktime
function 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's mktime
implementation probably does not support dates before the Unix epoch. You can use Python's datetime
module to construct a datetime
object corresponding to the above date, subtract it from another datetime
object that represents the Unix epoch and use the calculated timedelta
object to get the number of seconds since the epoch:
from datetime import datetime
epoch = datetime(1970, 1, 1)
t = datetime(1956, 3, 2)
diff = t-epoch
print diff.days * 24 * 3600 + diff.seconds
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With