diff --git a/time/example_strftime.py b/time/example_strftime.py new file mode 100644 index 00000000..bf5f05af --- /dev/null +++ b/time/example_strftime.py @@ -0,0 +1,3 @@ +import time + +print(time.strftime("%Y-%m-%d %H:%M:%S")) diff --git a/time/metadata.txt b/time/metadata.txt index dc5f60a6..a9a85a5b 100644 --- a/time/metadata.txt +++ b/time/metadata.txt @@ -1,3 +1,3 @@ -srctype = dummy +srctype = micropython-lib type = module -version = 0.0.1 +version = 0.1 diff --git a/time/setup.py b/time/setup.py index 5817f03d..7b97316d 100644 --- a/time/setup.py +++ b/time/setup.py @@ -6,9 +6,9 @@ from setuptools import setup setup(name='micropython-time', - version='0.0.1', - description='Dummy time module for MicroPython', - long_description='This is a dummy implementation of a module for MicroPython standard library.\nIt contains zero or very little functionality, and primarily intended to\navoid import errors (using idea that even if an application imports a\nmodule, it may be not using it onevery code path, so may work at least\npartially). It is expected that more complete implementation of the module\nwill be provided later. Please help with the development if you are\ninterested in this module.', + version='0.1', + description='time module for MicroPython', + long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.", url='https://github.com/micropython/micropython/issues/405', author='MicroPython Developers', author_email='micro-python@googlegroups.com', diff --git a/time/time.py b/time/time.py index bc54271a..f311433a 100644 --- a/time/time.py +++ b/time/time.py @@ -1 +1,26 @@ from utime import * +import ffi +import _libc +import array + +libc = _libc.get() + +# struct tm *gmtime(const time_t *timep); +# struct tm *localtime(const time_t *timep); +# size_t strftime(char *s, size_t max, const char *format, +# const struct tm *tm); +gmtime_ = libc.func("P", "gmtime", "P") +localtime_ = libc.func("P", "localtime", "P") +strftime_ = libc.func("i", "strftime", "sisP") + + +def strftime(format, t=None): + if t is None: + t = time() + + t = int(t) + a = array.array('i', [t]) + tm_p = localtime_(a) + buf = bytearray(32) + l = strftime_(buf, 32, format, tm_p) + return str(buf[:l], "utf-8")