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).

Change in datetime.mpy: 8466 -> 8897, so +431 bytes.

Signed-off-by: Damien George <[email protected]>
Reduces datetime.mpy by: 8897 -> 8728, so saves 169 bytes.

Signed-off-by: Damien George <[email protected]>
@dpgeorge dpgeorge force-pushed the datetime-apply-localtz-patch branch from 20e0cbc to 3a71425 Compare September 24, 2025 15:02
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