time(mode=0)
Get current time from selected source.
Parameters
mode:
integer, one of:
- 0: Controller execution time. Time counted since low level controller start. Guaranteed monotonic. Reset on robot restart.
- 1: Reserved.
- 2: System time in GMT time zone. Not guaranteed to be monotonic - can go backwards when system time is adjusted.
Return
Function retruns structure in format struct(sec, nanosec)
Example 1: Get seconds part of current time counted since low level controller start.
current_time_s = time().sec
Example 2: Get current system time in seconds including fraction of second.
t = time() global current_time_s = t.sec + t.nanosec / 1000000000
Example 3: Get current date derived from system clock. NOTE: time(2) function returns GMT time.
# Converts seconds since 1970.01.01 to date
# Based on http://howardhinnant.github.io/date_algorithms.html
# Returns:
# struct(year, month, day)
def seconds_to_date(z):
local d = struct(year = 0, month = 0, day = 0)
z = floor(z / 86400)
z = z + 719468
local era = floor(z/146097)
local doe = z - era * 146097
local yoe = floor((doe - floor(doe/1460) + floor(doe/36524) - floor(doe/146096)) / 365)
d.year = yoe + era * 400
local doy = doe - (365*yoe + floor(yoe/4) - floor(yoe/100))
local mp = floor((5*doy + 2)/153)
d.day = doy - floor((153*mp + 2)/5) + 1
if(mp < 10):
d.month = mp + 3
else:
d.month = mp - 9
end
if(d.month <= 2):
d.year = d.year + 1
end
return d
end
date_gmt = seconds_to_date(time(2).sec)
Example 4: Get current GMT time derived from system clock.
Converts seconds since 1970.01.01 to time of day
# Returns:
# struct(hour, minute, second)
def seconds_to_time(z):
local t = struct(hour = 0, minute = 0, second = 0)
local sod = z % 86400
t.hour = floor(sod / 3600)
t.minute = floor((sod - t.hour * 3600) / 60)
t.second = sod % 60
return t
end
time_gmt = seconds_to_time(time(2).sec)