Skip to content

Commit e52fbf2

Browse files
committed
Add time.struct_time support.
1 parent 4bf7c6f commit e52fbf2

File tree

4 files changed

+122
-20
lines changed

4 files changed

+122
-20
lines changed

py/objnamedtuple.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,13 @@
2828
#include <string.h>
2929

3030
#include "py/nlr.h"
31+
#include "py/objnamedtuple.h"
3132
#include "py/objtuple.h"
3233
#include "py/runtime.h"
3334
#include "py/objstr.h"
3435

3536
#if MICROPY_PY_COLLECTIONS
3637

37-
typedef struct _mp_obj_namedtuple_type_t {
38-
mp_obj_type_t base;
39-
mp_uint_t n_fields;
40-
qstr fields[];
41-
} mp_obj_namedtuple_type_t;
42-
43-
typedef struct _mp_obj_namedtuple_t {
44-
mp_obj_tuple_t tuple;
45-
} mp_obj_namedtuple_t;
46-
4738
STATIC mp_uint_t namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name) {
4839
for (mp_uint_t i = 0; i < type->n_fields; i++) {
4940
if (type->fields[i] == name) {
@@ -53,15 +44,15 @@ STATIC mp_uint_t namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qst
5344
return -1;
5445
}
5546

56-
STATIC void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
47+
void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
5748
(void)kind;
5849
mp_obj_namedtuple_t *o = MP_OBJ_TO_PTR(o_in);
5950
mp_printf(print, "%q", o->tuple.base.type->name);
6051
const qstr *fields = ((mp_obj_namedtuple_type_t*)o->tuple.base.type)->fields;
6152
mp_obj_attrtuple_print_helper(print, fields, &o->tuple);
6253
}
6354

64-
STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
55+
void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
6556
if (dest[0] == MP_OBJ_NULL) {
6657
// load attribute
6758
mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in);
@@ -77,7 +68,7 @@ STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
7768
}
7869
}
7970

80-
STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
71+
mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
8172
const mp_obj_namedtuple_type_t *type = (const mp_obj_namedtuple_type_t*)type_in;
8273
size_t num_fields = type->n_fields;
8374
if (n_args + n_kw != num_fields) {
@@ -134,7 +125,7 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args,
134125
return MP_OBJ_FROM_PTR(tuple);
135126
}
136127

137-
STATIC const mp_rom_obj_tuple_t namedtuple_base_tuple = {{&mp_type_tuple}, 1, {MP_ROM_PTR(&mp_type_tuple)}};
128+
const mp_rom_obj_tuple_t namedtuple_base_tuple = {{&mp_type_tuple}, 1, {MP_ROM_PTR(&mp_type_tuple)}};
138129

