Module:Citation/CS1/Date validation: Difference between revisions

    (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 the parameter has a value
    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 access- or archive-date and format not xxx-all
    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
    param_val.val = ''; -- set to empty string so we don't process this date
    for source, pattern in pairs (source_patterns) do
    end
    if param_val.val:match (pattern) then
    for source, pattern in pairs (source_patterns) do
    if 'ymd' == source then
    if param_val.val:match (pattern) then
    get_ymd_date_parts (param_val.val, source_date); -- get the date parts into the source_date table
    if 'ymd' == source then
    elseif 'dmy' == source then
    get_ymd_date_parts (param_val.val, source_date); -- get the date parts into the source_date table
    get_dmy_date_parts (param_val.val, source_date); -- get the date parts into the source_date table
    elseif 'dmy' == source then
    elseif 'mdy' == source then
    get_dmy_date_parts (param_val.val, source_date); -- get the date parts into the source_date table
    get_mdy_date_parts (param_val.val, source_date); -- get the date parts into the source_date table
    elseif 'mdy' == source then
    end
    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
    if 'ymd' == format and 1582 > tonumber(source_date.year) then -- ymd format dates not allowed before 1582
    end
    return false; -- abandon reformatting
    end
    if short then
    format_str = short_formats[format];
    if short then
    else
    format_str = short_formats[format];
    format_str = long_formats[format];
    else
    end
    format_str = long_formats[format];
    -- convert date and save;
    end
    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
    -- convert date and save;
    result = true;
    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
    end -- if
    end
    end -- for
    end
    end -- if
    end
    end -- if
    end
    end -- for
    return true; -- declare success and done
    return result; -- declare result and done
    end
    end