Skip to content

Commit 613fd0a

Browse files
committed
docs/library/utime: Elaborate on epochs and calendar time maintenance.
1 parent 9dd2c92 commit 613fd0a

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

docs/library/utime.rst

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,40 @@
77
The ``utime`` module provides functions for getting the current time and date,
88
and for sleeping.
99

10+
**Time Epoch**: Unix port uses standard for POSIX systems epoch of
11+
1970-01-01 00:00:00 UTC. However, embedded ports use epoch of
12+
2000-01-01 00:00:00 UTC.
13+
14+
**Maintaining actual calendar date/time**: This requires a
15+
Real Time Clock (RTC). On systems with underlying OS (including some
16+
RTOS), an RTC may be implicit. Setting and maintaining actual calendar
17+
time is responsibility of OS/RTOS and is done outside of MicroPython,
18+
it just uses OS API to query date/time. On baremetal ports however
19+
system time depends on ``machine.RTC()`` object. The current calendar time
20+
may be set using ``machine.RTC().datetime(tuple)`` function, and maintained
21+
by following means:
22+
23+
* By a backup battery (which may be an additional, optional component for
24+
a particular board).
25+
* Using networked time protocol (requires setup by a port/user).
26+
* Set manually by a user on each power-up (many boards then maintain
27+
RTC time across hard resets, though some may require setting it again
28+
in such case).
29+
30+
If actual calendar time is not maintained with a system/MicroPython RTC,
31+
functions below which require reference to current absolute time may
32+
behave not as expected.
33+
1034
Functions
1135
---------
1236

1337
.. function:: localtime([secs])
1438

15-
Convert a time expressed in seconds since Jan 1, 2000 into an 8-tuple which
39+
Convert a time expressed in seconds since the Epoch (see above) into an 8-tuple which
1640
contains: (year, month, mday, hour, minute, second, weekday, yearday)
1741
If secs is not provided or None, then the current time from the RTC is used.
18-
year includes the century (for example 2014).
1942

43+
* year includes the century (for example 2014).
2044
* month is 1-12
2145
* mday is 1-31
2246
* hour is 0-23
@@ -92,24 +116,24 @@ Functions
92116

93117
.. function:: time()
94118

95-
Returns the number of seconds, as an integer, since a port-specific reference point
96-
in time (for embedded boards without RTC, usually since power up or reset). If you
97-
want to develop portable MicroPython application, you should not rely on this
98-
function to provide higher than second precision, or on a specific reference time
99-
point. If you need higher precision, use ``ticks_ms()`` and ``ticks_us()`` functions,
100-
if you need calendar time, ``localtime()`` without argument is the best possibility
101-
to get it.
119+
Returns the number of seconds, as an integer, since the Epoch, assuming that underlying
120+
RTC is set and maintained as decsribed above. If an RTC is not set, this function returns
121+
number of seconds since a port-specific reference point in time (for embedded boards without
122+
a battery-backed RTC, usually since power up or reset). If you want to develop portable
123+
MicroPython application, you should not rely on this function to provide higher than second
124+
precision. If you need higher precision, use ``ticks_ms()`` and ``ticks_us()`` functions,
125+
if you need calendar time, ``localtime()`` without an argument is a better choice.
102126

103127
.. admonition:: Difference to CPython
104128
:class: attention
105129

106130
In CPython, this function returns number of
107131
seconds since Unix epoch, 1970-01-01 00:00 UTC, as a floating-point,
108132
usually having microsecond precision. With MicroPython, only Unix port
109-
uses the same reference point, and if floating-point precision allows,
133+
uses the same Epoch, and if floating-point precision allows,
110134
returns sub-second precision. Embedded hardware usually doesn't have
111135
floating-point precision to represent both long time ranges and subsecond
112-
precision, so use integer value with second precision. Most embedded
136+
precision, so they use integer value with second precision. Some embedded
113137
hardware also lacks battery-powered RTC, so returns number of seconds
114138
since last power-up or from other relative, hardware-specific point
115139
(e.g. reset).

0 commit comments

Comments
 (0)