8000 unix/modtime: Replace strftime() with localtime(). · micropython/micropython@d42b80f · GitHub
[go: up one dir, main page]

Skip to content

Commit d42b80f

Browse files
author
Paul Sokolovsky
committed
unix/modtime: Replace strftime() with localtime().
Baremetal ports standardized on providing localtime(). localtime() offers more functionality, in particular, strftime() can be completely implemented in Python with localtime().
1 parent 08c73d9 commit d42b80f

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

unix/modtime.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,40 @@ STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
125125
}
126126
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_time_sleep_obj, mod_time_sleep);
127127

128-
STATIC mp_obj_t mod_time_strftime(size_t n_args, const mp_obj_t *args) {
128+
STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) {
129129
time_t t;
130-
if (n_args == 1) {
130+
if (n_args == 0) {
131131
t = time(NULL);
132132
} else {
133-
// CPython requires passing struct tm, but we allow to pass time_t
134-
// (and don't support struct tm so far).
135-
t = mp_obj_get_int(args[1]);
133+
#if MICROPY_PY_BUILTINS_FLOAT
134+
mp_float_t val = mp_obj_get_float(args[0]);
135+
t = (time_t)MICROPY_FLOAT_C_FUN(trunc)(val);
136+
#else
137+
t = mp_obj_get_int(args[0]);
138+
#endif
136139
}
137140
struct tm *tm = localtime(&t);
138-
char buf[32];
139-
size_t sz = strftime(buf, sizeof(buf), mp_obj_str_get_str(args[0]), tm);
140-
return mp_obj_new_str(buf, sz, false);
141+
142+
mp_obj_t ret = mp_obj_new_tuple(9, NULL);
143+
144+
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(ret);
145+
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(tm->tm_year + 1900);
146+
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(tm->tm_mon + 1);
147+
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(tm->tm_mday);
148+
tuple->items[3] = MP_OBJ_NEW_SMALL_INT(tm->tm_hour);
149+
tuple->items[4] = MP_OBJ_NEW_SMALL_INT(tm->tm_min);
150+
tuple->items[5] = MP_OBJ_NEW_SMALL_INT(tm->tm_sec);
151+
int wday = tm->tm_wday - 1;
152+
if (wday < 0) {
153+
wday = 6;
154+
}
155+
tuple->items[6] = MP_OBJ_NEW_SMALL_INT(wday);
156+
tuple->items[7] = MP_OBJ_NEW_SMALL_INT(tm->tm_yday + 1);
157+
tuple->items[8] = MP_OBJ_NEW_SMALL_INT(tm->tm_isdst);
158+
159+
return ret;
141160
}
142-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_time_strftime_obj, 1, 2, mod_time_strftime);
161+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_time_localtime_obj, 0, 1, mod_time_localtime);
143162

144163
STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = {
145164
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
@@ -153,7 +172,7 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = {
153172
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
154173
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
155174
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
156-
{ MP_ROM_QSTR(MP_QSTR_strftime), MP_ROM_PTR(&mod_time_strftime_obj) },
175+
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mod_time_localtime_obj) },
157176
};
158177

159178
STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table);

0 commit comments

Comments
 (0)
0