micropython-lib icon indicating copy to clipboard operation
micropython-lib copied to clipboard

time: Add time module to provide strftime.

Open iabdalkader opened this issue 3 years ago • 5 comments

iabdalkader avatar Jul 16 '22 15:07 iabdalkader

FYI I've also got a recent open PR to add a time module with monotonic in https://github.com/micropython/micropython-lib/pull/502

It'd certainly be trivial to merge them eventually.

It's probably worth you adding from time import * in __init__.py so this new module transparently includes everything from there too?

andrewleech avatar Jul 17 '22 04:07 andrewleech

It's probably worth you adding from time import * in __init__.py so this new module transparently includes everything from there too?

I don't think it should import everything in there.

iabdalkader avatar Jul 17 '22 06:07 iabdalkader

In micropython, the C implementation of builtins are in the u modules, eg uos, ucollections. These are automatically shadowed to the non-u names if there's no other non-u implementation on path, eg os, collections for compatibility.

If there's a python / frozen version of the non-u version provided however that overrides the shadow; the intention is to allow python modules to extend the built-ins exactly like this PR is doing.

As-is, once this time.py is added to path, import time should no longer include the rest of the built in functionality? In my understanding the python built-in packages should wrap / override the built-in C u version, see https://github.com/micropython/micropython-lib/blob/70e422dc2e885bbaafe6eb7e3d81118e17d4b555/python-stdlib/collections/collections/init.py#L5

That wayimport time will give access to everything in C utime as well as the new python functionality.

andrewleech avatar Jul 17 '22 08:07 andrewleech

Ah okay, but you said :

It's probably worth you adding from time import * in init.py so this new module transparently includes everything from there too?

I think you meant from utime import * now that makes sense yes.

iabdalkader avatar Jul 17 '22 11:07 iabdalkader

I think you meant from utime import * now that makes sense yes.

Ah geez sorry for the confusion, I missed the unwanted autocorrect!

andrewleech avatar Jul 17 '22 12:07 andrewleech

Thanks for updating. Merged, and I removed the import re and associated global variable because it's no longer used.

I also added a unit test (taking out the test from time.py).

dpgeorge avatar Dec 14 '22 00:12 dpgeorge