Skip to content

Conversation

dpgeorge
Copy link
Member

This PR applies the existing localtz.patch patch to add support for naive datetime objects. That is, objects that don't have any info about the current timezone.

This allows datetime.datetime.now() to work (prior to this patch it would raise NotImplementedError.

Although we don't really have support for localtime vs gmtime on bare-metal, ports such as the unix port and webassembly port do have this distinction, and for them being able to do datetime.datetime.now() is quite important (at least, that's what users expect to be able to do).

IMO it's too difficult to maintain separate datetime modules, one with support for naive objects, and one without. So just unconditionally apply this patch. That increases the .mpy size of the datetime module by +431 bytes (up to 8897 bytes total). To mitigate that, I've also applied some simple code-size optimisations to claw back -169 bytes (ie decrease datetime.mpy down to 8728 bytes total). That involved:

  • writing once-used helper functions inline in their place of use
  • writing once-used constant tuples inline in their place of use (I would have used from micropython import const but that renders the code not runnable under CPython for testing, and also increases code size itself for the import)
  • renamed _tmod to _t
  • renamed _format to _fmt
  • optimised timedelta._tuple() slightly

The associated unittest test has been updated and verified to pass (should also be run as part of CI).

@dpgeorge dpgeorge force-pushed the datetime-apply-localtz-patch branch from 20e0cbc to 3a71425 Compare September 24, 2025 15:02
This commit applies the existing `localtz.patch` patch to add support for
naive datetime objects.  That is, objects that don't have any info about
the current timezone.

This allows `datetime.datetime.now()` to work; prior to this patch it would
raise NotImplementedError.

Although we don't really have support for localtime vs gmtime on
bare-metal, ports such as the unix port and webassembly port do have this
distinction, and for them being able to do `datetime.datetime.now()` is
quite important (at least, that's what users expect to be able to do).

The associated unittest test has been updated.

This patch changes the size of datetime.mpy: 8466 -> 8897, so +431 bytes.

Signed-off-by: Damien George <damien@micropython.org>
Optimizations applied here are:
- writing once-used helper functions inline in their place of use
- writing once-used constant tuples inline in their place of use (I would
  have used `from micropython import const` but that renders the code not
  runnable under CPython for testing, and also increases code size itself
  for the import)
- renamed _tmod to _t
- renamed _format to _fmt
- optimised timedelta._tuple() slightly

Reduces datetime.mpy by: 8897 -> 8728, so saves 169 bytes.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the datetime-apply-localtz-patch branch from 3a71425 to 3eaf027 Compare October 1, 2025 14:28
@dpgeorge dpgeorge merged commit 3eaf027 into micropython:master Oct 1, 2025
4 checks passed
@dpgeorge dpgeorge deleted the datetime-apply-localtz-patch branch October 1, 2025 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant