Модуль:Code/Формат/Время: различия между версиями
Pok (обсуждение | вклад) Новая страница: «local p = {} local function format_time(mode, value) local total_seconds = mode == "minutes" and (value * 60) or value local hours = math.floor(total_seconds / 3600) local minutes = math.floor((total_seconds % 3600) / 60) local seconds = total_seconds % 60 local parts = {} if hours > 0 then table.insert(parts, hours .. " час." .. (hours > 1 and "а" or "")) end if minutes > 0 then table.inser...» |
Pok (обсуждение | вклад) мНет описания правки |
||
| (не показаны 2 промежуточные версии этого же участника) | |||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local function | local function parse_timespan(str) | ||
local | if not str then return 0 end | ||
str = str:match("^%s*(.-)%s*$") | |||
local number = tonumber(str) | |||
if number then return number end | |||
if #str <= 1 then return 0 end | |||
local value = tonumber(str:sub(1, -2)) | |||
local unit = str:sub(-1):lower() | |||
if not value then return 0 end | |||
if unit == "s" then | |||
return value | |||
elseif unit == "m" then | |||
return value * 60 | |||
elseif unit == "h" then | |||
return value * 3600 | |||
else | |||
return 0 | |||
end | |||
end | |||
-- Форматирование в "X час. Y мин. Z сек." | |||
local function format_time(value) | |||
local total_seconds = math.floor(value) | |||
local hours = math.floor(total_seconds / 3600) | local hours = math.floor(total_seconds / 3600) | ||
local minutes = math.floor((total_seconds % 3600) / 60) | local minutes = math.floor((total_seconds % 3600) / 60) | ||
local seconds = total_seconds % 60 | local seconds = total_seconds % 60 | ||
local parts = {} | local parts = {} | ||
if hours > 0 then | if hours > 0 then | ||
table.insert(parts, hours .. " час." | table.insert(parts, hours .. " час.") | ||
end | end | ||
if minutes > 0 then | if minutes > 0 then | ||
| Строка 19: | Строка 45: | ||
table.insert(parts, seconds .. " сек.") | table.insert(parts, seconds .. " сек.") | ||
end | end | ||
return table.concat(parts, " ") | return table.concat(parts, " ") | ||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
local args = frame. | local args = frame.args[1] | ||
local time = parse_timespan(args) | |||
local | return format_time(time) | ||
return format_time( | |||
end | end | ||
return p | return p | ||