File: time_time_ns.py

package info (click to toggle)
micropython 1.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 48,944 kB
  • sloc: ansic: 317,850; python: 59,539; xml: 4,241; makefile: 3,530; sh: 1,421; javascript: 744; asm: 681; cpp: 45; exp: 11; pascal: 6
file content (31 lines) | stat: -rw-r--r-- 720 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# test time.time_ns()

try:
    import time

    time.sleep_us
    time.time_ns
except (ImportError, AttributeError):
    print("SKIP")
    raise SystemExit


t0 = time.time_ns()
time.sleep_us(5000)
t1 = time.time_ns()

# Check that time_ns increases.
print(t0 < t1)

# Check that time_ns counts correctly, but be very lenient with the bounds (2ms to 50ms).
if 2000000 < t1 - t0 < 50000000:
    print(True)
else:
    print(t0, t1, t1 - t0)

# Check that time.time() and time.time_ns() are within a second of each other.
# Note that time.time() may return an int or float.
for _ in range(10):
    t_s, t_ns = time.time(), time.time_ns()
    print(abs(t_s * 1_000 - t_ns // 1_000_000) <= 1_000)
    time.sleep_us(100_000)