pywin32 icon indicating copy to clipboard operation
pywin32 copied to clipboard

PyTime to integer - year 2038 bug

Open moalexmonk opened this issue 7 years ago • 4 comments

Ran into an interesting bug with a date in 2121 being interpreted as 1985:

>>> datetime.datetime.fromtimestamp(int(pywintypes.Time((2121, 6, 30, 0, 0, 0))))
datetime.datetime(1985, 5, 23, 17, 31, 44)

Dug into it a bit, this seems like the year 2038 bug occurring twice:

>>> int(pywintypes.Time((2038, 1, 19, 3, 14, 7)))
2147483647
>>> int(pywintypes.Time((2038, 1, 19, 3, 14, 8)))
-2147483648

This is https://en.wikipedia.org/wiki/Year_2038_problem

>>> int(pywintypes.Time((2106, 2, 7, 6, 28, 14)))
-2
>>> int(pywintypes.Time((2106, 2, 7, 6, 28, 15)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: illegal time value
>>> int(pywintypes.Time((2106, 2, 7, 6, 28, 16)))
0

Probably a result of https://github.com/mhammond/pywin32/blob/master/win32/src/PyTime.cpp#L675

Should an nb_long function (doing basically the same thing but with PyLong_FromLong) be provided, and this restriction be documented / be made to throw an exception?

I also think that "illegal time value" exception is a problem but our hands may be tied by mktime there.

moalexmonk avatar Feb 13 '18 13:02 moalexmonk

Note this was in Python 2. It doesn't like you can use int() like this in Python 3?

moalexmonk avatar Feb 23 '18 15:02 moalexmonk

Should an nb_long function (doing basically the same thing but with PyLong_FromLong) be provided, and this restriction be documented / be made to throw an exception?

Yeah, I think that makes sense - we should supply nb_long, then work out how to have PyTime::intFunc throw when it would wrap.

mhammond avatar Feb 25 '18 04:02 mhammond

I would try to make a pull request but haven't been able to build pywin32 on my computer. The timestamp() function (PyWinMethod_NewTimeStamp) on these objects in Python 3 seems to work okay.

moalexmonk avatar Feb 26 '18 08:02 moalexmonk

I would try to make a pull request but haven't been able to build pywin32 on my computer.

If you know which changes to do, you can still submit a PR, and use the generated artefacts to confirm.

Avasam avatar Jun 05 '24 02:06 Avasam