پودمان:Countdown

از ویکی‌سفر، راهنمای آزاد سفر
نماد توضیحات توضیحات پودمان[ایجاد]
-- This module powers {{countdown}}.

local p = {}

-- Constants
local lang = mw.language.getContentLanguage()
local getArgs = require('Module:Arguments').getArgs

function formatMessage(secondsLeft, event, color, refreshLink)
    local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'})
    -- Find whether we are plural or not.
    -- no use to us.
    -- no use to us.
    -- no use to us.
    -- no use to us.
    -- no use to us.
    -- no use to us.
    -- Color and bold the numbers, because it makes them look important.
    local timeLeft = mw.ustring.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>')
    -- Make the refresh link and join it all together.
    return mw.ustring.format('تا %s %s باقی مانده‌است.%s', event, timeLeft, refreshLink)
end

function p.main(frame)
    local args = getArgs(frame)

    if not (args["سال"] and args["ماه"] and args["روز"]) then
        return  '<strong class="error">خطا: سال، ماه، و روز باید مشخص شده باشند</strong>'
    end

    local eventTime = os.time{year=args["سال"], month=args["ماه"], day=args["روز"], hour=args["ساعت"], min=args["دقیقه"], sec=args["ثانیه"]}
    local timeToStart = os.difftime(eventTime, os.time()) -- (future time - current time)

    local refreshLink = mw.title.getCurrentTitle():fullUrl{action = 'purge'}
    refreshLink = mw.ustring.format(' <small><span class="plainlinks">([%s تازه‌سازی])</span></small>', refreshLink)
    if timeToStart > 0 then
        -- Event has not begun yet
        return formatMessage(timeToStart, args["رویداد"] or 'آغاز رویداد', args["رنگ"], refreshLink)
    elseif args["مدت"] then
        local timeToEnd
        if args["یکای مدت"] then
            -- Duration is in unit other than seconds, use formatDate to add
            timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) .. ' +' .. tostring(args["مدت"]) .. ' ' .. args["یکای مدت"]))
        else
            timeToEnd = timeToStart + tonumber(args["مدت"])
        end
        if timeToEnd > 0 then
            -- Event is in progress
            return args["آغاز رویداد"] .. refreshLink or formatMessage(timeToEnd, (args["رویداد"] or 'رویداد') .. ' ends', args["رویداد"], refreshLink)
        else
            -- Event had a duration and has now ended
            return ((args["پایان رویداد"] or ((args["رویداد"] or 'رویداد') .. ' پایان یافته‌است.')) .. refreshLink)
        end
    else
        -- Event had no duration and has begun
        return ((args["آغاز رویداد"] or ((args["رویداد"] or 'رویداد') .. ' آغاز شده‌است.')) .. refreshLink)
    end
end

return p