From d69aaba546576f053c5845809e5ba731b9e7e6df Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 9 Sep 2017 14:01:00 +0300 Subject: [PATCH] time: Introduce "real" struct_time. Implemented using namedtuple and lacks tm_zone, tm_gmtoff fields. --- time/time.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/time/time.py b/time/time.py index 9dc8f10e..f46a0764 100644 --- a/time/time.py +++ b/time/time.py @@ -1,4 +1,5 @@ from utime import * +from ucollections import namedtuple import ustruct import uctypes import ffi @@ -16,6 +17,8 @@ localtime_ = libc.func("P", "localtime", "P") strftime_ = libc.func("i", "strftime", "sisP") mktime_ = libc.func("i", "mktime", "P") +_struct_time = namedtuple("struct_time", + ["tm_year", "tm_mon", "tm_mday", "tm_hour", "tm_min", "tm_sec", "tm_wday", "tm_yday", "tm_isdst"]) def _tuple_to_c_tm(t): 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]) @@ -23,10 +26,10 @@ def _tuple_to_c_tm(t): def _c_tm_to_tuple(tm): t = ustruct.unpack("@iiiiiiiii", tm) - 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]]) + 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]) def struct_time(tm): - return tm + return _struct_time(*tm) def strftime(format, t=None):