وحدة:Countdown-0

من ويكيبيديا، الموسوعة الحرة
-- This module powers {{عد تنازلي}}.

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