Skip to content

Commit d0b2544

Browse files
committed
Add precision info to time.monotonic()
1 parent f6681ef commit d0b2544

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

shared-bindings/time/__init__.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,26 @@
3939
//| """time and timing related functions
4040
//|
4141
//| The `time` module is a strict subset of the CPython `cpython:time` module. So, code
42-
//| written in MicroPython will work in CPython but not necessarily the other
42+
//| using `time` written in CircuitPython will work in CPython but not necessarily the other
4343
//| way around."""
4444
//|
4545
//| def monotonic() -> float:
4646
//| """Returns an always increasing value of time with an unknown reference
47-
//| point. Only use it to compare against other values from `monotonic`.
47+
//| point. Only use it to compare against other values from `time.monotonic()`.
48+
//|
49+
//| On most boards, `time.monotonic()` converts a 64-bit millisecond tick counter
50+
//| to a float. Floats on most boards are encoded in 30 bits internally, with
51+
//| effectively 22 bits of precision. The float returned by `time.monotonic()` will
52+
//| accurately represent time to millisecond precision only up to 2**22 milliseconds
53+
//| (about 1.165 hours).
54+
//| At that point it will start losing precision, and its value will change only
55+
//| every second millisecond. At 2**23 milliseconds it will change every fourth
56+
//| millisecond, and so forth.
57+
//|
58+
//| If you need more consistent precision, use `time.monotonic_ns()`, or `supervisor.ticks_ms()`.
59+
//| `time.monotonic_ns()` is not available on boards without long integer support.
60+
//| `supervisor.ticks_ms()` uses intervals of a millisecond, but wraps around, and is not
61+
//| CPython-compatible.
4862
//|
4963
//| :return: the current monotonic time
5064
//| :rtype: float"""
@@ -216,7 +230,8 @@ STATIC mp_obj_t time_time(void) {
216230
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
217231

218232
//| def monotonic_ns() -> int:
219-
//| """Return the time of the monotonic clock, cannot go backward, in nanoseconds.
233+
//| """Return the time of the monotonic clock, which cannot go backward, in nanoseconds.
234+
//| Not available on boards without long integer support.
220235
//|
221236
//| :return: the current time
222237
//| :rtype: int"""

0 commit comments

Comments
 (0)