:mod:`utime` -- time related functions ====================================== .. module:: utime :synopsis: time related functions The ``utime`` module provides functions for getting the current time and date, measuring time intervals, and for delays. ``utime`` supports a subset of the functionality of the CPython ``time`` module. .. only:: port_sl3 or port_xlink **Time Epoch**: uses standard for POSIX systems epoch of 1970-01-01 00:00:00 UTC. Functions --------- .. only:: port_sl3 or port_xlink .. function:: localtime([secs]) Convert a time expressed in seconds since 1970 (see above) into a 9-tuple which contains: (year, month, mday, hour, minute, second, weekday, yearday, dst) If secs is not provided or None, then the current time from the RTC is used. * year includes the century (for example 2014). * month is 1-12 * mday is 1-31 * hour is 0-23 * minute is 0-59 * second is 0-59 * weekday is 0-6 for Mon-Sun * yearday is 1-366 * isdst is 0 (for compatibility) .. function:: mktime(time_tuple) This is the inverse function of localtime. It's argument is a 9-tuple which expresses a time as per localtime. It returns a floating point number which is the number of seconds since Jan 1, 1970. .. only:: port_unix or port_pyboard or port_esp8266 .. function:: localtime([secs]) Convert a time expressed in seconds since the Epoch (see above) into an 8-tuple which contains: (year, month, mday, hour, minute, second, weekday, yearday) If secs is not provided or None, then the current time from the RTC is used. * year includes the century (for example 2014). * month is 1-12 * mday is 1-31 * hour is 0-23 * minute is 0-59 * second is 0-59 * weekday is 0-6 for Mon-Sun * yearday is 1-366 .. function:: mktime() This is inverse function of localtime. It's argument is a full 8-tuple which expresses a time as per localtime. It returns an integer which is the number of seconds since Jan 1, 2000. .. only:: port_sl3 or port_xlink .. function:: sleep(seconds) Sleep for the given number of seconds. Seconds is a floating-point number with millisecond resolution. .. only:: port_unix or port_pyboard or port_wipy or port_esp8266 .. function:: sleep_ms(ms) Delay for given number of milliseconds, should be positive or 0. .. only:: port_wipy .. function:: sleep(seconds) Sleep for the given number of seconds. .. only:: port_unix or port_pyboard or port_wipy or port_esp8266 or port_sl3 or port_xlink .. function:: sleep_ms(ms) Delay for given number of milliseconds, should be positive or 0. .. function:: sleep_us(us) Delay for given number of microseconds, should be positive or 0 .. function:: ticks_ms() Returns an increasing millisecond counter with arbitrary reference point, that wraps after some (unspecified) value. The value should be treated as opaque, suitable for use only with ticks_diff(). .. function:: ticks_us() Just like ``ticks_ms`` above, but in microseconds. .. only:: port_wipy or port_pyboard or port_sl3 or port_xlink .. function:: ticks_cpu() Similar to ``ticks_ms`` and ``ticks_us``, but with higher resolution (usually CPU clocks). .. only:: port_unix or port_pyboard or port_wipy or port_esp8266 or port_sl3 or port_xlink .. function:: ticks_add(ticks, delta) Offset ticks value by a given number, which can be either positive or negative. Given a *ticks* value, this function allows to calculate ticks value *delta* ticks before or after it, following modular-arithmetic definition of tick values (see `ticks_ms()` above). *ticks* parameter must be a direct result of call to `ticks_ms()`, `ticks_us()`, or `ticks_cpu()` functions (or from previous call to `ticks_add()`). However, *delta* can be an arbitrary integer number or numeric expression. `ticks_add()` is useful for calculating deadlines for events/tasks. (Note: you must use `ticks_diff()` function to work with deadlines.) Examples:: # Find out what ticks value there was 100ms ago print(ticks_add(time.ticks_ms(), -100)) # Calculate deadline for operation and test for it deadline = ticks_add(time.ticks_ms(), 200) while ticks_diff(deadline, time.ticks_ms()) > 0: do_a_little_of_something() # Find out maximum ticks value) print(ticks_add(0, -1)) .. function:: ticks_diff(old, new) Measure period between consecutive calls to ticks_ms(), ticks_us(), or ticks_cpu(). The value returned by these functions may wrap around at any time, so directly subtracting them is not supported. ticks_diff() should be used instead. "old" value should actually precede "new" value in time, or result is undefined. This function should not be used to measure arbitrarily long periods of time (because ticks_*() functions wrap around and usually would have short period). The expected usage pattern is implementing event polling with timeout:: # Wait at most 500us for the battery voltage to climb to 12.5V start = time.ticks_us() while batt() < 12.5: if time.ticks_diff(start, time.ticks_us()) > 500: raise TimeoutError .. only:: port_sl3 or port_xlink .. function:: time() Returns the number of seconds (as a floating point number) since Jan 1, 1970 with millisecond resolution. .. only:: port_unix or port_pyboard or port_esp8266 .. function:: time() Returns the number of seconds, as an integer, since the Epoch, assuming that underlying RTC is set and maintained as described above. If an RTC is not set, this function returns number of seconds since a port-specific reference point in time (for embedded boards without a battery-backed RTC, usually since power up or reset). If you want to develop portable MicroPython application, you should not rely on this function to provide higher than second precision. If you need higher precision, use ``ticks_ms()`` and ``ticks_us()`` functions, if you need calendar time, ``localtime()`` without an argument is a better choice. .. admonition:: Difference to CPython :class: attention In CPython, this function returns number of seconds since Unix epoch, 1970-01-01 00:00 UTC, as a floating-point, usually having microsecond precision. With MicroPython, only Unix port uses the same Epoch, and if floating-point precision allows, returns sub-second precision. Embedded hardware usually doesn't have floating-point precision to represent both long time ranges and subsecond precision, so they use integer value with second precision. Some embedded hardware also lacks battery-powered RTC, so returns number of seconds since last power-up or from other relative, hardware-specific point (e.g. reset). .. only:: port_sl3 or port_xlink .. function:: elapsed_ms(start) Returns the number of milliseconds which have elapsed since ``start``. This function takes care of counter wrap, and always returns a positive number. This means it can be used to measure periods up to about 12.4 days. Example:: start = utime.ticks_ms() while utime.elapsed_millis(start) < 1000: # Perform some operation pass .. function:: elapsed_us(start) Returns the number of microseconds which have elapsed since ``start``. This function takes care of counter wrap, and always returns a positive number. This means it can be used to measure periods up to about 17.8 minutes. Example:: start = utime.ticks_us() while utime.elapsed_micros(start) < 1000: # Perform some operation pass