|
| 1 | +Index: micropython/extmod/modtime.c |
| 2 | +=================================================================== |
| 3 | +--- micropython.orig/extmod/modtime.c |
| 4 | ++++ micropython/extmod/modtime.c |
| 5 | +@@ -40,7 +40,8 @@ |
| 6 | + |
| 7 | + #include "shared/timeutils/timeutils.h" |
| 8 | + |
| 9 | +-// localtime([secs]) |
| 10 | ++// gmtime([secs]) |
| 11 | ++// The gmtime() function returns a date-time tuple in UTC. |
| 12 | + // Convert a time expressed in seconds since the Epoch into an 8-tuple which |
| 13 | + // contains: (year, month, mday, hour, minute, second, weekday, yearday) |
| 14 | + // If secs is not provided or None, then the current time is used. |
| 15 | +@@ -52,10 +53,10 @@ |
| 16 | + // - second is 0-59 |
| 17 | + // - weekday is 0-6 for Mon-Sun |
| 18 | + // - yearday is 1-366 |
| 19 | +-static mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { |
| 20 | ++static mp_obj_t time_gmtime(size_t n_args, const mp_obj_t *args) { |
| 21 | + if (n_args == 0 || args[0] == mp_const_none) { |
| 22 | + // Get current date and time. |
| 23 | +- return mp_time_localtime_get(); |
| 24 | ++ return mp_time_gmtime_get(); |
| 25 | + } else { |
| 26 | + // Convert given seconds to tuple. |
| 27 | + mp_int_t seconds = mp_obj_get_int(args[0]); |
| 28 | +@@ -74,6 +75,30 @@ static mp_obj_t time_localtime(size_t n_ |
| 29 | + return mp_obj_new_tuple(8, tuple); |
| 30 | + } |
| 31 | + } |
| 32 | ++MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_time_gmtime_obj, 0, 1, time_gmtime); |
| 33 | ++ |
| 34 | ++// localtime([secs]) |
| 35 | ++// Convert a time expressed in seconds since the Epoch into an 8-tuple which |
| 36 | ++// contains: (year, month, mday, hour, minute, second, weekday, yearday) |
| 37 | ++// If secs is not provided or None, then the current time is used. |
| 38 | ++// - year is the full year, eg 2000 |
| 39 | ++// - month is 1-12 |
| 40 | ++// - mday is 1-31 |
| 41 | ++// - hour is 0-23 |
| 42 | ++// - minute is 0-59 |
| 43 | ++// - second is 0-59 |
| 44 | ++// - weekday is 0-6 for Mon-Sun |
| 45 | ++// - yearday is 1-366 |
| 46 | ++static mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { |
| 47 | ++ if (n_args == 0 || args[0] == mp_const_none) { |
| 48 | ++ // Get current date and time. |
| 49 | ++ return mp_time_localtime_get(); |
| 50 | ++ } else { |
| 51 | ++ // Convert given seconds to tuple. |
| 52 | ++ mp_int_t seconds = mp_obj_get_int(args[0]); |
| 53 | ++ return mp_time_localtime_convert(seconds); |
| 54 | ++ } |
| 55 | ++} |
| 56 | + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_time_localtime_obj, 0, 1, time_localtime); |
| 57 | + |
| 58 | + // mktime() |
| 59 | +@@ -200,7 +225,7 @@ static const mp_rom_map_elem_t mp_module |
| 60 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) }, |
| 61 | + |
| 62 | + #if MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME |
| 63 | +- { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mp_time_localtime_obj) }, |
| 64 | ++ { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mp_time_gmtime_obj) }, |
| 65 | + { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mp_time_localtime_obj) }, |
| 66 | + { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mp_time_mktime_obj) }, |
| 67 | + #endif |
| 68 | +Index: micropython/ports/esp32/modtime.c |
| 69 | +=================================================================== |
| 70 | +--- micropython.orig/ports/esp32/modtime.c |
| 71 | ++++ micropython/ports/esp32/modtime.c |
| 72 | +@@ -30,9 +30,48 @@ |
| 73 | + |
| 74 | + #include "py/obj.h" |
| 75 | + #include "shared/timeutils/timeutils.h" |
| 76 | ++#include "uiflow_utility.h" |
| 77 | ++ |
| 78 | ++static mp_obj_t mp_time_localtime_convert(time_t sec) { |
| 79 | ++ struct tm timeinfo; |
| 80 | ++ localtime_r(&sec, &timeinfo); |
| 81 | ++ mp_obj_t tuple[8] = { |
| 82 | ++ tuple[0] = mp_obj_new_int(timeinfo.tm_year + 1900), |
| 83 | ++ tuple[1] = mp_obj_new_int(timeinfo.tm_mon + 1), |
| 84 | ++ tuple[2] = mp_obj_new_int(timeinfo.tm_mday), |
| 85 | ++ tuple[3] = mp_obj_new_int(timeinfo.tm_hour), |
| 86 | ++ tuple[4] = mp_obj_new_int(timeinfo.tm_min), |
| 87 | ++ tuple[5] = mp_obj_new_int(timeinfo.tm_sec), |
| 88 | ++ tuple[6] = mp_obj_new_int(timeinfo.tm_wday), |
| 89 | ++ tuple[7] = mp_obj_new_int(timeinfo.tm_yday), |
| 90 | ++ }; |
| 91 | ++ return mp_obj_new_tuple(8, tuple); |
| 92 | ++} |
| 93 | + |
| 94 | + // Return the localtime as an 8-tuple. |
| 95 | + static mp_obj_t mp_time_localtime_get(void) { |
| 96 | ++ timeutils_struct_time_t tm; |
| 97 | ++ |
| 98 | ++ time_t now; |
| 99 | ++ struct tm timeinfo; |
| 100 | ++ time(&now); |
| 101 | ++ localtime_r(&now, &timeinfo); |
| 102 | ++ |
| 103 | ++ mp_obj_t tuple[8] = { |
| 104 | ++ tuple[0] = mp_obj_new_int(timeinfo.tm_year + 1900), |
| 105 | ++ tuple[1] = mp_obj_new_int(timeinfo.tm_mon + 1), |
| 106 | ++ tuple[2] = mp_obj_new_int(timeinfo.tm_mday), |
| 107 | ++ tuple[3] = mp_obj_new_int(timeinfo.tm_hour), |
| 108 | ++ tuple[4] = mp_obj_new_int(timeinfo.tm_min), |
| 109 | ++ tuple[5] = mp_obj_new_int(timeinfo.tm_sec), |
| 110 | ++ tuple[6] = mp_obj_new_int(timeinfo.tm_wday), |
| 111 | ++ tuple[7] = mp_obj_new_int(timeinfo.tm_yday), |
| 112 | ++ }; |
| 113 | ++ return mp_obj_new_tuple(8, tuple); |
| 114 | ++} |
| 115 | ++ |
| 116 | ++// Return the gmtime as an 8-tuple. |
| 117 | ++static mp_obj_t mp_time_gmtime_get(void) { |
| 118 | + struct timeval tv; |
| 119 | + gettimeofday(&tv, NULL); |
| 120 | + timeutils_struct_time_t tm; |
| 121 | +@@ -56,3 +95,36 @@ static mp_obj_t mp_time_time_get(void) { |
| 122 | + gettimeofday(&tv, NULL); |
| 123 | + return mp_obj_new_int(tv.tv_sec); |
| 124 | + } |
| 125 | ++ |
| 126 | ++static mp_obj_t time_timezone(size_t n_args, const mp_obj_t *args) { |
| 127 | ++ if (n_args == 0 || args[0] == mp_const_none) { |
| 128 | ++ char *tz = getenv("TZ"); |
| 129 | ++ if (tz == NULL) { |
| 130 | ++ return mp_const_none; |
| 131 | ++ } else { |
| 132 | ++ return mp_obj_new_str(tz, strlen(tz)); |
| 133 | ++ } |
| 134 | ++ } else { |
| 135 | ++ char tz[64]; |
| 136 | ++ snprintf(tz, sizeof(tz), "%s", mp_obj_str_get_str(args[0])); |
| 137 | ++ setenv("TZ", tz, 1); |
| 138 | ++ tzset(); |
| 139 | ++ |
| 140 | ++ time_t now; |
| 141 | ++ char strftime_buf[64]; |
| 142 | ++ struct tm timeinfo; |
| 143 | ++ |
| 144 | ++ time(&now); |
| 145 | ++ |
| 146 | ++ localtime_r(&now, &timeinfo); |
| 147 | ++ strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); |
| 148 | ++ ESP_LOGE("time", "The current date/time in Shanghai is: %s", strftime_buf); |
| 149 | ++ |
| 150 | ++ nvs_write_str_helper(UIFLOW_NVS_NAMESPACE, "tz", tz); |
| 151 | ++ return mp_const_none; |
| 152 | ++ } |
| 153 | ++} |
| 154 | ++static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_timezone_obj, 0, 1, time_timezone); |
| 155 | ++ |
| 156 | ++#define MICROPY_PY_TIME_EXTRA_GLOBALS \ |
| 157 | ++ { MP_ROM_QSTR(MP_QSTR_timezone), MP_ROM_PTR(&time_timezone_obj) }, |
| 158 | +Index: micropython/ports/esp32/mpconfigport.h |
| 159 | +=================================================================== |
| 160 | +--- micropython.orig/ports/esp32/mpconfigport.h |
| 161 | ++++ micropython/ports/esp32/mpconfigport.h |
| 162 | +@@ -78,6 +78,7 @@ |
| 163 | + #define MICROPY_PY_IO_BUFFEREDWRITER (1) |
| 164 | + #define MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME (1) |
| 165 | + #define MICROPY_PY_TIME_TIME_TIME_NS (1) |
| 166 | ++#define MICROPY_EPOCH_IS_1970 (1) |
| 167 | + #define MICROPY_PY_TIME_INCLUDEFILE "ports/esp32/modtime.c" |
| 168 | + #define MICROPY_PY_THREAD (1) |
| 169 | + #define MICROPY_PY_THREAD_GIL (1) |
0 commit comments