Модуль:Песочница/CatBG
Материал из Space Station 14 Вики
Для документации этого модуля может быть создана страница Модуль:Песочница/CatBG/doc
local p = {}
-- Парсим строки вида "10h", "2.5h" → секунды
local function parse_hours_string(s)
if type(s) ~= "string" then return nil end
local h = s:match("^%s*([%d.]+)%s*[hH]%s*$")
if h then
local num = tonumber(h)
if num then
return num * 3600
end
end
return nil
end
local function format_time(mode, value)
local total_seconds
-- Попытка распарсить как "10h", "2.5h"
local parsed = parse_hours_string(value)
if parsed ~= nil then
total_seconds = parsed
else
-- Старая логика: value должно быть числом
local num = tonumber(value)
if num == nil then
total_seconds = 0
else
total_seconds = (mode == "minutes") and (num * 60) or num
end
end
local hours = math.floor(total_seconds / 3600)
local minutes = math.floor((total_seconds % 3600) / 60)
local seconds = math.floor(total_seconds + 0.5) % 60 -- округление при дробях
local parts = {}
if hours > 0 then
table.insert(parts, hours .. " час.")
end
if minutes > 0 then
table.insert(parts, minutes .. " мин.")
end
if seconds > 0 or #parts == 0 then
table.insert(parts, seconds .. " сек.")
end
return table.concat(parts, " ")
end
function p.main(frame)
local args = frame.args
local mode = args[1] or "seconds"
local value = args[2] or 0
return format_time(mode, value)
end
return p