1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2019 Damien P. George
7 * Copyright (c) 2020 Jim Mussared
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 "py/runtime.h"
29 #include "shared/timeutils/timeutils.h"
30 #include "extmod/utime_mphal.h"
31 #include "fsl_snvs_lp.h"
32
33 // localtime([secs])
34 // Convert a time expressed in seconds since the Epoch into an 8-tuple which
35 // contains: (year, month, mday, hour, minute, second, weekday, yearday)
36 // If secs is not provided or None, then the current time from the RTC is used.
time_localtime(size_t n_args,const mp_obj_t * args)37 STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
38 if (n_args == 0 || args[0] == mp_const_none) {
39 // Get current date and time.
40 snvs_lp_srtc_datetime_t t;
41 SNVS_LP_SRTC_GetDatetime(SNVS, &t);
42 mp_obj_t tuple[8] = {
43 mp_obj_new_int(t.year),
44 mp_obj_new_int(t.month),
45 mp_obj_new_int(t.day),
46 mp_obj_new_int(t.hour),
47 mp_obj_new_int(t.minute),
48 mp_obj_new_int(t.second),
49 mp_obj_new_int(timeutils_calc_weekday(t.year, t.month, t.day)),
50 mp_obj_new_int(timeutils_year_day(t.year, t.month, t.day)),
51 };
52 return mp_obj_new_tuple(8, tuple);
53 } else {
54 // Convert given seconds to tuple.
55 mp_int_t seconds = mp_obj_get_int(args[0]);
56 timeutils_struct_time_t tm;
57 timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
58 mp_obj_t tuple[8] = {
59 tuple[0] = mp_obj_new_int(tm.tm_year),
60 tuple[1] = mp_obj_new_int(tm.tm_mon),
61 tuple[2] = mp_obj_new_int(tm.tm_mday),
62 tuple[3] = mp_obj_new_int(tm.tm_hour),
63 tuple[4] = mp_obj_new_int(tm.tm_min),
64 tuple[5] = mp_obj_new_int(tm.tm_sec),
65 tuple[6] = mp_obj_new_int(tm.tm_wday),
66 tuple[7] = mp_obj_new_int(tm.tm_yday),
67 };
68 return mp_obj_new_tuple(8, tuple);
69 }
70 }
71 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
72
73 // mktime()
74 // This is inverse function of localtime. It's argument is a full 8-tuple
75 // which expresses a time as per localtime. It returns an integer which is
76 // the number of seconds since the Epoch.
time_mktime(mp_obj_t tuple)77 STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
78 size_t len;
79 mp_obj_t *elem;
80 mp_obj_get_array(tuple, &len, &elem);
81
82 // localtime generates a tuple of len 8. CPython uses 9, so we accept both.
83 if (len < 8 || len > 9) {
84 mp_raise_TypeError(MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9"));
85 }
86
87 return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
88 mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
89 mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
90 }
91 MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
92
93 // time()
94 // Return the number of seconds since the Epoch.
time_time(void)95 STATIC mp_obj_t time_time(void) {
96 snvs_lp_srtc_datetime_t t;
97 SNVS_LP_SRTC_GetDatetime(SNVS, &t);
98 return mp_obj_new_int_from_ull(timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.minute, t.second));
99 }
100 STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
101
102 STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
103 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
104
105 { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) },
106 { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
107 { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
108
109 { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
110 { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) },
111
112 { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
113 { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
114 { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
115 { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
116 { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
117 { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
118 { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
119 { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
120 };
121
122 STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
123
124 const mp_obj_module_t mp_module_utime = {
125 .base = { &mp_type_module },
126 .globals = (mp_obj_dict_t *)&time_module_globals,
127 };
128