UTime



Utime.time ¶ Returns the number of seconds, as an integer, since the Epoch, assuming that underlying RTC is set and maintained as decsribed 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). Follow Utime's earnings. For all future releases Just for the upcoming release Send me a reminder 1 trading day before. Delivery Method. Mobile App notifications. Email Notifications.

Changes the access and modification times on each file of a list of files. The first two elements of the list must be the NUMERIC access and modification times, in that order. Returns the number of files successfully changed. The inode change time of each file is set to the current time. For example, this code has the same effect as the Unix touch(1) command when the files already exist and belong to the user running the program:

Since Perl 5.8.0, if the first two elements of the list are undef, the utime(2) syscall from your C library is called with a null second argument. On most systems, this will set the file's access and modification times to the current time (i.e., equivalent to the example above) and will work even on files you don't own provided you have write permission:

Under NFS this will use the time of the NFS server, not the time of the local machine. If there is a time synchronization problem, the NFS server and local machine will have different times. The Unix touch(1) command will in fact normally use this form instead of the one shown in the first example.

Passing only one of the first two elements as undef is equivalent to passing a 0 and will not have the effect described when both are undef. This also triggers an uninitialized warning.

On systems that support futimes(2), you may pass filehandles among the files. On systems that don't support futimes(2), passing filehandles raises an exception. Filehandles must be passed as globs or glob references to be recognized; barewords are considered filenames.

Portability issues: 'utime' in perlport.

This module implements a subset of the correspondingCPythonmodule,as described below. For more information, refer to the originalCPython documentation:time.

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

Time Epoch: Unix port uses standard for POSIX systems epoch of1970-01-01 00:00:00 UTC. However, embedded ports use epoch of2000-01-01 00:00:00 UTC.

Maintaining actual calendar date/time: This requires aReal Time Clock (RTC). On systems with underlying OS (including someRTOS), an RTC may be implicit. Setting and maintaining actual calendartime is responsibility of OS/RTOS and is done outside of MicroPython,it just uses OS API to query date/time. On baremetal ports howeversystem time depends on machine.RTC() object. The current calendar timemay be set using machine.RTC().datetime(tuple) function, and maintainedby following means:

  • By a backup battery (which may be an additional, optional component fora particular board).

  • Using networked time protocol (requires setup by a port/user).

  • Set manually by a user on each power-up (many boards then maintainRTC time across hard resets, though some may require setting it againin such case).

If actual calendar time is not maintained with a system/MicroPython RTC,functions below which require reference to current absolute time maybehave not as expected.

Functions¶

utime.gmtime([secs])
utime.localtime([secs])

Convert the time secs expressed in seconds since the Epoch (see above) into an8-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.

The gmtime() function returns a date-time tuple in UTC, and localtime() returns adate-time tuple in local time.

The format of the entries in the 8-tuple are:

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

This is inverse function of localtime. It’s argument is a full 8-tuplewhich expresses a time as per localtime. It returns an integer which isthe number of seconds since Jan 1, 2000.

utime.sleep(seconds)
Utime

Sleep for the given number of seconds. Some boards may accept seconds as afloating-point number to sleep for a fractional number of seconds. Note thatother boards may not accept a floating-point argument, for compatibility withthem use sleep_ms() and sleep_us() functions.

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 an arbitrary reference point, thatwraps around after some value.

The wrap-around value is not explicitly exposed, but we willrefer to it as TICKS_MAX to simplify discussion. Period of the values isTICKS_PERIOD = TICKS_MAX + 1. TICKS_PERIOD is guaranteed to be a power oftwo, but otherwise may differ from port to port. The same period value is usedfor all of ticks_ms(), ticks_us(), ticks_cpu() functions (forsimplicity). Thus, these functions will return a value in range [0 ..TICKS_MAX], inclusive, total TICKS_PERIOD values. Note that onlynon-negative values are used. For the most part, you should treat values returnedby these functions as opaque. The only operations available for them areticks_diff() and ticks_add() functions described below.

Note: Performing standard mathematical operations (+, -) or relationaloperators (<, <=, >, >=) directly on these value will lead to invalidresult. Performing mathematical operations and then passing their resultsas arguments to ticks_diff() or ticks_add() will also lead toinvalid results from the latter functions.

utime.ticks_us()
UTime

Time Zone Converter

Just like ticks_ms() above, but in microseconds.

utime.ticks_cpu()

