datetime: apply patch to add support for naive datetime objects #1045
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.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 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
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 decreasedatetime.mpy
down to 8728 bytes total). That involved:from micropython import const
but that renders the code not runnable under CPython for testing, and also increases code size itself for the import)_tmod
to_t
_format
to_fmt
timedelta._tuple()
slightlyThe associated unittest test has been updated and verified to pass (should also be run as part of CI).