Browse Source

unix/modtime: Add strftime() function (only single argument is supported).

Following "don't rely on FFI for basic functionality" approach.
pull/1714/merge
Paul Sokolovsky 9 years ago
parent
commit
8fb255dc34
  1. 11
      unix/modtime.c
  2. 1
      unix/qstrdefsport.h

11
unix/modtime.c

@ -154,6 +154,16 @@ STATIC mp_obj_t mod_time_sleep_us(mp_obj_t arg) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_time_sleep_us_obj, mod_time_sleep_us);
STATIC mp_obj_t mod_time_strftime(mp_uint_t n_args, const mp_obj_t *args) {
assert(n_args == 1);
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char buf[32];
size_t sz = strftime(buf, sizeof(buf), mp_obj_str_get_str(args[0]), tm);
return mp_obj_new_str(buf, sz, false);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_time_strftime_obj, 1, 2, mod_time_strftime);
STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
{ MP_ROM_QSTR(MP_QSTR_clock), MP_ROM_PTR(&mod_time_clock_obj) },
@ -164,6 +174,7 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mod_time_ticks_ms_obj) },
{ MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mod_time_ticks_us_obj) },
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mod_time_ticks_diff_obj) },
{ MP_ROM_QSTR(MP_QSTR_strftime), MP_ROM_PTR(&mod_time_strftime_obj) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table);

1
unix/qstrdefsport.h

@ -77,6 +77,7 @@ Q(sleep_us)
Q(ticks_ms)
Q(ticks_us)
Q(ticks_diff)
Q(strftime)
Q(socket)
Q(sockaddr)

Loading…
Cancel
Save