Module:Citation/CS1/Date validation: Difference between revisions
synch from sandbox;
(synch from sandbox;) |
w>Trappist the monk (synch from sandbox;) |
||
Line 744: | Line 744: | ||
local function reformat_dates (date_parameters_list, format, short) | local function reformat_dates (date_parameters_list, format, short) | ||
local all = false; -- set to false to skip access- and archive-dates | local all = false; -- set to false to skip access- and archive-dates | ||
local result = false; | |||
local format_str; | local format_str; | ||
local source_date = {}; | local source_date = {}; | ||
Line 753: | Line 754: | ||
for param_name, param_val in pairs (date_parameters_list) do -- for each date-holding parameter in the list | for param_name, param_val in pairs (date_parameters_list) do -- for each date-holding parameter in the list | ||
if is_set (param_val.val) then | if is_set (param_val.val) then -- if the parameter has a value | ||
if not all and in_array (param_name, {'access-date', 'archive-date'}) then -- | if not (not all and in_array (param_name, {'access-date', 'archive-date'})) then -- skip access- or archive-date unless format is xxx-all; yeah, ugly; TODO: find a better way | ||
for source, pattern in pairs (source_patterns) do | |||
if param_val.val:match (pattern) then | |||
if 'ymd' == source then | |||
get_ymd_date_parts (param_val.val, source_date); -- get the date parts into the source_date table | |||
elseif 'dmy' == source then | |||
get_dmy_date_parts (param_val.val, source_date); -- get the date parts into the source_date table | |||
elseif 'mdy' == source then | |||
get_mdy_date_parts (param_val.val, source_date); -- get the date parts into the source_date table | |||
end | |||
if 'ymd' == format and 1582 > tonumber(source_date.year) then -- ymd format dates not allowed before 1582 | |||
return false; -- abandon reformatting | |||
end | |||
if short then | |||
format_str = short_formats[format]; | |||
else | |||
format_str = long_formats[format]; | |||
end | |||
-- convert date and save; | |||
date_parameters_list[param_name].val = mw.text.trim (os.date (format_str, os.time(source_date))); -- strip leading space when single digit day and %e is first format | |||
result = true; | |||
end -- if | |||
end | end -- for | ||
end | end -- if | ||
end | end -- if | ||
end | end -- for | ||
return result; -- declare result and done | |||
end | end | ||