Jump to content

Module:Infobox/dates/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 10:48, 7 April 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local getArgs = require('Module:Arguments').getArgs

local default_error_category = "[[Category:Pages using infobox television with nonstandard dates]]"

local p = {}

function p.start_end_date_template_validation(frame)
	local args = getArgs(frame)
	local error_category = args.error_category or default_error_category

	local start_date = args.first_aired or args.released or args.airdate or args.release_date or args.airdate_overall
	if start_date then
		if not start_date:find("dtstart") then
			return error_category
		end
	end

	local end_date = args.last_aired
	if end_date then
		if not end_date:find("dtend") and end_date ~= "present" then
			return error_category
		end
	end
end

local function replace_space(value)
	if value then
		return value:gsub(" ", " ")
	end
	return value
end

function p.dates(frame)
	local args = getArgs(frame)
	
	-- Handle missing arguments cases
	if table.getn(args) < 2 then
		if args['1'] == nil and args['2'] == nil then
			return ''
		elseif args['1'] == nil then 
			return args['2']
		elseif args['2'] == nil then 
			return args['1']
		end
	end
	
	-- Try to parse dates
	local pr1, m1, d1, y1, su1, pr2, m2, d2, y2, su2
	local dmy = false
	
	-- Try MDY format first
	pr1, m1, d1, y1, su1 = string.match(args['1'], '(.-)(%u%a+)%s(%d+),%s(%d+)(.*)')
	pr2, m2, d2, y2, su2 = string.match(args['2'], '(.-)(%u%a+)%s(%d+),%s(%d+)(.*)')
	
	-- If MDY failed, try DMY format
	if y1 == nil then
		dmy = true
		pr1, d1, m1, y1, su1 = string.match(args['1'], '(.-)(%d%d?)%s(%a+)%s(%d+)(.*)')
		pr2, d2, m2, y2, su2 = string.match(args['2'], '(.-)(%d%d?)%s(%a+)%s(%d+)(.*)')
	end
	
	-- Clean up spaces
	d1, m1, y1 = replace_space(d1), replace_space(m1), replace_space(y1)
	d2, m2, y2 = replace_space(d2), replace_space(m2), replace_space(y2)
	
	-- Set default empty strings for suffixes
	su1, su2 = su1 or '', su2 or ''
	pr1, pr2 = pr1 or '', pr2 or ''
	
	-- Handle unparsable dates or fallback
	if y1 == nil or y2 == nil then
		-- Line break for all but year-year ranges
		local dash = '&nbsp;–<br />'
		return args['1'] .. dash .. args['2']
	end
	
	-- Validate date range
	local MONTHS = {January=1, February=2, March=3, April=4, May=5, June=6, 
	                July=7, August=8, September=9, October=10, November=11, December=12}
	
	-- Check if both dates have valid months
	if not MONTHS[m1] or not MONTHS[m2] then
		local dash = '&nbsp;–<br />'
		return args['1'] .. dash .. args['2']
	end
	
	local diff = os.time({year=y2, month=MONTHS[m2], day=d2 or 1, hour=0, min=0, sec=0}) - 
	            os.time({year=y1, month=MONTHS[m1], day=d1 or 1, hour=0, min=0, sec=0})
	
	if diff < 0 then
		return 'Invalid date range'
	end
	
	-- Format the output based on date pattern
	local returnval
	
	-- Determine if we have a year-year, month-month, or day-specific range
	local hasDay1 = d1 ~= nil and d1 ~= ""
	local hasDay2 = d2 ~= nil and d2 ~= ""
	local hasMonth1 = m1 ~= nil and m1 ~= ""
	local hasMonth2 = m2 ~= nil and m2 ~= ""
	
	-- Choose appropriate dash format
	local dash = '&nbsp;–<br />' -- Default with line break
	
	-- Year-year range (no line break)
	if not hasDay1 and not hasDay2 and not hasMonth1 and not hasMonth2 then
		dash = '&nbsp;–'
	end
	
	-- Format based on type of range and date style (DMY vs MDY)
	if y1 == y2 then
		-- Same year
		if m1 == m2 then
			-- Same month: day-day
			if dmy then
				returnval = pr1 .. d1 .. '–' .. d2 .. ' ' .. m1 .. ' ' .. y1 .. su2
			else
				returnval = pr1 .. m1 .. ' ' .. d1 .. '–' .. d2 .. ', ' .. y1 .. su2
			end
		else
			-- Different months, same year: specific dates or month-month
			if hasDay1 and hasDay2 then
				-- Between specific dates in different months
				if dmy then
					returnval = pr1 .. d1 .. ' ' .. m1 .. dash .. d2 .. ' ' .. m2 .. ' ' .. y1 .. su2
				else
					returnval = pr1 .. m1 .. ' ' .. d1 .. dash .. m2 .. ' ' .. d2 .. ', ' .. y1 .. su2
				end
			else
				-- Month-month
				returnval = pr1 .. m1 .. '–' .. m2 .. ' ' .. y1 .. su2
			end
		end
	else
		-- Different years
		if hasDay1 and hasDay2 then
			-- Between specific dates in different years
			if dmy then
				returnval = pr1 .. d1 .. ' ' .. m1 .. ' ' .. y1 .. su1 .. dash .. d2 .. ' ' .. m2 .. ' ' .. y2 .. su2
			else
				returnval = pr1 .. m1 .. ' ' .. d1 .. ', ' .. y1 .. su1 .. dash .. m2 .. ' ' .. d2 .. ', ' .. y2 .. su2
			end
		else if hasMonth1 and hasMonth2 then
			-- Between months in different years
			returnval = pr1 .. m1 .. ' ' .. y1 .. su1 .. dash .. m2 .. ' ' .. y2 .. su2
		else
			-- Year-year (no line break)
			returnval = pr1 .. y1 .. su1 .. '&nbsp;–' .. y2 .. su2
		end
	end
	
	mw.log(returnval)
	return returnval
end

return p

end