Skip to content

Commit de82922

Browse files
committed
unix modtime: Adhere to MICROPY_ENABLE_FLOAT better.
1 parent b7e90ea commit de82922

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

unix/modtime.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,28 @@
1111
#include "runtime.h"
1212

1313
STATIC mp_obj_t mod_time_time() {
14+
#if MICROPY_ENABLE_FLOAT
15+
struct timeval tv;
16+
gettimeofday(&tv, NULL);
17+
mp_float_t val = tv.tv_sec + (mp_float_t)tv.tv_usec / 1000000;
18+
return mp_obj_new_float(val);
19+
#else
1420
return mp_obj_new_int((machine_int_t)time(NULL));
21+
#endif
1522
}
1623
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_time_obj, mod_time_time);
1724

1825
// Note: this is deprecated since CPy3.3, but pystone still uses it.
1926
STATIC mp_obj_t mod_time_clock() {
20-
// return mp_obj_new_int((machine_int_t)clock());
21-
// POSIX requires CLOCKS_PER_SEC equals 1000000, so that's what we assume
27+
#if MICROPY_ENABLE_FLOAT
28+
// POSIX requires CLOCKS_PER_SEC equals 1000000, so that's what we assume.
2229
// float cannot represent full range of int32 precisely, so we pre-divide
2330
// int to reduce resolution, and then actually do float division hoping
2431
// to preserve integer part resolution.
2532
return mp_obj_new_float((float)(clock() / 1000) / 1000.0);
33+
#else
34+
return mp_obj_new_int((machine_int_t)clock());
35+
#endif
2636
}
2737
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_clock_obj, mod_time_clock);
2838

0 commit comments

Comments
 (0)