Модуль:Serialization/TimeSpan: различия между версиями

Материал из Space Station 14 Вики
м Pok переименовал страницу Модуль:Code/Формат/Время в Модуль:Serialization/TimeSpan
Нет описания правки
 
(не показаны 2 промежуточные версии этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs


local function parse_timespan(str)
local function parse_timespan(str)
Строка 26: Строка 27:
end
end


-- Форматирование в "X час. Y мин. Z сек."
local function bold(n)
    return "<b>" .. tostring(n) .. "</b>"
end
 
-- Форматирование в "X час. Y мин. Z сек." с жирными числами
local function format_time(value)
local function format_time(value)
     local total_seconds = math.floor(value)
     local total_seconds = math.floor(value)
Строка 37: Строка 42:


     if hours > 0 then
     if hours > 0 then
         table.insert(parts, hours .. " час.")
         table.insert(parts, bold(hours) .. " час.")
     end
     end
     if minutes > 0 then
     if minutes > 0 then
         table.insert(parts, minutes .. " мин.")
         table.insert(parts, bold(minutes) .. " мин.")
     end
     end
     if seconds > 0 or #parts == 0 then
     if seconds > 0 or #parts == 0 then
         table.insert(parts, seconds .. " сек.")
         table.insert(parts, bold(seconds) .. " сек.")
     end
     end


Строка 50: Строка 55:


function p.main(frame)
function p.main(frame)
     local args = frame.args[1]
     local args = getArgs(frame, { removeBlanks = false })
     local time = parse_timespan(args)
     local time = parse_timespan(args[1])
     return format_time(time)
     return format_time(time)
end
end


return p
return p

Текущая версия от 18:09, 8 марта 2026

Для документации этого модуля может быть создана страница Модуль:Serialization/TimeSpan/doc

local p = {}
local getArgs = require('Module:Arguments').getArgs

local function parse_timespan(str)
    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

local function bold(n)
    return "<b>" .. tostring(n) .. "</b>"
end

-- Форматирование в "X час. Y мин. Z сек." с жирными числами
local function format_time(value)
    local total_seconds = math.floor(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, bold(hours) .. " час.")
    end
    if minutes > 0 then
        table.insert(parts, bold(minutes) .. " мин.")
    end
    if seconds > 0 or #parts == 0 then
        table.insert(parts, bold(seconds) .. " сек.")
    end

    return table.concat(parts, " ")
end

function p.main(frame)
    local args = getArgs(frame, { removeBlanks = false })
    local time = parse_timespan(args[1])
    return format_time(time)
end

return p