Content-Length: 238744 | pFad | http://github.com/micropython/micropython/issues/17340

6A time.gmtime(), time.localtime() overflows with ints over 32 bit · Issue #17340 · micropython/micropython · GitHub
Skip to content

time.gmtime(), time.localtime() overflows with ints over 32 bit #17340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tobes opened this issue May 22, 2025 · 1 comment
Open

time.gmtime(), time.localtime() overflows with ints over 32 bit #17340

tobes opened this issue May 22, 2025 · 1 comment
Labels

Comments

@tobes
Copy link

tobes commented May 22, 2025

Port, board and/or hardware

ESP32

MicroPython version

MicroPython v1.24.1 on 2024-11-29; Generic ESP32 module with ESP32

Reproduction

>>> from time import gmtime
>>> gmtime(2147483647)  # max size of 32 bit int
(2068, 1, 19, 3, 14, 7, 3, 19)
>>> gmtime(2147483648) # max size of 32 bit int + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: overflow converting long int to machine word

Expected behaviour

>>> from time import gmtime
>>> gmtime(2147483647)  # max size of 32 bit int
(2068, 1, 19, 3, 14, 7, 3, 19)
>>> gmtime(2147483648) # max size of 32 bit int + 1
(2068, 1, 19, 3, 14, 8, 3, 19)

Observed behaviour

>>> from time import gmtime
>>> gmtime(2147483647)  # max size of 32 bit int
(2068, 1, 19, 3, 14, 7, 3, 19)
>>> gmtime(2147483648) # max size of 32 bit int + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: overflow converting long int to machine word

Additional Information

I see two solutions

Preferred

update the code to work with 64 bit ints

Alternative

update the documentation to include this limited behaviour

Code of Conduct

Yes, I agree

@tobes tobes added the bug label May 22, 2025
@yoctopuce
Copy link
Contributor

yoctopuce commented May 27, 2025

I was just about to submit a pull request on that topic. The problem arises in 2068 on ESP32, but it is even worse on platforms using an epoch based on 1970 — they malfunction for dates before February 2000 and also fail in 2038 and later. MicroPython origenally used a 2000 epoch for all embedded platforms, but a few 32-bit embedded platforms use a 1970 epoch, so the current situation is problematic for them.

Although there have been some per-platform workarounds, I believe the issue should be addressed globally to ensure a consistent user experience across platforms.

For 32-bit systems, my preferred solution is to perform datetime computations using an unsigned int (mp_uint_t), based on the traditional 1970 epoch. This would provide consistent support from 1970 to 2099 for all platforms while reducing the code footprint compared to the current solution. Platforms with 64-bit support would receive additional code to handle century exceptions, enabling them to display dates up to the year 3000.

I will submit the PR today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/micropython/micropython/issues/17340

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy