Skip to content

extmod/modtime: Move tuple creation to common localtime implementation. #17898

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

Conversation

dpgeorge
Copy link
Member

Summary

This factors code out of the ports and into the common time.localtime implementation in extmod/modtime.c. That helps to reduce code duplication, prevent errors in implementation, and hopefully reduce code size.

Testing

Tested on PYBD_SF6, running the test suite (which includes tests for localtime()). It passes.

CI will test the unix port.

Trade-offs and Alternatives

This is a code refactoring, I don't see any downsides to it.

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Aug 12, 2025
Copy link

codecov bot commented Aug 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.38%. Comparing base (0ee3f99) to head (c0a3271).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17898   +/-   ##
=======================================
  Coverage   98.38%   98.38%           
=======================================
  Files         171      171           
  Lines       22295    22295           
=======================================
  Hits        21936    21936           
  Misses        359      359           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:   -32 -0.008% PYBV10
     mimxrt:   -24 -0.006% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@andrewleech
Copy link
Contributor

unix isn't covered here, it has a different localtime tuple layout iirc? Should it also be consolidated as a V2 breaking change?
Do you know how much size-add it would be to make this a namedtuple?

@dpgeorge
Copy link
Member Author

unix isn't covered here, it has a different localtime tuple layout iirc?

You're right, unix isn't touched by this PR. But it still returns the same layout (although it has an extra field for "isdst").

Do you know how much size-add it would be to make this a namedtuple?

Do you mean for all ports, or just unix?

I'm not really sure, but is making it a namedtuple important/useful?

@andrewleech
Copy link
Contributor

extra field for "isdst\

Ah yes that's it. The inconsistency does add some difficulty when writing modules / tests that are intended to run on both Unix and hardware, though noting DST can be quite important when the platform supports it.

I'm not really sure, but is making it a namedtuple important/useful?

Yeah sorry it's a bit off topic for this PR, but localtime and os.stat are two things I run into semi-regularly that have named attributes in cpython but not in micropython; it makes them harder to access individual elements in a documented way in code and harder to manually introspect on repl.

Every time I want to I need to research either online or by finding in the mpy source what the fields are, then for code use define field mappings to then reference the right field in a reviewable way.

@andrewleech
Copy link
Contributor

Regardless of the above comments, I'm in full support of the changes made already here, they look good and are a really logical cleanup.

This factors code out of the ports and into the common `time.localtime`
implementation in `extmod/modtime.c`.  That helps to reduce code
duplication, prevent errors in implementation, and reduce code size on
some ports (mimxrt and stm32 at least).

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the extmod-modtime-simplify-port-localtime-func branch from c0a3271 to b3cd1a3 Compare August 15, 2025 02:10
@dpgeorge dpgeorge merged commit b3cd1a3 into micropython:master Aug 15, 2025
68 checks passed
@dpgeorge dpgeorge deleted the extmod-modtime-simplify-port-localtime-func branch August 15, 2025 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants