Module:TimeSwitch

-- Tests for Module:TimeSwitch are in the /testcases subpage --

-- -- This module displays different text based on the current date. -- It reuqires a table of numbered arguments which are any string alternating with a date-string. -- The parameter list can be read as follow: -- -- Display *param1* until the date specified in *param2* is reached. -- If the date specified in *param2* is reached, -- display *param3* until the time specified in *param4* is reached. -- -- This template supports an unlimited amount of passed parameters. -- If the last passed date is over, the invoking page will automatically -- be added to the category "Pages with unneded TimeSwitches" so that no longer necessary -- calls to this module can be removed to improve page-generation time. -- -- @see http://tieske.github.io/date/

local date = require('Dev:Date') local util = require('Module:Utility')

local p = {}

-- -- Main entry point to be called from the templates. -- It processes the frame arguments. Arguments of hte function p.main(frame) local tArgs = util.getArgs(frame)

return p._main(tArgs) end

-- -- Intermal main function. Only handles a table of arguments and not the complate frame. -- -- @param args A table of arguments. Must be any string and a date-string alternating --              in any number of repetitions. -- -- @return  One of the strings in the parameter list. -- function p._main(tArgs) local category = "" local i = 1 local dayStart = util.dayStart while tArgs[i] or tArgs[i+1] do       if tArgs[i+1] == nil then return tArgs[i] .. category else local switchTime if string.match(tArgs[i+1], "%s%d%d:%d%d:%d%d$") then switchTime = date(tArgs[i+1] .. " UTC") else switchTime = date(tArgs[i+1] .. " " .. dayStart) end if switchTime >= date then if tArgs[i] == nil then return "" end return tArgs[i] end end i = i + 2; end return category end

return p

-- --