Why time.mktime returns different result on windows and linux for same input?

Viewed 77

time.mktime behaves differently on windows and linux. For a datetime value '0001-01-01 00:00:00' , on windows it throws exception "OverflowError: mktime argument out of range" while on Linux it does not throw any exception and returns an incorrect (-ve) value.

on windows -

>>> from dateutil.parser import parse    
>>> dt = parse('0001-01-01 00:00:00')
>>> dt
datetime.datetime(1, 1, 1, 0, 0)
>>> time.mktime(dt.timetuple())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: mktime argument out of range

On Linux

>>> from dateutil.parser import parse    
>>> dt = parse('0001-01-01 00:00:00')
>>> dt
datetime.datetime(1, 1, 1, 0, 0)
>>> time.mktime(dt.timetuple())
-62135596800.0

any pointer as to why same function is behaving differently on windows and Linux is appreciated.

1 Answers

The underlying C libraries for mktime used by the Python implementations for Windows and Linux are different.

As the documentation points out:

The earliest date for which it (mktime) can generate a time is platform-dependent.

Related