Skip to content
Boris Lovosevic edited this page Feb 18, 2019 · 5 revisions

utime Module


The utime module provides functions for getting the current time and date, measuring time intervals, and for delays.

Time Epoch: This port uses standard for POSIX systems epoch of 1970-01-01 00:00:00 UTC.

Maintaining actual calendar date/time: K210 RTC peripheral is used for maintaining the actual date/time.
The date/time information is lost after power is removed.
Support for external RTC module is planned for some later time.


Methods


utime.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.
The time returnes is corrected to the local time zone.

  • 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

utime.gmtime( [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.
The time returned is the UTC+0 time.

  • 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

utime.mktime( time_tuple [,tz] [,setrtc] )

This is inverse function of gmtime/localtime. It’s argument is a full 8-tuple which expresses a time as per gmtime/localtime. It returns an integer which is the number of seconds since Epoch (Jan 1, 1970).

If the optional argument tz is provides, the local time zone is set.
The tz argument must be a time integer in the rangs -11 - +12 (relative to UTC).

If the optional argument setrtc is True, the system RTC time is set.

utime.strftime( format [,time] [,local] )

Convert a tuple time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument. If time is not provided, the current RTC time is used.
format must be a string. Standard Python time formating should be used
If local argument is True (default), local time is used, if False gmtime is used.

utime.time()

Returns the number of seconds, as an integer, since the Epoch (Jan 1, 1970) from system RTC peripheral.



For the sleep time up to 2000 us the sleep functions are blocking.
For the sleep times greater than 2000 us, the functions do not block the execution of the other threads.
Sleeping can be interrupted if the thread executing it receives the notification.

utime.sleep( seconds )

Sleep for the given number of seconds.
Seconds provided as integer or a floating-point (sleep for a fractional number of seconds) are accepted.

utime.sleep_ms( ms )

Sleep for the given number of milli seconds.

utime.sleep_us( us )

Sleep for the given number of micro seconds.

utime.rsleep( seconds )

Sleep for the given number of seconds.
Seconds provided as integer or a floating-point (sleep for a fractional number of seconds) are accepted.
Returns the actual sleep time. If the sleep was interrupted, it will be less than requested.

utime.rsleep_ms( ms )

Sleep for the given number of milli seconds.
Returns the actual sleep time. If the sleep was interrupted, it will be less than requested.

utime.rsleep_us( us )

Sleep for the given number of micro seconds.
Returns the actual sleep time. If the sleep was interrupted, it will be less than requested.


All ticks_* functions uses 64-bit values and never overflows.
Performing standard mathematical operations (+, -) or relational operators (<, <=, >, >=) directly on returned values is allowed.

utime.ticks_cpu()

Returns the number of CPU clocks since the power was applied.

utime.ticks_us()

Returns the number micro seconds since the power was applied.

utime.ticks_ms()

Returns the number of milli seconds since the power was applied.

utime.ticks_add(ticks, delta)

Offset ticks value by a given number, which can be either positive or negative.
This function is provided for compatibility with other MicroPython ports.
As ticks values never overflows, performing standard mathematical operations (+, -) on ticks values is allowed.

utime.ticks_diff(ticks, delta)

Measure ticks difference between values returned from ticks_ms(), ticks_us(), or ticks_cpu().
This function is provided for compatibility with other MicroPython ports.
As ticks values never overflows, performing standard mathematical operations (+, -) on ticks values is allowed.