|
| 1 | +diff --git a/extmod/modtime.c b/extmod/modtime.c |
| 2 | +index 805c2621c..66cd88fc1 100644 |
| 3 | +--- a/extmod/modtime.c |
| 4 | ++++ b/extmod/modtime.c |
| 5 | +@@ -29,6 +29,8 @@ |
| 6 | + #include "py/runtime.h" |
| 7 | + #include "py/smallint.h" |
| 8 | + #include "extmod/modtime.h" |
| 9 | ++#include "string.h" |
| 10 | ++#include "uiflow_utility.h" |
| 11 | + |
| 12 | + #if MICROPY_PY_TIME |
| 13 | + |
| 14 | +@@ -76,6 +78,42 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { |
| 15 | + } |
| 16 | + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_time_localtime_obj, 0, 1, time_localtime); |
| 17 | + |
| 18 | ++// localtime([secs]) |
| 19 | ++// Convert a time expressed in seconds since the Epoch into an 8-tuple which |
| 20 | ++// contains: (year, month, mday, hour, minute, second, weekday, yearday) |
| 21 | ++// If secs is not provided or None, then the current time is used. |
| 22 | ++// - year is the full year, eg 2000 |
| 23 | ++// - month is 1-12 |
| 24 | ++// - mday is 1-31 |
| 25 | ++// - hour is 0-23 |
| 26 | ++// - minute is 0-59 |
| 27 | ++// - second is 0-59 |
| 28 | ++// - weekday is 0-6 for Mon-Sun |
| 29 | ++// - yearday is 1-366 |
| 30 | ++STATIC mp_obj_t time_gmtime(size_t n_args, const mp_obj_t *args) { |
| 31 | ++ if (n_args == 0 || args[0] == mp_const_none) { |
| 32 | ++ // Get current date and time. |
| 33 | ++ return mp_time_gmtime_get(); |
| 34 | ++ } else { |
| 35 | ++ // Convert given seconds to tuple. |
| 36 | ++ mp_int_t seconds = mp_obj_get_int(args[0]); |
| 37 | ++ timeutils_struct_time_t tm; |
| 38 | ++ timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); |
| 39 | ++ mp_obj_t tuple[8] = { |
| 40 | ++ tuple[0] = mp_obj_new_int(tm.tm_year), |
| 41 | ++ tuple[1] = mp_obj_new_int(tm.tm_mon), |
| 42 | ++ tuple[2] = mp_obj_new_int(tm.tm_mday), |
| 43 | ++ tuple[3] = mp_obj_new_int(tm.tm_hour), |
| 44 | ++ tuple[4] = mp_obj_new_int(tm.tm_min), |
| 45 | ++ tuple[5] = mp_obj_new_int(tm.tm_sec), |
| 46 | ++ tuple[6] = mp_obj_new_int(tm.tm_wday), |
| 47 | ++ tuple[7] = mp_obj_new_int(tm.tm_yday), |
| 48 | ++ }; |
| 49 | ++ return mp_obj_new_tuple(8, tuple); |
| 50 | ++ } |
| 51 | ++} |
| 52 | ++MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_time_gmtime_obj, 0, 1, time_gmtime); |
| 53 | ++ |
| 54 | + // mktime() |
| 55 | + // This is the inverse function of localtime. Its argument is a full 8-tuple |
| 56 | + // which expresses a time as per localtime. It returns an integer which is |
| 57 | +@@ -116,6 +154,26 @@ MP_DEFINE_CONST_FUN_OBJ_0(mp_time_time_ns_obj, time_time_ns); |
| 58 | + |
| 59 | + #endif // MICROPY_PY_TIME_TIME_TIME_NS |
| 60 | + |
| 61 | ++STATIC mp_obj_t time_timezone(size_t n_args, const mp_obj_t *args) { |
| 62 | ++ if (n_args == 0 || args[0] == mp_const_none) { |
| 63 | ++ char *tz = getenv("TZ"); |
| 64 | ++ if (tz == NULL) { |
| 65 | ++ return mp_const_none; |
| 66 | ++ } else { |
| 67 | ++ return mp_obj_new_str(tz, strlen(tz)); |
| 68 | ++ } |
| 69 | ++ } else { |
| 70 | ++ char tz[64]; |
| 71 | ++ snprintf(tz, sizeof(tz), "%s", mp_obj_str_get_str(args[0])); |
| 72 | ++ setenv("TZ", tz, 1); |
| 73 | ++ tzset(); |
| 74 | ++ |
| 75 | ++ nvs_write_str_helper(UIFLOW_NVS_NAMESPACE, "tz", tz); |
| 76 | ++ return mp_const_none; |
| 77 | ++ } |
| 78 | ++} |
| 79 | ++STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_timezone_obj, 0, 1, time_timezone); |
| 80 | ++ |
| 81 | + STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { |
| 82 | + #ifdef MICROPY_PY_TIME_CUSTOM_SLEEP |
| 83 | + mp_time_sleep(seconds_o); |
| 84 | +@@ -200,7 +258,7 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { |
| 85 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) }, |
| 86 | + |
| 87 | + #if MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME |
| 88 | +- { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mp_time_localtime_obj) }, |
| 89 | ++ { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mp_time_gmtime_obj) }, |
| 90 | + { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mp_time_localtime_obj) }, |
| 91 | + { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mp_time_mktime_obj) }, |
| 92 | + #endif |
| 93 | +@@ -210,6 +268,7 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { |
| 94 | + { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_time_time_ns_obj) }, |
| 95 | + #endif |
| 96 | + |
| 97 | ++ { MP_ROM_QSTR(MP_QSTR_timezone), MP_ROM_PTR(&time_timezone_obj) }, |
| 98 | + { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_time_sleep_obj) }, |
| 99 | + { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_time_sleep_ms_obj) }, |
| 100 | + { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_time_sleep_us_obj) }, |
| 101 | +diff --git a/ports/esp32/modtime.c b/ports/esp32/modtime.c |
| 102 | +index 7a2b21508..e361a3d64 100644 |
| 103 | +--- a/ports/esp32/modtime.c |
| 104 | ++++ b/ports/esp32/modtime.c |
| 105 | +@@ -31,8 +31,8 @@ |
| 106 | + #include "py/obj.h" |
| 107 | + #include "shared/timeutils/timeutils.h" |
| 108 | + |
| 109 | +-// Return the localtime as an 8-tuple. |
| 110 | +-STATIC mp_obj_t mp_time_localtime_get(void) { |
| 111 | ++// Return the gmtime as an 8-tuple. |
| 112 | ++STATIC mp_obj_t mp_time_gmtime_get(void) { |
| 113 | + struct timeval tv; |
| 114 | + gettimeofday(&tv, NULL); |
| 115 | + timeutils_struct_time_t tm; |
| 116 | +@@ -50,6 +50,27 @@ STATIC mp_obj_t mp_time_localtime_get(void) { |
| 117 | + return mp_obj_new_tuple(8, tuple); |
| 118 | + } |
| 119 | + |
| 120 | ++ |
| 121 | ++// Return the localtime as an 8-tuple. |
| 122 | ++STATIC mp_obj_t mp_time_localtime_get(void) { |
| 123 | ++ time_t rawtime; |
| 124 | ++ struct tm *tm; |
| 125 | ++ time( &rawtime ); |
| 126 | ++ tm = localtime( &rawtime ); |
| 127 | ++ |
| 128 | ++ mp_obj_t tuple[8] = { |
| 129 | ++ tuple[0] = mp_obj_new_int(tm->tm_year + 1900) , |
| 130 | ++ tuple[1] = mp_obj_new_int(tm->tm_mon + 1), |
| 131 | ++ tuple[2] = mp_obj_new_int(tm->tm_mday), |
| 132 | ++ tuple[3] = mp_obj_new_int(tm->tm_hour), |
| 133 | ++ tuple[4] = mp_obj_new_int(tm->tm_min), |
| 134 | ++ tuple[5] = mp_obj_new_int(tm->tm_sec), |
| 135 | ++ tuple[6] = mp_obj_new_int(tm->tm_wday), |
| 136 | ++ tuple[7] = mp_obj_new_int(tm->tm_yday + 1), |
| 137 | ++ }; |
| 138 | ++ return mp_obj_new_tuple(8, tuple); |
| 139 | ++} |
| 140 | ++ |
| 141 | + // Return the number of seconds since the Epoch. |
| 142 | + STATIC mp_obj_t mp_time_time_get(void) { |
| 143 | + struct timeval tv; |
| 144 | +diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h |
| 145 | +index 5dc4a9c75..31cd46f10 100644 |
| 146 | +--- a/ports/esp32/mpconfigport.h |
| 147 | ++++ b/ports/esp32/mpconfigport.h |
| 148 | +@@ -88,6 +88,7 @@ |
| 149 | + #define MICROPY_GC_SPLIT_HEAP_AUTO (1) |
| 150 | + |
| 151 | + // extended modules |
| 152 | ++#define MICROPY_EPOCH_IS_1970 (1) |
| 153 | + #ifndef MICROPY_PY_ESPNOW |
| 154 | + #define MICROPY_PY_ESPNOW (1) |
| 155 | + #endif |
0 commit comments