Prijeđi na sadržaj

Modul:Countdown

Izvor: Wikipedija


-- This module powers {{countdown}}.

local p = {}

-- Constants
local DST = {         -- mjesec i dan kada počinje i završava ljetno vrijeme
             ["2023"] = {s=0326, e=1029},
             ["2024"] = {s=0331, e=1027},
             ["2025"] = {s=0330, e=1026},
             ["2026"] = {s=0329, e=1025},
             ["2027"] = {s=0328, e=1031},
             ["2028"] = {s=0326, e=1029},
}

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

local function formatMessage(secondsLeft, event, color)
	local timeLeft = lang:formatDuration(secondsLeft, {'years', 'weeks', 'days', 'hours', 'minutes', 'seconds'})
	-- Find whether we are plural or not.
	local isOrAre
	if string.match(timeLeft, '^%d+') == '1' then
		isOrAre = 'is'
	else
		isOrAre = 'are'
	end
	-- Color and bold the numbers, because it makes them look important.
	timeLeft = string.gsub(timeLeft, '(%d+)', '<span style="color: ' .. (color or '#F00') .. '; font-weight: bold;">%1</span>')
	return string.format('%s %s.', event, timeLeft)
end

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

	if not (args.year and args.month and args.day) then
		return '<strong class="error">Error: year, month, and day must be specified</strong>'
	end

	local timeArgs = {year=args.year, month=args.month, day=args.day, hour=args.hour, min=args.minute, sec=args.second}
	for k,v in pairs(timeArgs) do
		if not tonumber(v) then
			error('Argument ' .. k .. ' could not be parsed as a number: ' .. v)
		end
	end
	local eventTime = os.time(timeArgs)
	local corr = 3600 -- popravka u sekundama za CET prema UTC; ispod uzimamo u obzir i CEST, zanemarujući ona 2-3 sata poslije ponoći
	local DSTbeg = DST[args.year] and DST[args.year].s or 9999
	local DSTend   = DST[args.year] and DST[args.year].e or 0000
	if (args.month*100+args.day >= DSTbeg) and (args.month*100+args.day < DSTend) then corr=7200 end
	local currTime  = os.time() + corr
	local timeToStart = os.difftime(eventTime, currTime) -- (future time - current time)
	local text
	if timeToStart > 0 then
		-- Event has not begun yet
		text = formatMessage(timeToStart, (args.event or 'Događaj') .. ' započinje za ', args.color)
	elseif args.duration then
		local timeToEnd
		if args['duration unit'] then
			-- Duration is in unit other than seconds, use formatDate to add
			timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) .. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit']))
		else
			timeToEnd = timeToStart + (tonumber(args.duration) or error('args.duration should be a number of seconds', 0))
		end
		if timeToEnd > 0 then
			-- Event is in progress
			text = args.eventstart or formatMessage(timeToEnd, (args.event or 'Događaj') .. ' završava za ', args.color)
		else
			-- Event had a duration and has now ended
			text = args.eventend or ((lang:ucfirst(args.event or 'Događaj')) .. ' je završilo.')
		end
	else
		-- Event had no duration and has begun
		text = args.eventstart or ((lang:ucfirst(args.event or 'Događaj')) .. ' je završilo.')
	end
	local refreshLink
	if args.refresh == 'no' then
		refreshLink = ''
	else
		refreshLink = mw.title.getCurrentTitle():fullUrl({action = 'purge'})
		refreshLink = string.format(' <small><span class="plainlinks">([%s osvježi])</span></small>', refreshLink)
	end
	return text .. refreshLink
end

return p