|
28 | 28 |
|
29 | 29 | #include <string.h>
|
30 | 30 |
|
31 |
| -//#include "py/nlr.h" |
32 | 31 | #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" |
37 | 33 | #include "shared-bindings/time/__init__.h"
|
38 | 34 |
|
39 | 35 | //| :mod:`time` --- time and timing related functions
|
@@ -76,11 +72,71 @@ STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
|
76 | 72 | }
|
77 | 73 | MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
|
78 | 74 |
|
| 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 | + |
79 | 132 | STATIC const mp_map_elem_t time_module_globals_table[] = {
|
80 | 133 | { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_time) },
|
81 | 134 |
|
82 | 135 | { MP_OBJ_NEW_QSTR(MP_QSTR_monotonic), (mp_obj_t)&time_monotonic_obj },
|
83 | 136 | { 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 |
84 | 140 | };
|
85 | 141 |
|
86 | 142 | STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
|
|
0 commit comments