82 lines
2.9 KiB
Lua
82 lines
2.9 KiB
Lua
|
|
-- tracker.today — report total active time accrued today (local-day).
|
||
|
|
local lang = jarvis.context.language or "ru"
|
||
|
|
local now = jarvis.context.time.timestamp or os.time()
|
||
|
|
|
||
|
|
local data = jarvis.state.get("data") or {}
|
||
|
|
local sessions = data.sessions or {}
|
||
|
|
|
||
|
|
-- Local-day boundary: use the calendar fields (already in local time).
|
||
|
|
local t = jarvis.context.time
|
||
|
|
local y = tonumber(t.year) or 1970
|
||
|
|
local mo = tonumber(t.month) or 1
|
||
|
|
local d = tonumber(t.day) or 1
|
||
|
|
local h = tonumber(t.hour) or 0
|
||
|
|
local mi = tonumber(t.minute) or 0
|
||
|
|
local s = tonumber(t.second) or 0
|
||
|
|
|
||
|
|
-- Seconds since local midnight today, derived from the broken-down time.
|
||
|
|
local secs_today = h * 3600 + mi * 60 + s
|
||
|
|
local today_start = now - secs_today
|
||
|
|
|
||
|
|
local total = 0
|
||
|
|
for _, sess in ipairs(sessions) do
|
||
|
|
local sstart = tonumber(sess.start) or 0
|
||
|
|
local send = tonumber(sess["end"]) or 0
|
||
|
|
-- Overlap with [today_start, now]
|
||
|
|
local lo = math.max(sstart, today_start)
|
||
|
|
local hi = math.min(send, now)
|
||
|
|
if hi > lo then total = total + (hi - lo) end
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Include the open session, if any, up to now.
|
||
|
|
local open_start = data.current_session_start
|
||
|
|
if open_start then
|
||
|
|
local lo = math.max(tonumber(open_start) or 0, today_start)
|
||
|
|
if now > lo then total = total + (now - lo) end
|
||
|
|
end
|
||
|
|
|
||
|
|
local function format_dur(sec)
|
||
|
|
sec = math.floor(sec)
|
||
|
|
local hh = math.floor(sec / 3600)
|
||
|
|
local mm = math.floor((sec % 3600) / 60)
|
||
|
|
if lang == "ru" then
|
||
|
|
local function h_word(n)
|
||
|
|
local n10 = n % 10; local n100 = n % 100
|
||
|
|
if n100 >= 11 and n100 <= 14 then return "часов" end
|
||
|
|
if n10 == 1 then return "час" end
|
||
|
|
if n10 >= 2 and n10 <= 4 then return "часа" end
|
||
|
|
return "часов"
|
||
|
|
end
|
||
|
|
local function m_word(n)
|
||
|
|
local n10 = n % 10; local n100 = n % 100
|
||
|
|
if n100 >= 11 and n100 <= 14 then return "минут" end
|
||
|
|
if n10 == 1 then return "минута" end
|
||
|
|
if n10 >= 2 and n10 <= 4 then return "минуты" end
|
||
|
|
return "минут"
|
||
|
|
end
|
||
|
|
if hh > 0 and mm > 0 then
|
||
|
|
return hh .. " " .. h_word(hh) .. " " .. mm .. " " .. m_word(mm)
|
||
|
|
end
|
||
|
|
if hh > 0 then return hh .. " " .. h_word(hh) end
|
||
|
|
return mm .. " " .. m_word(mm)
|
||
|
|
else
|
||
|
|
if hh > 0 and mm > 0 then return hh .. "h " .. mm .. "m" end
|
||
|
|
if hh > 0 then return hh .. "h" end
|
||
|
|
return mm .. "m"
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Suppress unused-warning for y/mo/d — only h/mi/s feed the calc, but having
|
||
|
|
-- y/mo/d on hand keeps the script easy to extend later.
|
||
|
|
local _ = y + mo + d
|
||
|
|
|
||
|
|
if total < 60 then
|
||
|
|
return jarvis.cmd.ok(lang == "ru"
|
||
|
|
and "Сегодня ещё ничего не отработано, сэр."
|
||
|
|
or "Nothing tracked yet today, sir.")
|
||
|
|
end
|
||
|
|
|
||
|
|
return jarvis.cmd.ok(lang == "ru"
|
||
|
|
and ("Сегодня отработано " .. format_dur(total) .. ".")
|
||
|
|
or ("Today: " .. format_dur(total) .. "."))
|