|
25 | 25 | * THE SOFTWARE.
|
26 | 26 | */
|
27 | 27 |
|
| 28 | +#include "py/runtime.h" |
| 29 | +#include "lib/timeutils/timeutils.h" |
28 | 30 | #include "extmod/utime_mphal.h"
|
| 31 | +#include "fsl_snvs_lp.h" |
| 32 | + |
| 33 | +extern int calc_weekday(int y, int m, int d); |
| 34 | + |
| 35 | +// localtime([secs]) |
| 36 | +// Convert a time expressed in seconds since the Epoch into an 8-tuple which |
| 37 | +// contains: (year, month, mday, hour, minute, second, weekday, yearday) |
| 38 | +// If secs is not provided or None, then the current time from the RTC is used. |
| 39 | +STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { |
| 40 | + if (n_args == 0 || args[0] == mp_const_none) { |
| 41 | + // Get current date and time. |
| 42 | + snvs_lp_srtc_datetime_t t; |
| 43 | + SNVS_LP_SRTC_GetDatetime(SNVS, &t); |
| 44 | + mp_obj_t tuple[8] = { |
| 45 | + mp_obj_new_int(t.year), |
| 46 | + mp_obj_new_int(t.month), |
| 47 | + mp_obj_new_int(t.day), |
| 48 | + mp_obj_new_int(t.hour), |
| 49 | + mp_obj_new_int(t.minute), |
| 50 | + mp_obj_new_int(t.second), |
| 51 | + mp_obj_new_int(calc_weekday(t.year, t.month, t.day)), |
| 52 | + mp_obj_new_int(timeutils_year_day(t.year, t.month, t.day)), |
| 53 | + }; |
| 54 | + return mp_obj_new_tuple(8, tuple); |
| 55 | + } else { |
| 56 | + // Convert given seconds to tuple. |
| 57 | + mp_int_t seconds = mp_obj_get_int(args[0]); |
| 58 | + timeutils_struct_time_t tm; |
| 59 | + timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); |
| 60 | + mp_obj_t tuple[8] = { |
| 61 | + tuple[0] = mp_obj_new_int(tm.tm_year), |
| 62 | + tuple[1] = mp_obj_new_int(tm.tm_mon), |
| 63 | + tuple[2] = mp_obj_new_int(tm.tm_mday), |
| 64 | + tuple[3] = mp_obj_new_int(tm.tm_hour), |
| 65 | + tuple[4] = mp_obj_new_int(tm.tm_min), |
| 66 | + tuple[5] = mp_obj_new_int(tm.tm_sec), |
| 67 | + tuple[6] = mp_obj_new_int(tm.tm_wday), |
| 68 | + tuple[7] = mp_obj_new_int(tm.tm_yday), |
| 69 | + }; |
| 70 | + return mp_obj_new_tuple(8, tuple); |
| 71 | + } |
| 72 | +} |
| 73 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime); |
| 74 | + |
| 75 | +// mktime() |
| 76 | +// This is inverse function of localtime. It's argument is a full 8-tuple |
| 77 | +// which expresses a time as per localtime. It returns an integer which is |
| 78 | +// the number of seconds since the Epoch. |
| 79 | +STATIC mp_obj_t time_mktime(mp_obj_t tuple) { |
| 80 | + size_t len; |
| 81 | + mp_obj_t *elem; |
| 82 | + mp_obj_get_array(tuple, &len, &elem); |
| 83 | + |
| 84 | + // localtime generates a tuple of len 8. CPython uses 9, so we accept both. |
| 85 | + if (len < 8 || len > 9) { |
| 86 | + mp_raise_TypeError(MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9")); |
| 87 | + } |
| 88 | + |
| 89 | + return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]), |
| 90 | + mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]), |
| 91 | + mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5]))); |
| 92 | +} |
| 93 | +MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); |
| 94 | + |
| 95 | +// time() |
| 96 | +// Return the number of seconds since the Epoch. |
| 97 | +STATIC mp_obj_t time_time(void) { |
| 98 | + snvs_lp_srtc_datetime_t t; |
| 99 | + SNVS_LP_SRTC_GetDatetime(SNVS, &t); |
| 100 | + return mp_obj_new_int_from_ull(timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.minute, t.second)); |
| 101 | +} |
| 102 | +STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); |
29 | 103 |
|
30 | 104 | STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
|
31 | 105 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
|
32 | 106 |
|
| 107 | + { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) }, |
| 108 | + { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, |
| 109 | + { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, |
| 110 | + |
| 111 | + { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, |
| 112 | + { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) }, |
| 113 | + |
33 | 114 | { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
|
34 | 115 | { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
|
35 | 116 | { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
|
|
0 commit comments