Изменения
Модуль:Citation/CS1/Date validation (посмотреть исходный код)
Версия от 17:25, 11 декабря 2023
, 1 год назадобновление из песочницы, оригинал https://en.wikipedia.org/w/index.php?title=Module:Citation/CS1/Date validation от 20 ноября 2023
local patterns = {
local patterns = {
-- year-initial numerical year-month-day
-- year-initial numerical year-month-day
['ymd'] = {'^(%d%d%d%d)%-(%d%d)%-(%d%d)$', 'y', 'm', 'd'},
['ymd'] = {'^(%d%d%d%d)%-(%d%d)%-(%d%d)$', 'y', 'm', 'd'},
['ym'] = {'^(%d%d%d%d)%-(%d%d)$', 'y', 'm'},
-- month-initial: month day, year
-- month-initial: month day, year
['Mdy'] = {'^(%D-) +([1-9]%d?), +((%d%d%d%d?)%a?)$', 'm', 'd', 'a', 'y'},
['Mdy'] = {'^(%D-) +([1-9]%d?), +((%d%d%d%d?)%a?)$', 'm', 'd', 'a', 'y'},
['Sy-y'] = {'^(%D-) +(%d%d%d%d)[%-–]((%d%d%d%d)%a?)$'}, -- special case Winter/Summer year-year; year separated with unspaced endash
['Sy-y'] = {'^(%D-) +(%d%d%d%d)[%-–]((%d%d%d%d)%a?)$'}, -- special case Winter/Summer year-year; year separated with unspaced endash
['y-y'] = {'^(%d%d%d%d?)[%-–]((%d%d%d%d?)%a?)$'}, -- year range: YYY-YYY or YYY-YYYY or YYYY–YYYY; separated by unspaced endash; 100-9999
['y-y'] = {'^(%d%d%d%d?)[%-–]((%d%d%d%d?)%a?)$'}, -- year range: YYY-YYY or YYY-YYYY or YYYY–YYYY; separated by unspaced endash; 100-9999
-- ['y4-y2'] = {'^((%d%d)%d%d)[%-–]((%d%d)%a?)$'}, -- year range: YYYY–YY; separated by unspaced endash
['y'] = {'^((%d%d%d%d?)%a?)$'}, -- year; here accept either YYY or YYYY
['y'] = {'^((%d%d%d%d?)%a?)$'}, -- year; here accept either YYY or YYYY
}
}
year, month, day = date_string:match (patterns['ymd'][1]);
year, month, day = date_string:match (patterns['ymd'][1]);
if 12 < tonumber(month) or 1 > tonumber(month) or 1582 > tonumber(year) or 0 == tonumber(day) then return false; end -- month or day number not valid or not Gregorian calendar
if 12 < tonumber(month) or 1 > tonumber(month) or 1582 > tonumber(year) or 0 == tonumber(day) then return false; end -- month or day number not valid or not Gregorian calendar
anchor_year = year;
elseif date_string:match (patterns['ym'][1]) then -- year-initial numerical year month format
year, month = date_string:match (patterns['ym'][1]);
if 12 < tonumber(month) or 1 > tonumber(month) or 1582 > tonumber(year) then return false; end -- month number not valid or not Gregorian calendar
anchor_year = year;
anchor_year = year;
if not is_valid_year(year2) then return false; end -- no year farther in the future than next year
if not is_valid_year(year2) then return false; end -- no year farther in the future than next year
-- elseif mw.ustring.match(date_string, patterns['y4-y2'][1]) then -- Year range: YYYY–YY; separated by unspaced endash
-- local century;
-- year, century, anchor_year, year2 = mw.ustring.match(date_string, patterns['y4-y2'][1]);
-- anchor_year = year .. '–' .. anchor_year; -- assemble anchor year from both years
-- if 13 > tonumber(year2) then return false; end -- don't allow 2003-05 which might be May 2003
-- year2 = century .. year2; -- add the century to year2 for comparisons
-- if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same
-- if not is_valid_year(year2) then return false; end -- no year farther in the future than next year
-- if in_array (param, {'date', 'publication-date', 'year'}) then -- here when 'valid' abbreviated year range; if one of these parameters
-- add_prop_cat ('year-range-abbreviated'); -- add properties cat
-- end
elseif mw.ustring.match(date_string, patterns['y'][1]) then -- year; here accept either YYY or YYYY
elseif mw.ustring.match(date_string, patterns['y'][1]) then -- year; here accept either YYY or YYYY
year = year_string:match ('(%d%d%d%d?)');
year = year_string:match ('(%d%d%d%d?)');
if date_string:match ('%d%d%d%d%-%d%d%-%d%d') and year_string:match ('%d%d%d%d%a') then --special case where both date and year are required YYYY-MM-DD and YYYYx
if ( date_string:match ('%d%d%d%d%-%d%d%-%d%d') or date_string:match ('%d%d%d%d%-%d%d') ) and year_string:match ('%d%d%d%d%a') then --special case where both date and year are required YYYY-MM-DD and YYYYx
date1 = date_string:match ('(%d%d%d%d)');
date1 = date_string:match ('(%d%d%d%d)');
year = year_string:match ('(%d%d%d%d)');
year = year_string:match ('(%d%d%d%d)');
end
end
-- elseif mw.ustring.match(date_string, "%d%d%d%d[%-–]%d%d") then -- YYYY-YY date ranges
-- local century;
-- date1, century, date2 = mw.ustring.match(date_string, "((%d%d)%d%d)[%-–]+(%d%d)");
-- date2 = century..date2; -- convert YY to YYYY
-- if year ~= date1 and year ~= date2 then
-- result = 0;
-- end
elseif date_string:match ("%d%d%d%d?") then -- any of the standard formats of date with one year
elseif date_string:match ("%d%d%d%d?") then -- any of the standard formats of date with one year