local main = {};
local monthIndices = {
['january'] = 1,
['february'] = 2,
['march'] = 3,
['april'] = 4,
['may'] = 5,
['june'] = 6,
['july'] = 7,
['august'] = 8,
['september'] = 9,
['october'] = 10,
['november'] = 11,
['december'] = 12
}
local monthDays = {
[1] = 31,
[2] = 29, -- will check below
[3] = 31,
[4] = 30,
[5] = 31,
[6] = 30,
[7] = 31,
[8] = 31,
[9] = 30,
[10] = 31,
[11] = 30,
[12] = 31
}
function tryParseDateTime(input)
--1 May 1973, 10:38:27
--10:38:27, 1 May 1973
--10:38am
--10am
local matchDay, matchMonth, matchYear
local matchHour, matchMinute, matchSecond
-- Try dMy hms
matchDay, matchMonth, matchYear, matchHour, matchMinute, matchSecond = input:match('^([0-9][0-9]?) ([A-Za-z]+) ([0-9][0-9][0-9][0-9]),? ([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)$')
if (matchDay) then
local month = monthIndices[matchMonth:lower()]
if (month == nil) then return nil end
return tonumber(matchDay), month, tonumber(matchYear), tonumber(matchHour), tonumber(matchMinute), tonumber(matchSecond)
end
-- Try Mdy hms
matchMonth, matchDay, matchYear, matchHour, matchMinute, matchSecond = input:match('^([A-Za-z]+) ([0-9][0-9]?),? ([0-9][0-9][0-9][0-9]),? ([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?)$')
if (matchDay) then
local month = monthIndices[matchMonth:lower()]
if (month == nil) then return nil end
return tonumber(matchDay), month, tonumber(matchYear), tonumber(matchHour), tonumber(matchMinute), tonumber(matchSecond)
end
-- Try hms dMy
matchHour, matchMinute, matchSecond, matchDay, matchMonth, matchYear = input:match('^([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?),? ([0-9][0-9]?) ([A-Za-z]+) ([0-9][0-9][0-9][0-9])$')
if (matchDay) then
local month = monthIndices[matchMonth:lower()]
if (month == nil) then return nil end
return tonumber(matchDay), month, tonumber(matchYear), tonumber(matchHour), tonumber(matchMinute), tonumber(matchSecond)
end
-- Try hms Mdy
matchHour, matchMinute, matchSecond, matchMonth, matchDay, matchYear = input:match('^([0-9][0-9]?):([0-9][0-9]?):([0-9][0-9]?),? ([A-Za-z]+) ([0-9][0-9]?),? ([0-9][0-9][0-9][0-9])$')
if (matchDay) then
local month = monthIndices[matchMonth:lower()]
if (month == nil) then return nil end
return tonumber(matchDay), month, tonumber(matchYear), tonumber(matchHour), tonumber(matchMinute), tonumber(matchSecond)
end
-- todo others
-- Well, we ran out of valid date formats
return nil
end
function tryParseDateOnly(input)
local matchDay, matchMonth, matchYear
-- Try Y
matchYear = input:match('^([0-9][0-9][0-9][0-9])$')
if (matchYear) then
return nil, nil, tonumber(matchYear)
end
-- Try My
matchMonth, matchYear = input:match('^([A-Za-z]+) ([0-9][0-9][0-9][0-9])$')
if (matchMonth) then
local month = monthIndices[matchMonth:lower()]
if (month == nil) then return nil end
return nil, month, tonumber(matchYear)
end
-- Try dMy
matchDay, matchMonth, matchYear = input:match('^([0-9][0-9]?) ([A-Za-z]+) ([0-9][0-9][0-9][0-9])$')
if (matchDay) then
local month = monthIndices[matchMonth:lower()]
if (month == nil) then return nil end
return tonumber(matchDay), month, tonumber(matchYear)
end
-- Try Mdy
matchMonth, matchDay, matchYear = input:match('^([A-Za-z]+) ([0-9][0-9]?),? ([0-9][0-9][0-9][0-9])$')
if (matchDay) then
local month = monthIndices[matchMonth:lower()]
if (month == nil) then return nil end
return tonumber(matchDay), month, tonumber(matchYear)
end
-- Try ymd
matchYear, matchMonth, matchDay = input:match('^([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])$')
if (matchDay) then
return tonumber(matchDay), tonumber(matchMonth), tonumber(matchYear)
end
-- Well, we ran out of valid date formats
return nil
end
function main.parseDate(frame)
local input = frame.args[1]
local day, month, year, hour, minute, second = tryParseDateTime(input)
if (second) then
if (second >= 60) then return nil end
if (minute >= 60) then return nil end
if (hour >= 24) then return nil end
return string.format("%d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second)
end
day, month, year = tryParseDateOnly(input)
if (not year) then
return nil end --input .. ' -> is not a recognized pattern match' end
if (year and not month) then
-- year only
if (year < 1760) then -- TODO: make this a validateDate or something function
return nil end --input .. ' -> year is too early (calendar discrepancy)' end
--if (year > 2100) then
-- return nil end --input .. ' -> year is too far into future' end
return year
end
if (year and month and not day) then
-- month and year only
if (year < 1583) then
return nil end --input .. ' -> year is too early (calendar discrepancy)' end
--if (year > 2100) then
-- return nil end --input .. ' -> year is too far into future' end
-- Month comes from month name string, so always valid, can skip the check
return string.format("%d-%02d", year, month)
end
if (year < 1583) then
return nil end --input .. ' -> year is too early (calendar discrepancy)' end
--if (year > 2100) then
-- return nil end --input .. ' -> year is too far into future' end
if (month == 0) then
return nil end --input .. ' -> month is invalid == 0' end
if (month > 12) then
return nil end --input .. ' -> month is invalid > 12' end
if (day == 0) then
return nil end --input .. ' -> day is invalid == 0' end
if (day > monthDays[month]) then
return nil end --input .. ' -> day is invalid > ' .. monthDays[month] end
-- February leap year check
if (month == 2) then
if (day == 29 and not ((year % 4 == 0) and (year % 100 ~= 0) or (year % 400 == 0))) then
return nil --input .. ' -> day is invalid > 28'
end
end
--parent stuff (infobox itself)
--local parent = frame:getParent()
--local parentArgs = parent.args
return string.format("%d-%02d-%02d", year, month, day)
end
function main.emitMetadata(frame)
-- First parse the date and see if we get a valid output date
local date = main.parseDate(frame)
if (not date) then return nil end
local spanClass = frame.args.spanClass or 'bday dtstart published updated'
return '<span style="display:none"> (<span class="' .. spanClass .. '">' .. date .. '</span>)</span>'
end
return main