Module:Citation/CS1/Date validation: Difference between revisions
Synch from sandbox;
m>Trappist the monk (Synch from sandbox;) |
m>Trappist the monk (Synch from sandbox;) |
||
Line 21: | Line 21: | ||
adding 24 hours gives 2015-01-03T00:00:00 – one second more than tomorrow | adding 24 hours gives 2015-01-03T00:00:00 – one second more than tomorrow | ||
This function does not work for languages other than English. Wikimedia #time: parser apparently doesn't understand | This function does not work if it is fed month names for languages other than English. Wikimedia #time: parser | ||
non-Engish date month names. This function will always return false when the date contains a non-English month name | apparently doesn't understand non-Engish date month names. This function will always return false when the date | ||
because good1 is false after the call to lang.formatDate. | contains a non-English month name because good1 is false after the call to lang.formatDate(). To get around that | ||
call this function with YYYY-MM-DD format dates. | |||
]=] | ]=] | ||
Line 44: | Line 42: | ||
return false; -- one or both failed to convert to unix time stamp | return false; -- one or both failed to convert to unix time stamp | ||
end | end | ||
if 979516800 <= access_ts and access_ts < tomorrow_ts then -- Wikipedia start date <= accessdate < tomorrow's date | if 979516800 <= access_ts and access_ts < tomorrow_ts then -- Wikipedia start date <= accessdate < tomorrow's date | ||
return true; | return true; | ||
Line 354: | Line 352: | ||
]] | ]] | ||
local function check_date (date_string, tCOinS_date) | local function check_date (date_string, tCOinS_date, test_accessdate) | ||
local year; -- assume that year2, months, and days are not used; | local year; -- assume that year2, months, and days are not used; | ||
local year2=0; -- second year in a year range | local year2=0; -- second year in a year range | ||
Line 441: | Line 439: | ||
month = get_season_number (month); -- for metadata | month = get_season_number (month); -- for metadata | ||
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d%d%d | elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d%d%d +[%-–] +%a+ +[1-9]%d%d%d%a?$") then -- month/season year - month/season year; separated by spaced endash | ||
month, year, month2, anchor_year, year2=mw.ustring.match(date_string, "(%a+) +(%d%d%d%d) [%-–] (%a+) +((%d%d%d%d)%a?)"); | month, year, month2, anchor_year, year2=mw.ustring.match(date_string, "(%a+) +(%d%d%d%d) +[%-–] +(%a+) +((%d%d%d%d)%a?)"); | ||
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years | anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years | ||
if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same | if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same | ||
Line 503: | Line 501: | ||
else | else | ||
return false; -- date format not one of the MOS:DATE approved formats | return false; -- date format not one of the MOS:DATE approved formats | ||
end | |||
if test_accessdate then -- test accessdate here because we have numerical date parts | |||
if 0 ~= year and 0 ~= month and 0 ~= day and -- all parts of a single date required | |||
0 == year2 and 0 == month2 and 0 == day2 then -- none of these; accessdate must not be a range | |||
if not is_valid_accessdate (year..'-'..month..'-'..day) then | |||
return false; -- return false when accessdate out of bounds | |||
end | |||
else | |||
return false; -- return false when accessdate is a range of two dates | |||
end | |||
end | end | ||
Line 561: | Line 570: | ||
end | end | ||
elseif 'date'==k then -- if the parameter is |date= | elseif 'date'==k then -- if the parameter is |date= | ||
if v:match("^n%.d%.%a?") then -- if |date=n.d. with or without a CITEREF disambiguator | if v:match("^n%.d%.%a?$") then -- if |date=n.d. with or without a CITEREF disambiguator | ||
good_date, anchor_year, COinS_date = true, v:match("((n%.d%.)%a?)"); --"n.d."; no error when date parameter is set to no date | 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 -- if |date=nd with or without a CITEREF disambiguator | elseif v:match("^nd%a?$") then -- if |date=nd with or without a CITEREF disambiguator | ||
Line 568: | Line 577: | ||
good_date, anchor_year, COinS_date = check_date (v, tCOinS_date); -- go test the date | good_date, anchor_year, COinS_date = check_date (v, tCOinS_date); -- go test the date | ||
end | end | ||
elseif ' | elseif 'year'==k then -- if the parameter is |year= it should hold only a year value | ||
if v:match("^[1-9]%d%d%d?%a?$") then -- if |year= 3 or 4 digits only with or without a CITEREF disambiguator | |||
good_date, anchor_year, COinS_date = true, v:match("((%d+)%a?)"); | |||
good_date = | |||
end | end | ||
elseif 'access-date'==k then -- if the parameter is |date= | |||
good_date = check_date (v, nil, true); -- go test the date; nil is a placeholder; true is the test_accessdate flag | |||
elseif 'embargo'==k then -- if the parameter is |embargo= | elseif 'embargo'==k then -- if the parameter is |embargo= | ||
good_date = check_date (v); -- go test the date | good_date = check_date (v); -- go test the date | ||
Line 750: | Line 760: | ||
format_str = long_formats[format]; | format_str = long_formats[format]; | ||
end | end | ||
-- convert date and save; | |||
date_parameters_list[param_name] = os.date (format_str, os.time(source_date)); -- | date_parameters_list[param_name] = mw.text.trim (os.date (format_str, os.time(source_date))); -- strip leading space when single digit day and %e is first format | ||
end | end | ||
end | end |