Module:time

Definition from Wiktionary, the free dictionary
Jump to: navigation, search

local export = {}


local language = mw.getContentLanguage()
local function format(code, timestamp)
	return language:formatDate(code, timestamp)
end

local gsub = mw.ustring.gsub


function export.quote(frame)
	local params = {
		["year"] = {},
		["month"] = {},
		["date"] = {},
		["start_date"] = {},
		["start_year"] = {},
		["nodate"] = { type = boolean, default = false },
		["accessdate"] = {},
		["origdate"] = {},
		["origyear"] = {},
		["origmonth"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params, true)
	
	local output = {}
	
	local namespace = mw.title.getCurrentTitle().nsText
	
	local start_date, date = args.start_date, args.date
	
	--[[	"1 January, 2015", with day of the month before month name
			and a comma, is not a valid date format; formatDate will
			ignore the year "2015" and replace it with the current year.
			The comma therefore has to be removed.		]]
	start_date = gsub(start_date, "(%d+ %a+),", "%1")
	date = gsub(date, "(%d+ %a+),", "%1")
	--[[
	mw.log("start_date = " .. start_date)
	mw.log("date = " .. date)
	mw.log(format("'''Y''' F j", start_date))
	]]
	
	local function insert(text)
		table.insert(output, text)
		mw.log(tostring(text))
	end
	
	if args.year then
		if start_date then
			if format("Y", start_date) == args.year then
				if format("F", start_date) == args.month then
					insert(
						format("'''Y''' F j", start_date)
							.. "–" .. date
					)
				else
					insert(
						format("'''Y''' F j",
							start_date)
							.. " – " .. args.month .. " " .. date
					)
				end
			end
		else
			if args.month then
				if args.start_year then
					insert(
						"'''" .. args.start_year
						.. "''' – "
					)
				end
				
				insert(
					"'''" .. args.year .. "''' "
					.. args.month
				)
				
				if date then
					insert(" " .. date)
				end
			else
				if args.start_year then
					insert("'''" .. args.start_year .. "'''–")
				end
				
				insert("'''" .. args.year .. "'''")
			end
		end
	else
		if date then
			if start_date then
				if format("Y", start_date) == format("Y", date) then
					if format("n", start_date) == format("n", date) then
						insert(
							format("'''Y''' F j", start_date)
								.. "–" .. format("j", date)
						)
					else
						insert(
							format("'''Y''' F j", start_date)
								.. "–" .. format("F j", date)
						)
					end
				else
					insert(
						format("'''Y''' F j", start_date)
							.. "–" .. format("'''Y''' F j", date)
					)
				end
			else
				insert(
					format("'''Y''' F j", date)
				)
			end
		else
			if not args.nodate then
				if args.accessdate then
					insert(
						format("'''Y''' F j", args.accessdate)
						.. " (last accessed)"
					)
				else
					if namespace ~= "Template" then
						insert(
							frame:expandTemplate{ title = rfdate }
						)
					end
				end
			end
		end
	end

	return table.concat(output)
end

return export