tags:

views:

39

answers:

1

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.

+2  A: 

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
Tamás