139130
STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, mp_uint_t n_fields, mp_obj_t *fields) {
140131
mp_obj_namedtuple_type_t *o = m_new_obj_var(mp_obj_namedtuple_type_t, qstr, n_fields);

py/objnamedtuple.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
* Copyright (c) 2014 Paul Sokolovsky
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include <string.h>
29+
30+
#include "py/nlr.h"
31+
#include "py/objtuple.h"
32+
#include "py/runtime.h"
33+
#include "py/objstr.h"
34+
35+
#if MICROPY_PY_COLLECTIONS
36+
37+
typedef struct _mp_obj_namedtuple_type_t {
38+
mp_obj_type_t base;
39+
mp_uint_t n_fields;
40+
qstr fields[];
41+
} mp_obj_namedtuple_type_t;
42+
43+
typedef struct _mp_obj_namedtuple_t {
44+
mp_obj_tuple_t tuple;
45+
} mp_obj_namedtuple_t;
46+
47+
void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
48+
49+
void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
50+
51+
mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
52+
53+
const mp_rom_obj_tuple_t namedtuple_base_tuple;
54+
55+
#endif // MICROPY_PY_COLLECTIONS

shared-bindings/nativeio/DigitalInOut.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ mp_obj_property_t nativeio_digitalinout_direction_obj = {
198198

199199
//| .. attribute:: value
200200
//|
201-
//| Get or set the digital logic level of the pin.
201+
//| The digital logic level of the pin.
202202
//|
203203
STATIC mp_obj_t nativeio_digitalinout_obj_get_value(mp_obj_t self_in) {
204204
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);

shared-bindings/time/__init__.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@
2828

2929
#include <string.h>
3030

31-
//#include "py/nlr.h"
3231
#include "py/obj.h"
33-
//#include "py/gc.h"
34-
//#include "py/runtime.h"
35-
//#include "py/mphal.h"
36-
//#include "py/smallint.h"
32+
#include "py/objnamedtuple.h"
3733
#include "shared-bindings/time/__init__.h"
3834

3935
//| :mod:`time` --- time and timing related functions
@@ -76,11 +72,71 @@ STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
7672
}
7773
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
7874

75+
#if MICROPY_PY_COLLECTIONS
76+
mp_obj_t struct_time_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
77+
if (n_args != 1) {
78+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "time.struct_time() takes exactly 1 argument"));
79+
}
80+
if (!MP_OBJ_IS_TYPE(args[0], &mp_type_tuple) || ((mp_obj_tuple_t*) MP_OBJ_TO_PTR(args[0]))->len != 9) {
81+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "time.struct_time() takes a 9-sequence"));
82+
}
83+
84+
mp_obj_tuple_t* tuple = MP_OBJ_TO_PTR(args[0]);
85+
return namedtuple_make_new(type, 9, 0, tuple->items);
86+
}
87+
88+
//| .. class:: struct_time((tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst))
89+
//|
90+
//| Structure used to capture a date and time. Note that it takes a tuple!
91+
//|
92+
//| :param int tm_year: the year, 2017 for example
93+
//| :param int tm_mon: the month, range [1, 12]
94+
//| :param int tm_mday: the day of the month, range [1, 31]
95+
//| :param int tm_hour: the hour, range [0, 23]
96+
//| :param int tm_min: the minute, range [0, 59]
97+
//| :param int tm_sec: the second, range [0, 61]
98+
//| :param int tm_wday: the day of the week, range [0, 6], Monday is 0
99+
//| :param int tm_yday: the day of the year, range [1, 366], -1 indicates not known
100+
//| :param int tm_isdst: 1 when in daylight savings, 0 when not, -1 if unknown.
101+
//|
102+
mp_obj_namedtuple_type_t struct_time_type_obj = {
103+
.base = {
104+
.base = {
105+
.type = &mp_type_type
106+
},
107+
.name = MP_QSTR_struct_time,
108+
.print = namedtuple_print,
109+
.make_new = struct_time_make_new,
110+
.unary_op = mp_obj_tuple_unary_op,
111+
.binary_op = mp_obj_tuple_binary_op,
112+
.attr = namedtuple_attr,
113+
.subscr = mp_obj_tuple_subscr,
114+
.getiter = mp_obj_tuple_getiter,
115+
.bases_tuple = (mp_obj_tuple_t*)(mp_rom_obj_tuple_t*)&namedtuple_base_tuple,
116+
},
117+
.n_fields = 9,
118+
.fields = {
119+
MP_QSTR_tm_year,
120+
MP_QSTR_tm_mon,
121+
MP_QSTR_tm_mday,
122+
MP_QSTR_tm_hour,
123+
MP_QSTR_tm_min,
124+
MP_QSTR_tm_sec,
125+
MP_QSTR_tm_wday,
126+
MP_QSTR_tm_yday,
127+
MP_QSTR_tm_isdst
128+
},
129+
};
130+
#endif
131+
79132
STATIC const mp_map_elem_t time_module_globals_table[] = {
80133
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_time) },
81134

82135
{ MP_OBJ_NEW_QSTR(MP_QSTR_monotonic), (mp_obj_t)&time_monotonic_obj },
83136
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj },
137+
#if MICROPY_PY_COLLECTIONS
138+
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct_time), (mp_obj_t)&struct_time_type_obj },
139+
#endif
84140
};
85141

86142
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);

0 commit comments

Comments
 (0)