Skip to content

Commit bd7af61

Browse files
committed
ports: Add utime.gmtime() function.
To portably get the Epoch. This is simply aliased to localtime() on ports that are not timezone aware. Signed-off-by: Damien George <[email protected]>
1 parent b287580 commit bd7af61

File tree

7 files changed

+28
-9
lines changed

7 files changed

+28
-9
lines changed

docs/library/utime.rst

+10-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ behave not as expected.
3636
Functions
3737
---------
3838

39-
.. function:: localtime([secs])
39+
.. function:: gmtime([secs])
40+
localtime([secs])
4041

41-
Convert a time expressed in seconds since the Epoch (see above) into an 8-tuple which
42-
contains: (year, month, mday, hour, minute, second, weekday, yearday)
43-
If secs is not provided or None, then the current time from the RTC is used.
42+
Convert the time *secs* expressed in seconds since the Epoch (see above) into an
43+
8-tuple which contains: ``(year, month, mday, hour, minute, second, weekday, yearday)``
44+
If *secs* is not provided or None, then the current time from the RTC is used.
45+
46+
The `gmtime()` function returns a date-time tuple in UTC, and `localtime()` returns a
47+
date-time tuple in local time.
48+
49+
The format of the entries in the 8-tuple are:
4450

4551
* year includes the century (for example 2014).
4652
* month is 1-12

ports/cc3200/mods/modutime.c

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
133133
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
134134
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
135135

136+
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) },
136137
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
137138
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
138139
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },

ports/esp32/modutime.c

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
8585
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
8686
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
8787

88+
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) },
8889
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
8990
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
9091
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },

ports/esp8266/modules/ntptime.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ def time():
2929
return val - NTP_DELTA
3030

3131

32-
# There's currently no timezone support in MicroPython, so
33-
# utime.localtime() will return UTC time (as if it was .gmtime())
32+
# There's currently no timezone support in MicroPython, and the RTC is set in UTC time.
3433
def settime():
3534
t = time()
3635
import machine
3736
import utime
3837

39-
tm = utime.localtime(t)
38+
tm = utime.gmtime(t)
4039
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))

ports/esp8266/modutime.c

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
108108
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
109109
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
110110

111+
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) },
111112
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
112113
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
113114
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },

ports/stm32/modutime.c

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
132132
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
133133
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
134134

135+
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) },
135136
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
136137
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
137138
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },

ports/unix/modtime.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
132132
}
133133
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_time_sleep_obj, mod_time_sleep);
134134

135-
STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) {
135+
STATIC mp_obj_t mod_time_gm_local_time(size_t n_args, const mp_obj_t *args, struct tm *(*time_func)(const time_t *timep)) {
136136
time_t t;
137137
if (n_args == 0) {
138138
t = time(NULL);
@@ -144,7 +144,7 @@ STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) {
144144
t = mp_obj_get_int(args[0]);
145145
#endif
146146
}
147-
struct tm *tm = localtime(&t);
147+
struct tm *tm = time_func(&t);
148148

149149
mp_obj_t ret = mp_obj_new_tuple(9, NULL);
150150

@@ -165,6 +165,15 @@ STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) {
165165

166166
return ret;
167167
}
168+
169+
STATIC mp_obj_t mod_time_gmtime(size_t n_args, const mp_obj_t *args) {
170+
return mod_time_gm_local_time(n_args, args, gmtime);
171+
}
172+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_time_gmtime_obj, 0, 1, mod_time_gmtime);
173+
174+
STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) {
175+
return mod_time_gm_local_time(n_args, args, localtime);
176+
}
168177
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_time_localtime_obj, 0, 1, mod_time_localtime);
169178

170179
STATIC mp_obj_t mod_time_mktime(mp_obj_t tuple) {
@@ -210,6 +219,7 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = {
210219
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
211220
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
212221
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
222+
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mod_time_gmtime_obj) },
213223
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mod_time_localtime_obj) },
214224
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mod_time_mktime_obj) },
215225
};

0 commit comments

Comments
 (0)