8000 time: Initial cut at implementing strftime() (via ffi). · micropython/micropython-lib@8c7a419 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c7a419

Browse files
author
Paul Sokolovsky
committed
time: Initial cut at implementing strftime() (via ffi).
1 parent e413ba6 commit 8c7a419

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

time/example_strftime.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import time
2+
3+
print(time.strftime("%Y-%m-%d %H:%M:%S"))

time/metadata.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
srctype = dummy
1+
srctype = micropython-lib
22
type = module
3-
version = 0.0.1
3+
version = 0.1

time/setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77

88
setup(name='micropython-time',
9-
version='0.0.1',
10-
description='Dummy time module for MicroPython',
11-
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.',
9+
version='0.1',
10+
description='time module for MicroPython',
11+
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.",
1212
url='https://github.com/micropython/micropython/issues/405',
1313
author='MicroPython Developers',
1414
author_email='micro-python@googlegroups.com',

time/time.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
from utime import *
2+
import ffi
3+
import _libc
4+
import array
5+
6+
libc = _libc.get()
7+
8+
# struct tm *gmtime(const time_t *timep);
9+
# struct tm *localtime(const time_t *timep);
10+
# size_t strftime(char *s, size_t max, const char *format,
11+
# const struct tm *tm);
12+
gmtime_ = libc.func("P", "gmtime", "P")
13+
localtime_ = libc.func("P", "localtime", "P")
14+
strftime_ = libc.func("i", "strftime", "sisP")
15+
16+
17+
def strftime(format, t=None):
18+
if t is None:
19+
t = time()
20+
21+
t = int(t)
22+
a = array.array('i', [t])
23+
tm_p = localtime_(a)
24+
buf = bytearray(32)
25+
l = strftime_(buf, 32, format, tm_p)
26+
return str(buf[:l], "utf-8")

0 commit comments

Comments
 (0)
0