Permanently protected module
From Wikipedia, the free encyclopedia


local p = {}



function p._error( error_str )

    return '[[Category:Duration with input error]]<strong class="error">Error in Module:Duration: ' .. error_str .. '</strong>'

end



function p.main(frame)

	local args = require('Module:Arguments').getArgs(frame, {wrappers = {'Template:Duration', 'Template:Duration/sandbox'}})

	local tmp = args.duration or args1 or ''

	local duration = {}

	if tonumber(args1]) or args2 or args3 then

		if args4 then return p._error('Parameter number 4 should not be specified') end

		if not args1 or args1 == '' then

			duration = {args2 or 0, args3 or 0}

		else

			duration = {args1], args2 or 0, args3 or 0}

		end

		tmp = nil

		for k, v in ipairs(duration) do

			durationk = tonumber(v)

			if not durationk then return p._error('Invalid values') end

		end

	elseif args.h or args.m or args.s then

		if not args.h or args.h == '' then

			duration = {args.m or 0, args.s or 0}

		else

			duration = {args.h, args.m or 0, args.s or 0}

		end

		tmp = nil

		for k, v in ipairs(duration) do

			durationk = tonumber(v)

			if not durationk then return p._error('Invalid values') end

		end

	else

		if mw.ustring.find(tmp, 'class="duration"', 1, yes) then return tmp end -- if there is already a microformat, don't do anything

		duration = mw.text.split(mw.ustring.match(tmp, '%d*:?%d+:%d+%.?%d*') or '', ':') -- split into table

		if duration4 then return p._error('Maximum of two colons allowed') end

		for k, v in ipairs(duration) do durationk = tonumber(v) or 0 end -- convert values to numbers

	end

	if duration3 then

		if (duration1 + duration2 + duration3]) == 0 then return nil end

		if (duration1 ~= math.ceil(duration1])) or (duration2 ~= math.ceil(duration2])) then return p._error('Hours and minutes values must be integers') end

		if duration3 >= 60 then return p._error('Seconds value must be less than 60') end

		if duration2 >= 60 then return p._error('Minutes value must be less than 60 if hours value is specified') end

		if duration2 < 10 then duration2 = '0'..duration2 end -- zero padding

		if duration3 < 10 then duration3 = '0'..duration3 end

		duration = '<span class="duration"><span class="h">' .. duration1 .. '</span>:<span class="min">' .. duration2 .. '</span>:<span class="s">' .. duration3 .. '</span></span>'

	elseif duration2 then

		if (duration1 + duration2]) == 0 then return nil end

		if duration1 ~= math.ceil(duration1]) then return p._error('Hours and minutes values must be integers') end

		if duration2 >= 60 then return p._error('Seconds value must be less than 60') end

		if duration2 < 10 then duration2 = '0'..duration2 end -- zero padding

		duration = '<span class="duration"><span class="min">' .. duration1 .. '</span>:<span class="s">' .. duration2 .. '</span></span>'

	else

		duration = ''

	end

	

	if tmp and tmp ~= '' then

		if duration ~= '' then tmp = mw.ustring.gsub(tmp, '%d*:?%d+:%d+%.?%d*', duration, 1) else tmp = tmp .. ' [[Category:Duration without hAudio microformat]]' end

	else

		if duration ~= '' then tmp = duration end

	end

	return tmp

end



return p