summaryrefslogtreecommitdiff
path: root/update_time.lua
blob: a57217cbf8322820605ff1ccd112a52e414e4ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function update_time()
	-- Update time
	-- note that rtctime.get() provided updates only if time was set at least once
	-- either with rtctime.set() or with sntp.sync() which sets it internally
	sec, usec  = rtctime.get()
	local usingUptime=false;
	if ( sec <= 0) then
		sec = tmr.time(); -- uptime
		usingUptime = true;
	else
		sec = sec + uts_offset;
	end

	tm = rtctime.epoch2cal(sec)
	-- print(string.format("%04d/%02d/%02d %02d:%02d:%02d", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"]))
	if ( usingUptime ) then
		local days = math.floor(sec/86400)
		dstr = string.format("up %03d days", days)
	else
		dstr = string.format("%04d/%02d/%02d", tm["year"], tm["mon"], tm["day"])
	end
	tstr = string.format("%02d:%02d:%02d", tm["hour"], tm["min"], tm["sec"])

	time_tmr:start()
end

-- set up timer for time update
time_tmr=tmr.create()
time_tmr:register(time_update_interval, tmr.ALARM_SEMI, function() update_time() end)

print("--- Starting Time Updates ---")
time_tmr:start()