Module:Infobox/dates/sandbox: Difference between revisions
Appearance
< Module:Infobox | dates
Content deleted Content added
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
-- Module for formatting date ranges according to [[MOS:DATERANGE]]. |
|||
local p = {} |
|||
local getArgs = require('Module:Arguments').getArgs |
local getArgs = require('Module:Arguments').getArgs |
||
-- Define constants for reuse throughout the module |
|||
local default_error_category = "[[Category:Pages using infobox television with nonstandard dates]]" |
|||
local DASH = '–' |
|||
local |
local DASH_BREAK = ' –<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) |
function p.start_end_date_template_validation(frame) |
||
local args = getArgs(frame) |
local args = getArgs(frame) |
||
local error_category = args.error_category or |
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 |
local start_date = args.first_aired or args.released or args.airdate or args.release_date or args.airdate_overall |
||
Line 24: | Line 29: | ||
end |
end |
||
-- ============================================= |
|||
-- Helper functions |
|||
-- ============================================= |
|||
-- Replace non-breaking spaces with regular spaces. |
|||
local function replace_space(value) |
local function replace_space(value) |
||
if value then |
if value then |
||
Line 31: | Line 41: | ||
end |
end |
||
-- Extract the hidden span portion from text if it exists. |
|||
function p.dates(frame) |
|||
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") |
|||
-- Handle missing arguments cases |
|||
local prefix, month, day, year, suffix = string.match(visibleText, '(.-)(%u%a+)%s(%d+),%s(%d+)(.*)') |
|||
if table.getn(args) < 2 then |
|||
if args['1'] == nil and args['2'] == nil then |
|||
-- If MDY failed, try DMY format (e.g., "15 January 2020") |
|||
return '' |
|||
if year == nil then |
|||
date_format = "dmy" |
|||
return args['2'] |
|||
prefix, day, month, year, suffix = string.match(visibleText, '(.-)(%d%d?)%s(%u%a+)%s(%d+)(.*)') |
|||
elseif args['2'] == nil then |
|||
return args['1'] |
|||
end |
|||
end |
end |
||
-- If month and year only (e.g., "April 2015") |
|||
-- Extract the hidden span portion if it exists |
|||
if year == nil then |
|||
local function extract_span(text) |
|||
month, year = visibleText:match('(%u%a+)%s(%d%d%d%d)') |
|||
local span_start = string.find(text, "<span") |
|||
prefix, suffix, day = "", "", nil |
|||
if span_start then |
|||
return string.sub(text, span_start) |
|||
end |
|||
return "" |
|||
end |
end |
||
-- |
-- If year only (e.g., "2015") |
||
if year == nil then |
|||
local function extract_visible(text) |
|||
year = visibleText:match('(%d%d%d%d)') |
|||
prefix, suffix, month, day = "", "", nil, nil |
|||
end |
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 |
local MONTHS = { |
||
January = 1, February = 2, March = 3, April = 4, |
|||
May = 5, June = 6, July = 7, August = 8, |
|||
-- Get visible parts only |
|||
September = 9, October = 10, November = 11, December = 12 |
|||
local visible1 = extract_visible(args['1']) |
|||
} |
|||
local visible2 = extract_visible(args['2']) |
|||
return month_name and MONTHS[month_name] |
|||
end |
|||
-- Format date range for same year according to Wikipedia style. |
|||
-- Clean up spaces |
|||
local function format_same_year(date1, date2, span1, span2) |
|||
visible1 = replace_space(visible1) |
|||
-- Both dates have just year, no month or day |
|||
visible2 = replace_space(visible2) |
|||
if date1.month == nil and date2.month == nil then |
|||
return date1.prefix .. date1.year .. span1 .. DASH .. date2.year .. span2 |
|||
-- Try to parse dates from visible text |
|||
end |
|||
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+)(.*)') |
|||
-- Both dates have month and year, but no day |
|||
-- If MDY failed, try DMY format |
|||
if |
if date1.day == nil and date2.day == nil then |
||
return date1.prefix .. date1.month .. span1 .. DASH .. date2.month .. ' ' .. date1.year .. span2 |
|||
dmy = true |
|||
pr1, d1, m1, y1, su1 = string.match(visible1, '(.-)(%d%d?) %a+ %d+ (.*)') |
|||
pr2, d2, m2, y2, su2 = string.match(visible2, '(.-)(%d%d?) %a+ %d+ (.*)') |
|||
end |
end |
||
-- |
-- Same month and year |
||
if |
if date1.month == date2.month then |
||
if date1.format == "dmy" then |
|||
m1, y1 = visible1:match('(%a+) (%d%d%d%d)') |
|||
-- Format: d1–d2 m1 y1 (5–7 January 1979) |
|||
pr1, su1 = "", "" -- Ensure placeholders remain valid |
|||
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 |
||
end |
|||
-- Format date range with "present" as the end date |
|||
if y2 == nil then |
|||
local function format_present_range(date1, span1) |
|||
m2, y2 = visible2:match('(%a+) (%d%d%d%d)') |
|||
-- Year only |
|||
pr2, su2 = "", "" |
|||
if date1.month == nil then |
|||
return date1.prefix .. date1.year .. span1 .. DASH .. "present" |
|||
end |
end |
||
-- Month and year, no day |
|||
-- If both MDY and DMY fail, check for a 4-digit year format |
|||
if |
if date1.day == nil then |
||
return date1.prefix .. date1.month .. ' ' .. date1.year .. span1 .. DASH .. "present" |
|||
y1 = visible1:match('(%d%d%d%d)') |
|||
pr1, su1 = "", "" -- Set placeholders for consistency |
|||
end |
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 |
|||
if y2 == nil then |
|||
local function format_different_years(visible_text1, visible_text2, span1, span2) |
|||
y2 = visible2:match('(%d%d%d%d)') |
|||
return visible_text1 .. span1 .. DASH_BREAK .. visible_text2 .. span2 |
|||
pr2, su2 = "", "" |
|||
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 |
end |
||
-- Get spans from original inputs |
|||
-- Set default empty strings for suffixes |
|||
local span1 = extract_span(args[1]) |
|||
su1, su2 = su1 or '', su2 or '' |
|||
local span2 = extract_span(args[2]) |
|||
pr1, pr2 = pr1 or '', pr2 or '' |
|||
local dash_break = ' –<br />' |
|||
local dash = '–' |
|||
-- Get visible parts only |
|||
-- Handle unparsable dates or fallback |
|||
local visible_text1 = extract_visible(args[1]) |
|||
if y1 == nil or y2 == nil then |
|||
local visible_text2 = extract_visible(args[2]) |
|||
return args['1'] .. dash .. 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 |
end |
||
-- Handle "present" as end date |
|||
-- Validate date range |
|||
if visible_text2:find("present") or date2.year == "present" then |
|||
local MONTHS = {January=1, February=2, March=3, April=4, May=5, June=6, |
|||
return format_present_range(date1, span1) |
|||
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 |
end |
||
-- Validate date range if both have months (skip validation if one is just a year) |
|||
local diff = os.time({year=y2, month=MONTHS[m2], day=d2 or 1, hour=0, min=0, sec=0}) - |
|||
if date1.month and date2.month then |
|||
os.time({year=y1, month=MONTHS[m1], day=d1 or 1, hour=0, min=0, sec=0}) |
|||
local month1_number = get_month_number(date1.month) |
|||
local month2_number = get_month_number(date2.month) |
|||
if diff < 0 then |
|||
return 'Invalid date range' |
|||
-- 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 |
end |
||
-- Format based on |
-- Format based on whether years are the same |
||
if |
if date1.year == date2.year then |
||
return format_same_year(date1, date2, span1, span2) |
|||
-- Same year |
|||
if d1 == nil and d2 == nil then |
|||
-- Foramt: m1–m2 y1 (May–July 1940) |
|||
return pr1 .. m1 .. span1 .. dash .. m2 .. ' ' .. y1 .. span2 |
|||
end |
|||
if m1 == m2 then |
|||
-- Same month and year |
|||
if dmy then |
|||
-- Foramt: d1–d2 m1 y1 (5–7 January 1979) |
|||
return pr1 .. d1 .. span1 .. dash .. d2 .. ' ' .. m1 .. ' ' .. y1 .. span2 |
|||
else |
|||
-- Foramt: m1 d1–d2, y1 (January 5–7, 1979) |
|||
return pr1 .. m1 .. ' ' .. d1 .. span1 .. dash .. 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_break .. d2 .. ' ' .. m2 .. ' ' .. y1 .. span2 |
|||
else |
|||
-- Format: m1 d1 – m2 d2, y1 (June 3 –<br/> August 18, 1952) |
|||
return pr1 .. m1 .. ' ' .. d1 .. span1 .. dash_break .. m2 .. ' ' .. d2 .. ', ' .. y1 .. span2 |
|||
end |
|||
end |
|||
else |
else |
||
-- Different years |
-- Different years |
||
return format_different_years(visible_text1, visible_text2, span1, span2) |
|||
end |
end |
||
end |
end |
Revision as of 16:43, 7 April 2025
![]() | 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.
-- 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 = ' –<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(" ", " ")
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(visible_text1, visible_text2, span1, span2)
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(visible_text1, visible_text2, span1, span2)
end
end
return p