Anonymous user
Module:Citation/CS1/Date validation: Difference between revisions
Module:Citation/CS1/Date validation (view source)
Revision as of 05:16, 12 December 2015
, 8 years agoSynch from sandbox;
(Synch from sandbox;) |
(Synch from sandbox;) |
||
Line 97:
return true;
end
--[[--------------------------< I S _ V A L I D _ Y E A R >----------------------------------------------------
Line 112 ⟶ 111:
end
--[[--------------------------< I S _ V A L I D _ D A T E >----------------------------------------------------
Returns true if day is less than or equal to the number of days in month and year is no farther into the future
than next year; else returns false.
Assumes Julian calendar prior to year 1582 and Gregorian calendar thereafter. Accounts for Julian calendar leap
years before 1582 and Gregorian leap years after 1582. Where the two calendars overlap (1582 to approximately
▲Assumes Julian calendar prior to year 1582 and Gregorian calendar thereafter. Accounts for Julian calendar leap years before 1582 and Gregorian leap years after 1582.
▲Where the two calendars overlap (1582 to approximately 1923) dates are assumed to be Gregorian.
]]
local function is_valid_date (year, month, day)
local days_in_month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
local month_length;
if not is_valid_year(year) then -- no farther into the future than next year
return false;
end
month_length = 28; -- then 28 days unless▼
▲ month_length = 28; -- then 28 days unless
if 1582 > tonumber(year) then -- Julian calendar
if 0==(year%4) then
month_length = 29;
end
else -- Gregorian calendar
if (0==(year%4) and (0~=(year%100) or 0==(year%400))) then -- is a leap year?
month_length = 29; -- if leap year then 29 days in February
end
end
else
month_length=days_in_month[
end
Line 213 ⟶ 218:
This function receives a table of date parts for one or two dates and an empty table reference declared in
Module:Citation/CS1. The function is called only for |date= parameters and only if the |date=<value> is
determined to be a valid date format. The question of what to do with
The date parts in the input table are converted to an ISO 8601 conforming date string:
Line 296 ⟶ 301:
--[[--------------------------< C H E C K _ D A T E >----------------------------------------------------------
Check date format to see that it is one of the formats approved by WP:DATESNO or WP:DATERANGE. Exception: only
allowed range separator is endash. Additionally, check the date to see that it is a real date: no 31 in 30-day
months; no 29 February when not a leap year. Months, both long-form and three character abbreviations, and seasons
If the date fails the format tests, this function returns false and does not return values for anchor_year and
COinS_date. When this happens, the date parameter is used in the COinS metadata and the CITEREF identifier gets
Inputs:
Line 310 ⟶ 317:
true, anchor_year, COinS_date
anchor_year can be used in CITEREF anchors
COinS_date is ISO 8601 format date; see make_COInS_date()
]]
local function check_date (date_string, tCOinS_date)
local year; -- assume that year2, months, and days are not used;
Line 415 ⟶ 423:
end
elseif date_string:match ("^%a+–%a+ +[1-9]%d%d%d%a?$") then
month, month2, anchor_year, year=date_string:match ("(%a+)–(%a+)%s*((%d%d%d%d)%a?)");
if (not is_valid_month_season_range(month, month2)) or (not is_valid_year(year)) then return false; end
Line 461 ⟶ 469:
else
return false; -- date format not one of the MOS:DATE approved formats
end
local result=true; -- check whole dates for validity; assume true because not all dates will go through this test
if 0 ~= year and 0 ~= month and 0 ~= day and 0 == year2 and 0 == month2 and 0 == day2 then -- YMD (simple whole date)
result=is_valid_date(year,month,day);
Line 482 ⟶ 490:
if false == result then return false; end
if nil ~= tCOinS_date then -- this table only passed into this function when testing |date= parameter values
Line 490 ⟶ 495:
end
return true, anchor_year; -- format is good and date string represents a real date
end
--[[--------------------------< D A T E S >--------------------------------------------------------------------
Line 509 ⟶ 514:
local COinS_date; -- will return as nil if the date being tested is not |date=
local error_message = "";
local good_date = false;
for k, v in pairs(date_parameters_list) do
if is_set(v) then
if v:match("^c%. [1-9]%d%d%d?%a?$") then
local year = v:match("c%. ([1-9]%d%d%d?)%a?");
if 'date'==k then
anchor_year, COinS_date = v:match("((c%. [1-9]%d%d%d?)%a?)"); -- anchor year and COinS_date only from |date= parameter
Line 522 ⟶ 526:
good_date = is_valid_year(year);
end
elseif 'date'==k then
if v:match("^n%.d%.%a?") then
good_date, anchor_year, COinS_date = true, v:match("((n%.d%.)%a?)"); --"n.d."; no error when date parameter is set to no date
elseif v:match("^nd%a?$") then
good_date, anchor_year, COinS_date = true, v:match("((nd)%a?)"); --"nd"; no error when date parameter is set to no date
else
good_date, anchor_year, COinS_date = check_date (v, tCOinS_date);
end
elseif 'access-date'==k then
good_date = check_date (v);
if true == good_date then
good_date = is_valid_accessdate (v);
end
else
good_date = check_date (v);
end
if false==good_date then
if is_set(error_message) then
error_message=error_message .. ", ";
end
error_message=error_message .. "|" .. k .. "=";
end
end
end
return anchor_year, error_message; -- and done
end
--[[--------------------------< Y E A R _ D A T E _ C H E C K >------------------------------------------------
|