Skip to content

Commit 4fda9ac

Browse files
github-actions[bot]Synaptics GitLab CI
andauthored
Documentation version 0.7.0 (#8)
Co-authored-by: Synaptics GitLab CI <[email protected]>
1 parent b303c42 commit 4fda9ac

File tree

2 files changed

+140
-128
lines changed

2 files changed

+140
-128
lines changed

docs/library/time.rst

Lines changed: 139 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
=====================================
33

44
.. module:: time
5-
:synopsis: time related functions
5+
:synopsis: time related functions
66

77
The ``time`` module provides functions for getting the current time and date,
88
measuring time intervals, and for delays.
@@ -19,99 +19,99 @@ Functions
1919
.. function:: gmtime([secs])
2020
localtime([secs])
2121

22-
Convert the time *secs* expressed in seconds since the Epoch (see above) into an
23-
8-tuple which contains: ``(year, month, mday, hour, minute, second, weekday, yearday)``
24-
If *secs* is not provided or None, then the current time from the RTC is used.
22+
Convert the time *secs* expressed in seconds since the Epoch (see above) into an
23+
8-tuple which contains: ``(year, month, mday, hour, minute, second, weekday, yearday)``
24+
If *secs* is not provided or None, then the current time from the RTC is used.
2525

26-
The `gmtime()` function returns a date-time tuple in UTC, and `localtime()` returns a
27-
date-time tuple in local time.
26+
The `gmtime()` function returns a date-time tuple in UTC, and `localtime()` returns a
27+
date-time tuple in local time.
2828

29-
The format of the entries in the 8-tuple are:
29+
The format of the entries in the 8-tuple are:
3030

31-
* year includes the century (for example 2014).
32-
* month is 1-12
33-
* mday is 1-31
34-
* hour is 0-23
35-
* minute is 0-59
36-
* second is 0-59
37-
* weekday is 0-6 for Mon-Sun
38-
* yearday is 1-366
31+
* year includes the century (for example 2014).
32+
* month is 1-12
33+
* mday is 1-31
34+
* hour is 0-23
35+
* minute is 0-59
36+
* second is 0-59
37+
* weekday is 0-6 for Mon-Sun
38+
* yearday is 1-366
3939

4040
.. function:: mktime()
4141

42-
This is inverse function of localtime. It's argument is a full 8-tuple
43-
which expresses a time as per localtime. It returns an integer which is
44-
the number of seconds since Jan 1, 2000.
42+
This is inverse function of localtime. It's argument is a full 8-tuple
43+
which expresses a time as per localtime. It returns an integer which is
44+
the number of seconds since Jan 1, 2000.
4545

4646
.. function:: sleep(seconds)
4747

48-
Sleep for the given number of *seconds*. *seconds* may be a floating-point
49-
number; this will result in the DL-7450 sleeping for a fractional number of
50-
seconds.
48+
Sleep for the given number of *seconds*. *seconds* may be a floating-point
49+
number; this will result in the DL-7450 sleeping for a fractional number of
50+
seconds.
5151

5252
.. function:: sleep_ms(ms)
5353

54-
Delay for given number of milliseconds, should be positive or 0.
54+
Delay for given number of milliseconds, should be positive or 0.
5555

56-
This function will delay for at least the given number of milliseconds, but
57-
may take longer than that if other processing must take place, for example
58-
interrupt handlers or other threads. Passing in 0 for *ms* will still allow
59-
other processing to occur. Use `sleep_us()` for more precise delays.
56+
This function will delay for at least the given number of milliseconds, but
57+
may take longer than that if other processing must take place, for example
58+
interrupt handlers or other threads. Passing in 0 for *ms* will still allow
59+
other processing to occur. Use `sleep_us()` for more precise delays.
6060

6161
.. function:: sleep_us(us)
6262

63-
Delay for given number of microseconds, should be positive or 0.
63+
Delay for given number of microseconds, should be positive or 0.
6464

65-
This function attempts to provide an accurate delay of at least *us*
66-
microseconds, but it may take longer if the system has other higher priority
67-
processing to perform.
65+
This function attempts to provide an accurate delay of at least *us*
66+
microseconds, but it may take longer if the system has other higher priority
67+
processing to perform.
6868

6969
.. function:: ticks_ms()
7070

71-
Returns an increasing millisecond counter with an arbitrary reference point, that
72-
wraps around after some value.
73-
74-
The wrap-around value is not explicitly exposed, but we will
75-
refer to it as *TICKS_MAX* to simplify discussion. Period of the values is
76-
*TICKS_PERIOD = TICKS_MAX + 1*. *TICKS_PERIOD* is guaranteed to be a power of
77-
two, but otherwise may differ from port to port. The same period value is used
78-
for all of `ticks_ms()`, `ticks_us()`, `ticks_cpu()` functions (for
79-
simplicity). Thus, these functions will return a value in range [*0* ..
80-
*TICKS_MAX*], inclusive, total *TICKS_PERIOD* values. Note that only
81-
non-negative values are used. For the most part, you should treat values returned
82-
by these functions as opaque. The only operations available for them are
83-
`ticks_diff()` and `ticks_add()` functions described below.
84-
85-
Note: Performing standard mathematical operations (+, -) or relational
86-
operators (<, <=, >, >=) directly on these value will lead to invalid
87-
result. Performing mathematical operations and then passing their results
88-
as arguments to `ticks_diff()` or `ticks_add()` will also lead to
89-
invalid results from the latter functions.
71+
Returns an increasing millisecond counter with an arbitrary reference point, that
72+
wraps around after some value.
73+
74+
The wrap-around value is not explicitly exposed, but we will
75+
refer to it as *TICKS_MAX* to simplify discussion. Period of the values is
76+
*TICKS_PERIOD = TICKS_MAX + 1*. *TICKS_PERIOD* is guaranteed to be a power of
77+
two, but otherwise may differ from port to port. The same period value is used
78+
for all of `ticks_ms()`, `ticks_us()`, `ticks_cpu()` functions (for
79+
simplicity). Thus, these functions will return a value in range [*0* ..
80+
*TICKS_MAX*], inclusive, total *TICKS_PERIOD* values. Note that only
81+
non-negative values are used. For the most part, you should treat values returned
82+
by these functions as opaque. The only operations available for them are
83+
`ticks_diff()` and `ticks_add()` functions described below.
84+
85+
Note: Performing standard mathematical operations (+, -) or relational
86+
operators (<, <=, >, >=) directly on these value will lead to invalid
87+
result. Performing mathematical operations and then passing their results
88+
as arguments to `ticks_diff()` or `ticks_add()` will also lead to
89+
invalid results from the latter functions.
9090

9191
.. function:: ticks_us()
9292

93-
Just like `ticks_ms()` above, but in microseconds.
93+
Just like `ticks_ms()` above, but in microseconds.
9494

9595
.. function:: ticks_cpu()
9696

97-
Similar to `ticks_ms()` and `ticks_us()`, but with the highest possible resolution
98-
in the system. This function is intended for very fine benchmarking or very tight
99-
real-time loops.
97+
Similar to `ticks_ms()` and `ticks_us()`, but with the highest possible resolution
98+
in the system. This function is intended for very fine benchmarking or very tight
99+
real-time loops.
100100

101101

102102
.. function:: ticks_add(ticks, delta)
103103

104-
Offset *ticks* by a given number, which can be either positive or negative.
105-
Given a *ticks* value, this function allows to calculate a ticks value *delta*
106-
ticks before or after it, following modular-arithmetic definition of tick values
107-
(see `ticks_ms()` above). *ticks* parameter must be a direct result of call
108-
to `ticks_ms()`, `ticks_us()`, or `ticks_cpu()` functions (or from previous
109-
call to `ticks_add()`). However, *delta* can be an arbitrary integer number
110-
or numeric expression. `ticks_add()` is useful for calculating deadlines for
111-
events/tasks. (Note: you must use `ticks_diff()` function to work with
112-
deadlines.)
104+
Offset *ticks* by a given number, which can be either positive or negative.
105+
Given a *ticks* value, this function allows to calculate a ticks value *delta*
106+
ticks before or after it, following modular-arithmetic definition of tick values
107+
(see `ticks_ms()` above). *ticks* parameter must be a direct result of call
108+
to `ticks_ms()`, `ticks_us()`, or `ticks_cpu()` functions (or from previous
109+
call to `ticks_add()`). However, *delta* can be an arbitrary integer number
110+
or numeric expression. `ticks_add()` is useful for calculating deadlines for
111+
events/tasks. (Note: you must use `ticks_diff()` function to work with
112+
deadlines.)
113113

114-
Examples::
114+
Examples::
115115

116116
# Find out what ticks value there was 100ms ago
117117
print(ticks_add(time.ticks_ms(), -100))
@@ -127,77 +127,91 @@ Functions
127127

128128
.. function:: ticks_diff(ticks1, ticks2)
129129

130-
Measure ticks difference between values returned from `ticks_ms()`, `ticks_us()`,
131-
or `ticks_cpu()` functions, as a signed value which may wrap around.
132-
133-
The argument order is the same as for subtraction
134-
operator, ``ticks_diff(ticks1, ticks2)`` has the same meaning as ``ticks1 - ticks2``.
135-
However, values returned by `ticks_ms()`, etc. functions may wrap around, so
136-
directly using subtraction on them will produce incorrect result. That is why
137-
`ticks_diff()` is needed, it implements modular (or more specifically, ring)
138-
arithmetic to produce correct result even for wrap-around values (as long as they not
139-
too distant in between, see below). The function returns a **signed** value in the range
140-
[*-TICKS_PERIOD/2* .. *TICKS_PERIOD/2-1*] (that's a typical range definition for
141-
two's-complement signed binary integers). If the result is negative, it means that
142-
*ticks1* occurred earlier in time than *ticks2*. Otherwise, it means that
143-
*ticks1* occurred after *ticks2*. This holds **only** if *ticks1* and *ticks2*
144-
are apart from each other for no more than *TICKS_PERIOD/2-1* ticks. If that does
145-
not hold, incorrect result will be returned. Specifically, if two tick values are
146-
apart for *TICKS_PERIOD/2-1* ticks, that value will be returned by the function.
147-
However, if *TICKS_PERIOD/2* of real-time ticks has passed between them, the
148-
function will return *-TICKS_PERIOD/2* instead, i.e. result value will wrap around
149-
to the negative range of possible values.
150-
151-
`ticks_diff()` is designed to accommodate various usage patterns, among them:
152-
153-
* Polling with timeout. In this case, the order of events is known, and you will deal
154-
only with positive results of `ticks_diff()`::
155-
156-
# Wait for GPIO pin to be asserted, but at most 500us
157-
start = time.ticks_us()
158-
while pin.value() == 0:
159-
if time.ticks_diff(time.ticks_us(), start) > 500:
160-
raise TimeoutError
161-
162-
* Scheduling events. In this case, `ticks_diff()` result may be negative
163-
if an event is overdue::
164-
165-
# This code snippet is not optimized
166-
now = time.ticks_ms()
167-
scheduled_time = task.scheduled_time()
168-
if ticks_diff(scheduled_time, now) > 0:
169-
print("Too early, let's nap")
170-
sleep_ms(ticks_diff(scheduled_time, now))
171-
task.run()
172-
elif ticks_diff(scheduled_time, now) == 0:
173-
print("Right at time!")
174-
task.run()
175-
elif ticks_diff(scheduled_time, now) < 0:
176-
print("Oops, running late, tell task to run faster!")
177-
task.run(run_faster=true)
178-
179-
Note: Do not pass `time()` values to `ticks_diff()`, you should use
180-
normal mathematical operations on them. But note that `time()` may (and will)
181-
also overflow. This is known as https://en.wikipedia.org/wiki/Year_2038_problem.
130+
Measure ticks difference between values returned from `ticks_ms()`, `ticks_us()`,
131+
or `ticks_cpu()` functions, as a signed value which may wrap around.
132+
133+
The argument order is the same as for subtraction
134+
operator, ``ticks_diff(ticks1, ticks2)`` has the same meaning as ``ticks1 - ticks2``.
135+
However, values returned by `ticks_ms()`, etc. functions may wrap around, so
136+
directly using subtraction on them will produce incorrect result. That is why
137+
`ticks_diff()` is needed, it implements modular (or more specifically, ring)
138+
arithmetic to produce correct result even for wrap-around values (as long as they not
139+
too distant in between, see below). The function returns a **signed** value in the range
140+
[*-TICKS_PERIOD/2* .. *TICKS_PERIOD/2-1*] (that's a typical range definition for
141+
two's-complement signed binary integers). If the result is negative, it means that
142+
*ticks1* occurred earlier in time than *ticks2*. Otherwise, it means that
143+
*ticks1* occurred after *ticks2*. This holds **only** if *ticks1* and *ticks2*
144+
are apart from each other for no more than *TICKS_PERIOD/2-1* ticks. If that does
145+
not hold, incorrect result will be returned. Specifically, if two tick values are
146+
apart for *TICKS_PERIOD/2-1* ticks, that value will be returned by the function.
147+
However, if *TICKS_PERIOD/2* of real-time ticks has passed between them, the
148+
function will return *-TICKS_PERIOD/2* instead, i.e. result value will wrap around
149+
to the negative range of possible values.
150+
151+
`ticks_diff()` is designed to accommodate various usage patterns, among them:
152+
153+
* Polling with timeout. In this case, the order of events is known, and you will deal
154+
only with positive results of `ticks_diff()`::
155+
156+
# Wait for GPIO pin to be asserted, but at most 500us
157+
start = time.ticks_us()
158+
while pin.value() == 0:
159+
if time.ticks_diff(time.ticks_us(), start) > 500:
160+
raise TimeoutError
161+
162+
* Scheduling events. In this case, `ticks_diff()` result may be negative
163+
if an event is overdue::
164+
165+
# This code snippet is not optimized
166+
now = time.ticks_ms()
167+
scheduled_time = task.scheduled_time()
168+
if ticks_diff(scheduled_time, now) > 0:
169+
print("Too early, let's nap")
170+
sleep_ms(ticks_diff(scheduled_time, now))
171+
task.run()
172+
elif ticks_diff(scheduled_time, now) == 0:
173+
print("Right at time!")
174+
task.run()
175+
elif ticks_diff(scheduled_time, now) < 0:
176+
print("Oops, running late, tell task to run faster!")
177+
task.run(run_faster=true)
178+
179+
Note: Do not pass `time()` values to `ticks_diff()`, you should use
180+
normal mathematical operations on them. But note that `time()` may (and will)
181+
also overflow. This is known as https://en.wikipedia.org/wiki/Year_2038_problem.
182182

183183

184184
.. function:: time()
185185

186-
Returns the number of seconds, as an integer, since the Epoch.
186+
Returns the number of seconds, as an integer, since the Epoch.
187187

188-
.. admonition:: Difference to CPython
189-
:class: attention
188+
.. admonition:: Difference to CPython
189+
:class: attention
190190

191-
In CPython, this function returns the number of seconds since Unix epoch,
192-
1970-01-01 00:00 UTC, as a floating-point.
191+
In CPython, this function returns the number of seconds since Unix epoch,
192+
1970-01-01 00:00 UTC, as a floating-point.
193193

194194
.. function:: time_ns()
195195

196-
Similar to `time()` but returns nanoseconds since the Epoch, as an integer.
196+
Similar to `time()` but returns nanoseconds since the Epoch, as an integer.
197197

198198
.. function:: strftime(fmt_string, [time_tuple])
199199

200-
Create a readable string from the time. The *fmt_string* is compatible with the `C Python
201-
strfmt function <https://docs.python.org/3/library/time.html#time.strftime>`_. The second
202-
parameter is a time-tuple, as returned by `time.localtime` or `time.gmtime`. If this parameter
203-
is omitted, then the current time is used.
200+
Create a readable string from the time. The *fmt_string* is compatible with the `POSIX 2017
201+
standard <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html>`__. The
202+
second parameter is a time-tuple, as returned by `time.localtime` or `time.gmtime`.
203+
If this parameter is omitted, then the current time is used.
204+
205+
Note that any conversion specifiers not specified in POSIX 2017, including any from future
206+
standards, are not supported and may cause unexpected behaviour.
207+
208+
.. function:: strptime(time_string, [format])
209+
210+
Parse a time contained within the given ``time_string``. The ``format`` string is compatible
211+
with the `POSIX 2017 standard
212+
<https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html>`__ and instructs
213+
the parser what parts of the ``time_string`` correspond to which aspects of the time. By
214+
default, the format string ``"%a %b %d %H:%M:%S %Y"`` is used.
215+
216+
Note that any conversion specifiers not specified in POSIX 2017, including any from future
217+
standards, are not supported and may cause unexpected behaviour.

samples/timers/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ of the standard time module can be used. It also shows that the *DockControl* in
7575
``` python
7676
dockControl.set_timezone("GMT")
7777
```
78-
78+
7979
reload the application and observe the timezone change. NB, the timezone
8080
is now set until another call changes it again, including when another
8181
application is loaded. In practice, an application should not assume the
8282
timezone, but should set it.
83-
84-
* Try the different
8583

8684
* While a one-second frequency clock is useful to demonstrate the timer
8785
functionality, in practice a clock that updates on the minute is more

0 commit comments

Comments
 (0)