Skip to content

Commit ec1aec1

Browse files
committed
shared-bindings/time: introduce time.monotonic_ns
This is intended to be compatible with Python 3.7's time.monotonic_ns. The "actual resolution" is 1ms due to this being the unit at which common_hal_time_monotonic ticks. Closes adafruit#519
1 parent b436666 commit ec1aec1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

shared-bindings/time/__init__.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,20 @@ STATIC mp_obj_t time_time(void) {
206206
}
207207
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
208208

209+
//| .. method:: monotonic_ns(clk_id)
210+
//|
211+
//| Return the time of the specified clock clk_id in nanoseconds. Refer to
212+
//| Clock ID Constants for a list of accepted values for clk_id.
213+
//|
214+
//| :return: the current time
215+
//| :rtype: int
216+
//|
217+
STATIC mp_obj_t time_monotonic_ns(void) {
218+
uint64_t time64 = common_hal_time_monotonic() * 1000000llu;
219+
return mp_obj_new_int_from_ll((long long) time64);
220+
}
221+
MP_DEFINE_CONST_FUN_OBJ_0(time_monotonic_ns_obj, time_monotonic_ns);
222+
209223
//| .. method:: localtime([secs])
210224
//|
211225
//| Convert a time expressed in seconds since Jan 1, 1970 to a struct_time in
@@ -280,6 +294,7 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
280294
#endif // MICROPY_PY_COLLECTIONS
281295
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
282296
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
297+
{ MP_ROM_QSTR(MP_QSTR_monotonic_ns), MP_ROM_PTR(&time_monotonic_ns_obj) },
283298
#endif
284299
};
285300

0 commit comments

Comments
 (0)