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 17:10, 7 April 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Module for formatting date ranges according to [[MOS:DATERANGE]].
local p = {}

local getArgs = require('Module:Arguments').getArgs

-- Define constants for reuse throughout the module
local DASH = '–'
local DASH_BREAK = '&nbsp;–<br />'
local DEFAULT_ERROR_CATEGORY = 'Pages with incorrectly formatted date ranges'

-- Template should be moved eventually to the infobox television season module.
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

-- =============================================
-- Helper functions
-- =============================================

-- Replace non-breaking spaces with regular spaces.
local function replace_space(value)
	if value then
		return value:gsub("&nbsp;", " ")
	end
	return value
end

-- Extract the hidden span portion from text if it exists.
local function extract_span(text)
	local span_start = string.find(text, "<span")
	if span_start then
		return string.sub(text, span_start)
	end
	return ""
end

-- Extract visible part (before any span).
local function extract_visible(text)
	return text:match("^(.-)<span") or text
end

-- Parse date components from visible text.
local function parse_date(visibleText)
	local date_format = "mdy" -- Default format
	
	-- Try MDY format first (e.g., "January 15, 2020")
	local prefix, month, day, year, suffix = string.match(visibleText, '(.-)(%u%a+)%s(%d+),%s(%d+)(.*)')
	
	-- If MDY failed, try DMY format (e.g., "15 January 2020")
	if year == nil then
		date_format = "dmy"
		prefix, day, month, year, suffix = string.match(visibleText, '(.-)(%d%d?)%s(%u%a+)%s(%d+)(.*)')
	end
	
	-- If month and year only (e.g., "April 2015")
	if year == nil then
		month, year = visibleText:match('(%u%a+)%s(%d%d%d%d)')
		prefix, suffix, day = "", "", nil
	end
	
	-- If year only (e.g., "2015")
	if year == nil then
		year = visibleText:match('(%d%d%d%d)')
		prefix, suffix, month, day = "", "", nil, nil
	end
	
	-- Handle "present" case
	if visibleText:find("present") then
		year = "present"
		prefix, suffix, month, day = "", "", nil, nil
	end
	
	-- Set default empty strings for optional components
	suffix = suffix or ''
	prefix = prefix or ''
	
	return {
		prefix = prefix,
		month = month,
		day = day,
		year = year,
		suffix = suffix,
		format = date_format
	}
end

-- Get month number from name.
local function get_month_number(month_name)
	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
	}
	return month_name and MONTHS[month_name]
end

-- Format date range for same year according to Wikipedia style.
local function format_same_year(date1, date2, span1, span2)
	-- Both dates have just year, no month or day
	if date1.month == nil and date2.month == nil then
		return date1.prefix .. date1.year .. span1 .. DASH .. date2.year .. span2
	end
	
	-- Both dates have month and year, but no day
	if date1.day == nil and date2.day == nil then
		return date1.prefix .. date1.month .. span1 .. DASH .. date2.month .. ' ' .. date1.year .. span2
	end
	
	-- Same month and year
	if date1.month == date2.month then
		if date1.format == "dmy" then
			-- Format: d1–d2 m1 y1 (5–7 January 1979)
			return date1.prefix .. date1.day .. span1 .. DASH .. date2.day .. ' ' .. date1.month .. ' ' .. date1.year .. span2
		else
			-- Format: m1 d1–d2, y1 (January 5–7, 1979)
			return date1.prefix .. date1.month .. ' ' .. date1.day .. span1 .. DASH .. date2.day .. ', ' .. date1.year .. span2
		end
	else
		-- Different months, same year
		if date1.format == "dmy" then
			-- Format: d1 m1 – d2 m2 y1 (3 June –<br/> 18 August 1952)
			return date1.prefix .. date1.day .. ' ' .. date1.month .. span1 .. DASH_BREAK .. date2.day .. ' ' .. date2.month .. ' ' .. date1.year .. span2
		else
			-- Format: m1 d1 – m2 d2, y1 (June 3 –<br/> August 18, 1952)
			return date1.prefix .. date1.month .. ' ' .. date1.day .. span1 .. DASH_BREAK .. date2.month .. ' ' .. date2.day .. ', ' .. date1.year .. span2
		end
	end
end

-- Format date range with "present" as the end date
local function format_present_range(date1, span1)
	-- Year only
	if date1.month == nil then
		return date1.prefix .. date1.year .. span1 .. DASH .. "present"
	end
	
	-- Month and year, no day
	if date1.day == nil then
		return date1.prefix .. date1.month .. ' ' .. date1.year .. span1 .. " " .. DASH .. " present"
	end
	
	-- Full date (with line break)
	if date1.format == "dmy" then
		return date1.prefix .. date1.day .. ' ' .. date1.month .. ' ' .. date1.year .. span1 .. DASH_BREAK .. "present"
	else
		return date1.prefix .. date1.month .. ' ' .. date1.day .. ', ' .. date1.year .. span1 .. DASH_BREAK .. "present"
	end
end

-- Format date range for different years.
local function format_different_years(date1, date2, visible_text1, visible_text2, span1, span2)
	-- If both entries are just years, use simple dash without line break
	if date1.month == nil and date2.month == nil then
		return visible_text1 .. span1 .. DASH .. visible_text2 .. span2
	end
	
	-- If one of them has a month or day, use dash with line break
	return visible_text1 .. span1 .. DASH_BREAK .. visible_text2 .. span2
end

-- =============================================
-- Main function
-- =============================================

-- Format date ranges according to Wikipedia style.
-- @param frame Frame object from Wikipedia
-- @return Formatted date range string
function p.dates(frame)
	local args = getArgs(frame)

	-- Handle missing arguments cases
	if #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

	-- Get spans from original inputs
	local span1 = extract_span(args[1])
	local span2 = extract_span(args[2])

	-- Get visible parts only
	local visible_text1 = extract_visible(args[1])
	local visible_text2 = extract_visible(args[2])

	-- Clean up spaces
	visible_text1 = replace_space(visible_text1)
	visible_text2 = replace_space(visible_text2)

	-- Parse dates
	local date1 = parse_date(visible_text1)
	local date2 = parse_date(visible_text2)

	-- Handle unparsable dates (fallback to original format)
	if date1.year == nil or (date2.year == nil and not string.find(visible_text2, "present")) then
		return args[1] .. DASH .. args[2]
	end

	-- Handle "present" as end date
	if visible_text2:find("present") or date2.year == "present" then
		return format_present_range(date1, span1)
	end
	
	-- Validate date range if both have months (skip validation if one is just a year)
	if date1.month and date2.month then
		local month1_number = get_month_number(date1.month)
		local month2_number = get_month_number(date2.month)
		
		-- If invalid month names, return original format
		if not month1_number or not month2_number then
			return args[1] .. DASH .. args[2]
		end
		
		-- Calculate time difference (use day 1 if day is not specified)
		local time1 = os.time({
			year = tonumber(date1.year),
			month = month1_number,
			day = tonumber(date1.day or 1),
			hour = 0, min = 0, sec = 0
		})
		
		local time2 = os.time({
			year = tonumber(date2.year),
			month = month2_number,
			day = tonumber(date2.day or 1),
			hour = 0, min = 0, sec = 0
		})
		
		if time1 > time2 then
			return 'Invalid date range'
		end
	end
	
	-- Format based on whether years are the same
	if date1.year == date2.year then
		return format_same_year(date1, date2, span1, span2)
	else
		-- Different years
		return format_different_years(date1, date2, visible_text1, visible_text2, span1, span2)
	end
end

return p