utime
– 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.
Time Epoch: uses standard for POSIX systems epoch of 19700101 00:00:00 UTC.
Functions¶

utime.
localtime
([secs])¶ Convert a time expressed in seconds since 1970 (see above) into a 9tuple 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 112
 mday is 131
 hour is 023
 minute is 059
 second is 059
 weekday is 06 for MonSun
 yearday is 1366
 isdst is 0 (for compatibility)

utime.
mktime
(time_tuple)¶ This is the inverse function of localtime. It’s argument is a 9tuple which expresses a time as per localtime. It returns a floating point number which is the number of seconds since Jan 1, 1970.

utime.
sleep
(seconds)¶ Sleep for the given number of seconds. Seconds is a floatingpoint number with millisecond resolution.

utime.
sleep_ms
(ms)¶ Delay for given number of milliseconds, should be positive or 0.

utime.
sleep_us
(us)¶ Delay for given number of microseconds, should be positive or 0

utime.
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().

utime.
ticks_us
()¶ Just like
ticks_ms
above, but in microseconds.

utime.
ticks_cpu
()¶ Similar to
ticks_ms
andticks_us
, but with higher resolution (usually CPU clocks).

utime.
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 modulararithmetic 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))

utime.
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

utime.
time
()¶ Returns the number of seconds (as a floating point number) since Jan 1, 1970 with millisecond resolution.

utime.
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

utime.
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