Similar to ticks_ms() and ticks_us(), but with the highest possible resolutionin the system. This is usually CPU clocks, and that’s why the function is named thatway. But it doesn’t have to be a CPU clock, some other timing source available in asystem (e.g. high-resolution timer) can be used instead. The exact timing unit(resolution) of this function is not specified on utime module level, butdocumentation for a specific port may provide more specific information. Thisfunction is intended for very fine benchmarking or very tight real-time loops.Avoid using it in portable code.

Availability: Not every port implements this function.

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 deltaticks before or after it, following modular-arithmetic definition of tick values(see ticks_ms() above). ticks parameter must be a direct result of callto ticks_ms(), ticks_us(), or ticks_cpu() functions (or from previouscall to ticks_add()). However, delta can be an arbitrary integer numberor numeric expression. ticks_add() is useful for calculating deadlines forevents/tasks. (Note: you must use ticks_diff() function to work withdeadlines.)

Examples:

utime.ticks_diff(ticks1, ticks2)

Measure ticks difference between values returned from ticks_ms(), ticks_us(),or ticks_cpu() functions, as a signed value which may wrap around.

The argument order is the same as for subtractionoperator, ticks_diff(ticks1,ticks2) has the same meaning as ticks1-ticks2.However, values returned by ticks_ms(), etc. functions may wrap around, sodirectly using subtraction on them will produce incorrect result. That is whyticks_diff() is needed, it implements modular (or more specifically, ring)arithmetics to produce correct result even for wrap-around values (as long as they nottoo distant inbetween, see below). The function returns signed value in the range[-TICKS_PERIOD/2 .. TICKS_PERIOD/2-1] (that’s a typical range definition fortwo’s-complement signed binary integers). If the result is negative, it means thatticks1 occurred earlier in time than ticks2. Otherwise, it means thatticks1 occurred after ticks2. This holds only if ticks1 and ticks2are apart from each other for no more than TICKS_PERIOD/2-1 ticks. If that doesnot hold, incorrect result will be returned. Specifically, if two tick values areapart for TICKS_PERIOD/2-1 ticks, that value will be returned by the function.However, if TICKS_PERIOD/2 of real-time ticks has passed between them, thefunction will return -TICKS_PERIOD/2 instead, i.e. result value will wrap aroundto the negative range of possible values.

Informal rationale of the constraints above: Suppose you are locked in a room with nomeans to monitor passing of time except a standard 12-notch clock. Then if you look atdial-plate now, and don’t look again for another 13 hours (e.g., if you fall for along sleep), then once you finally look again, it may seem to you that only 1 hourhas passed. To avoid this mistake, just look at the clock regularly. Your applicationshould do the same. “Too long sleep” metaphor also maps directly to applicationbehavior: don’t let your application run any single task for too long. Run tasksin steps, and do time-keeping inbetween.

ticks_diff() is designed to accommodate various usage patterns, among them:

UTime
  • Polling with timeout. In this case, the order of events is known, and you will dealonly with positive results of ticks_diff():

  • Scheduling events. In this case, ticks_diff() result may be negativeif an event is overdue:

Local News Albany Ny

Note: Do not pass time() values to ticks_diff(), you should usenormal mathematical operations on them. But note that time() may (and will)also overflow. This is known as https://en.wikipedia.org/wiki/Year_2038_problem .

utime.time()
Utimensat

Returns the number of seconds, as an integer, since the Epoch, assuming thatunderlying RTC is set and maintained as described above. If an RTC is not set, thisfunction returns number of seconds since a port-specific reference point in time (forembedded boards without a battery-backed RTC, usually since power up or reset). If youwant to develop portable MicroPython application, you should not rely on this functionto provide higher than second precision. If you need higher precision, absolutetimestamps, use time_ns(). If relative times are acceptable then use theticks_ms() and ticks_us() functions. If you need calendar time, gmtime() orlocaltime() without an argument is a better choice.

Utime_t

Difference to CPython

In CPython, this function returns number ofseconds since Unix epoch, 1970-01-01 00:00 UTC, as a floating-point,usually having microsecond precision. With MicroPython, only Unix portuses the same Epoch, and if floating-point precision allows,returns sub-second precision. Embedded hardware usually doesn’t havefloating-point precision to represent both long time ranges and subsecondprecision, so they use integer value with second precision. Some embeddedhardware also lacks battery-powered RTC, so returns number of secondssince last power-up or from other relative, hardware-specific point(e.g. reset).

utime.time_ns()

Current Time 22303

Similar to time() but returns nanoseconds since the Epoch, as an integer (usuallya big integer, so will allocate on the heap).