Module:Infobox/dates/sandbox
Appearance
< Module:Infobox | dates
![]() | This is the module sandbox page for Module:Infobox/dates (diff). See also the companion subpage for test cases (run). |
![]() | This module depends on the following other modules: |
Usage
{{#invoke:infobox/dates|dates}}
- formats the date range.{{#invoke:infobox/dates|start_end_date_template_validation}}
- checks if the values of|first_aired=
,|released=
,|aired=
,|released_date=
are not passed via{{Start date}}
and if the value of|last_aired=
is not passed via{{End date}}
(or is not|last_aired=present
, where relevant). If they aren't, the function returns the default error category, Category:Pages using infobox television with nonstandard dates or the error category from|error_category=
if used.
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
-- Extract the hidden span portion 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
-- Get spans from original inputs
local span1 = extract_span(args['1'])
local span2 = extract_span(args['2'])
-- Get visible parts only
local visible1 = extract_visible(args['1'])
local visible2 = extract_visible(args['2'])
-- Clean up spaces
visible1 = replace_space(visible1)
visible2 = replace_space(visible2)
-- Try to parse dates from visible text
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(visible1, '(.-)(%u%a+)%s(%d+),%s(%d+)(.*)')
pr2, m2, d2, y2, su2 = string.match(visible2, '(.-)(%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(visible1, '(.-)(%d%d?)%s(%a+)%s(%d+)(.*)')
pr2, d2, m2, y2, su2 = string.match(visible2, '(.-)(%d%d?)%s(%a+)%s(%d+)(.*)')
end
-- Set default empty strings for suffixes
su1, su2 = su1 or '', su2 or ''
pr1, pr2 = pr1 or '', pr2 or ''
local dash = ' –<br />'
-- Handle unparsable dates or fallback
if y1 == nil or y2 == nil then
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
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 based on the rules at [[MOS:DATERANGE]], but use special handling for same year cases
if y1 == y2 then
-- Same year
if m1 == m2 then
-- Same month and year
if dmy then
-- Foramt: d1–d2 m1 y1 (5–7 January 1979)
return pr1 .. d1 .. span1 .. '–' .. d2 .. ' ' .. m1 .. ' ' .. y1 .. span2
else
-- Foramt: m1 d1–d2, y1 (January 5–7, 1979)
return pr1 .. m1 .. ' ' .. d1 .. span1 .. '–' .. d2 .. ', ' .. y1 .. span2
end
else
-- Different months, same year
if dmy then
-- Format: d1 m1 – d2 m2 y1 (3 June –<br/> 18 August 1952)
return pr1 .. d1 .. ' ' .. m1 .. span1 .. dash .. d2 .. ' ' .. m2 .. ' ' .. y1 .. span2
else
-- Format: m1 d1 – m2 d2, y1 (June 3 –<br/> August 18, 1952)
return pr1 .. m1 .. ' ' .. d1 .. span1 .. dash .. m2 .. ' ' .. d2 .. ', ' .. y1 .. span2
end
end
else
-- Different years - use the full original inputs with dash
return args['1'] .. dash .. args['2']
end
end
return p