--[[ @module clock @summary 设备时钟: 平台 report_config 校准后输出真实 Unix 秒 @version 0.1 @date 2026.08.31 @usage 对齐《DLD960_IoT_MQTT协议》§2.3 时钟同步语义: - 未校准: 返回上电秒数(mstick()/1000,值很小,平台识别"未校准") - 校准: base_unix + (now_ms - base_ms)/1000(真实 Unix 秒) - 合法性门槛 ≥1600000000(2020-09 之后),非法值忽略 - 重启后需重新校准(无掉电保持) ]] local clock = {} local base_unix = 0 local base_ms = 0 -- 校准(平台 report_config 下发 ts 时调用) -- @param unix_ts number: 平台下发的 Unix 秒 -- @param now_ms number: 当前 mstick() function clock.sync(unix_ts, now_ms) if not unix_ts or unix_ts < 1600000000 then return -- 非法值忽略 end base_unix = unix_ts base_ms = now_ms or mstick() end -- 当前 Unix 秒(未校准返回上电秒数) function clock.now(now_ms) now_ms = now_ms or mstick() if base_unix >= 1600000000 then return base_unix + math.floor((now_ms - base_ms) / 1000) end return math.floor(now_ms / 1000) end -- 是否已校准 function clock.synced() return base_unix >= 1600000000 end return clock