Module:Countdown2

From Wikipedia, the free encyclopedia
-- This module powers {{countdown}}.

local p = {}

-- Constants
local lang = mw.language.getContentLanguage()
local getArgs = require('Module:Arguments').getArgs
local yn = require("Module:Yesno")
local floor = math.floor
local mod = math.fmod

function p.main(frame, args)
	args = args or frame.args
	local timeInput = args['time'] or args[1] or error("No time string specified!")
	local offset = args['offset'] or args[2] or ''
	local shorthand = yn(args['shorthand'], false)
	local timestamp = tonumber(frame:preprocess("{{#time:U|" .. timeInput .. ' ' .. offset .. "|}}"))
	local currentTime = tonumber(frame:preprocess("{{#time:U}}"))
	local time_days = floor((timestamp - currentTime) / 86400)
	local time_hours = floor(mod((timestamp - currentTime) / 3600, 24))
	local time_minutes = floor(mod((timestamp - currentTime) / 60, 60))
	local time_seconds = floor(mod((timestamp - currentTime), 60))
	local out = ''
	if shorthand then
		-- clip the first letter of each display
		local secondDisplay, minuteDisplay, hourDisplay, dayDisplay
		secondDisplay = mw.ustring.sub(mw.message.new("seconds", ""):plain(), 0, 1)
		minuteDisplay = mw.ustring.sub(mw.message.new("minutes", ""):plain(), 0, 1)
		hourDisplay = mw.ustring.sub(mw.message.new("hours", ""):plain(), 0, 1)
		dayDisplay = mw.ustring.sub(mw.message.new("days", ""):plain(), 0, 1)
		if time_days > 0 then
			out = out .. time_days .. dayDisplay .. " "
		end
		if time_hours > 0 then
			out = out .. time_hours .. hourDisplay .. " "
		end
		if time_minutes > 0 then
			out = out .. time_minutes .. minuteDisplay .. " "
		end
		if time_seconds > 0 then
			out = out .. time_seconds .. secondDisplay .. " "
		end
	else
		if time_days > 0 then
			out = out .. mw.message.new("days", time_days):plain() .. " "
		end
		if time_hours > 0 then
			out = out .. mw.message.new("hours", time_hours):plain() .. " "
		end
		if time_minutes > 0 then
			out = out .. mw.message.new("minutes", time_minutes):plain() .. " "
		end
		if time_seconds > 0 then
			out = out .. mw.message.new("seconds", time_seconds):plain() .. " "
		end
	end
	return frame:preprocess('<span style="' .. (args['style'] or '') .. '">' .. out .. '</span>' .. (mw.ustring.len(out) > 0 and ' [{{fullurl:{{FULLPAGENAMEE}}|action=purge}} Update])' or ''))
end

return p