Module:Sandbox/Hellknowz/Test: Difference between revisions

Content deleted Content added
add other 3 combos
validations
Line 140:
return nil
 
end
 
function checkIfDayValid(day, month)
-- First check that the month can have at least this many days
if (day > monthDays[month]) then return false 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
if (hour >= 24) then return nil endfalse
end
end
 
return true
end
 
function checkIfMonthValid(year)
return month ~= 0 and month <= 12 -- <0 never happens with [0-9] pattern
end
 
function checkIfYearValid(year)
return year >= 1583 -- up to 9999
end
 
function checkIfYearValid(hour)
return hour < 24 -- <0 never happens with [0-9] pattern
end
 
function checkIfMinuteValid(minute)
return minute < 60 -- <0 never happens with [0-9] pattern
end
 
function checkIfSecondValid(second)
return second < 60 -- <0 never happens with [0-9] pattern
end
 
Line 146 ⟶ 180:
local input = frame.args[1]
 
-- First, try to get a date and time
local day, month, year, hour, minute, second = tryParseDateTime(input)
 
-- If we have a second, we have all 6
if (second) then
 
if (not checkIfYearValid(year) or not checkIfMonthValid(month) or not checkIfDayValid(day, month) or not checkIfHourValid(hour) or not checkIfMinuteValid(minute) or not checkIfSecondValid(second)) then return nil end
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)
Line 158 ⟶ 192:
end
 
-- Second, try to get just the date
day, month, year = tryParseDateOnly(input)
 
if-- (notNo year), thenno date
if (not year) then return nil end --input .. ' -> is not a recognized pattern match' end
 
if (year and not month) then
Line 167 ⟶ 202:
-- year only
 
if (not checkIfYearValid(year < 1760)) then -- TODO: make this a validateDate orreturn somethingnil functionend
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
Line 181 ⟶ 212:
-- month and year only
 
if (yearnot < 1583checkIfYearValid(year)) then return nil end
-- Month always comes from month name pattern string, so always valid, can skip the check
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)
Line 193 ⟶ 219:
end
 
if (not checkIfYearValid(year) or not checkIfMonthValid(month) or not checkIfDayValid(day, month) or not checkIfHourValid(hour)) then return nil 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)