Skip to content

Commit d69aaba

Browse files
committed
time: Introduce "real" struct_time.
Implemented using namedtuple and lacks tm_zone, tm_gmtoff fields.
1 parent 2472ad4 commit d69aaba

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

time/time.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from utime import *
2+
from ucollections import namedtuple
23
import ustruct
34
import uctypes
45
import ffi
@@ -16,17 +17,19 @@
1617
strftime_ = libc.func("i", "strftime", "sisP")
1718
mktime_ = libc.func("i", "mktime", "P")
1819

20+
_struct_time = namedtuple("struct_time",
21+
["tm_year", "tm_mon", "tm_mday", "tm_hour", "tm_min", "tm_sec", "tm_wday", "tm_yday", "tm_isdst"])
1922

2023
def _tuple_to_c_tm(t):
2124
return ustruct.pack("@iiiiiiiii", t[5], t[4], t[3], t[2], t[1] - 1, t[0] - 1900, (t[6] + 1) % 7, t[7] - 1, t[8])
2225

2326

2427
def _c_tm_to_tuple(tm):
2528
t = ustruct.unpack("@iiiiiiiii", tm)
26-
return tuple([t[5] + 1900, t[4] + 1, t[3], t[2], t[1], t[0], (t[6] - 1) % 7, t[7] + 1, t[8]])
29+
return _struct_time(t[5] + 1900, t[4] + 1, t[3], t[2], t[1], t[0], (t[6] - 1) % 7, t[7] + 1, t[8])
2730

2831
def struct_time(tm):
29-
return tm
32+
return _struct_time(*tm)
3033

3134

3235
def strftime(format, t=None):

0 commit comments

Comments
 (0)