datetime: apply patch to add support for naive datetime objects#1045
Merged
dpgeorge merged 2 commits intomicropython:masterfrom Oct 1, 2025
Merged
datetime: apply patch to add support for naive datetime objects#1045dpgeorge merged 2 commits intomicropython:masterfrom
dpgeorge merged 2 commits intomicropython:masterfrom
Conversation
20e0cbc to
3a71425
Compare
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>
3a71425 to
3eaf027
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR applies the existing
localtz.patchpatch 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 raiseNotImplementedError.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
datetimemodule 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 decreasedatetime.mpydown to 8728 bytes total). That involved:from micropython import constbut that renders the code not runnable under CPython for testing, and also increases code size itself for the import)_tmodto_t_formatto_fmttimedelta._tuple()slightlyThe associated unittest test has been updated and verified to pass (should also be run as part of CI).