|
7 | 7 | The ``utime`` module provides functions for getting the current time and date, |
8 | 8 | and for sleeping. |
9 | 9 |
|
| 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 | + |
10 | 34 | Functions |
11 | 35 | --------- |
12 | 36 |
|
13 | 37 | .. function:: localtime([secs]) |
14 | 38 |
|
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 |
16 | 40 | contains: (year, month, mday, hour, minute, second, weekday, yearday) |
17 | 41 | If secs is not provided or None, then the current time from the RTC is used. |
18 | | - year includes the century (for example 2014). |
19 | 42 |
|
| 43 | + * year includes the century (for example 2014). |
20 | 44 | * month is 1-12 |
21 | 45 | * mday is 1-31 |
22 | 46 | * hour is 0-23 |
@@ -92,24 +116,24 @@ Functions |
92 | 116 |
|
93 | 117 | .. function:: time() |
94 | 118 |
|
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. |
102 | 126 |
|
103 | 127 | .. admonition:: Difference to CPython |
104 | 128 | :class: attention |
105 | 129 |
|
106 | 130 | In CPython, this function returns number of |
107 | 131 | seconds since Unix epoch, 1970-01-01 00:00 UTC, as a floating-point, |
108 | 132 | 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, |
110 | 134 | returns sub-second precision. Embedded hardware usually doesn't have |
111 | 135 | 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 |
113 | 137 | hardware also lacks battery-powered RTC, so returns number of seconds |
114 | 138 | since last power-up or from other relative, hardware-specific point |
115 | 139 | (e.g. reset). |
0 commit comments