adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
MIT License
3.97k stars 1.16k forks source link

`time.time()` returns int rather than float #3125

Open Flameeyes opened 4 years ago

Flameeyes commented 4 years ago

In CPython 3.8 time.time() returns a floating point value. In CircuitPython 5.3 on Feather M4 and M0, it returns int (or rather a long integer).

time.monotonic() returns float though.

Flameeyes commented 4 years ago

It appears this is behind a #define:

STATIC mp_obj_t mod_time_time(void) {
#if MICROPY_PY_BUILTINS_FLOAT
    struct timeval tv;
    gettimeofday(&tv, NULL);
    mp_float_t val = tv.tv_sec + (mp_float_t)tv.tv_usec / 1000000;
    return mp_obj_new_float(val);
#else
    return mp_obj_new_int((mp_int_t)time(NULL));
#endif
}
dhalbert commented 4 years ago
STATIC mp_obj_t mod_time_time(void) {

This is only in ports/unix/modtime.c. The erroneous code returning an int is in shared-bindings/time/__init__.c.

We do assume all CircuitPython impls have floats (but not longints).