Module:Age: Difference between revisions
NeonWabbit (talk | contribs) No edit summary |
NeonWabbit (talk | contribs) No edit summary |
||
| Line 31: | Line 31: | ||
['txt-and'] = ' and ', | ['txt-and'] = ' and ', | ||
['txt-or'] = ' or ', | ['txt-or'] = ' or ', | ||
['txt-category'] = 'Category: | ['txt-category'] = 'Category:Pages using age template with invalid date', | ||
['txt-comma-and'] = ', and ', | ['txt-comma-and'] = ', and ', | ||
['txt-error'] = 'Error: ', | ['txt-error'] = 'Error: ', | ||
| Line 55: | Line 55: | ||
-- Need to accept "on" because "round=on" is wanted. | -- Need to accept "on" because "round=on" is wanted. | ||
local yes = mtext['txt-yes'] | local yes = mtext['txt-yes'] | ||
-- Max valid age. | |||
local MAX_AGE = 130 | |||
local translate, from_en, to_en, isZero | local translate, from_en, to_en, isZero | ||
| Line 163: | Line 166: | ||
end | end | ||
local function dateFormat(args) | local function dateFormat(args, options) | ||
-- Return | -- Return | ||
-- nil, f if parameter is valid | -- nil, f if parameter is valid | ||
| Line 170: | Line 173: | ||
-- m = string for warning message with category | -- m = string for warning message with category | ||
-- f = string for wanted date format | -- f = string for wanted date format | ||
local wanted | |||
if options.suppliedformat == 'dmy' then | |||
wanted = 'df' | |||
elseif options.suppliedformat == 'mdy' then | |||
wanted = 'mf' | |||
else | |||
wanted = mtext['txt-format-default'] | |||
end | |||
local problem | local problem | ||
local other = wanted == 'df' and 'mf' or 'df' | local other = wanted == 'df' and 'mf' or 'df' | ||
local parm = args[other] or '' | local parm = args[other] or '' | ||
| Line 695: | Line 705: | ||
if not dates[1] then | if not dates[1] then | ||
return message(getopt.missing1 or 'mt-need-valid-ymd') | return message(getopt.missing1 or 'mt-need-valid-ymd') | ||
end | |||
if getopt.textdates then | |||
getopt.suppliedformat = dates[1].format | |||
end | end | ||
if getopt.single then | if getopt.single then | ||
| Line 900: | Line 913: | ||
local Date = getExports(frame) | local Date = getExports(frame) | ||
local diff = Date('currentdate') - date | local diff = Date('currentdate') - date | ||
if diff.isnegative or diff.years > | if diff.isnegative or diff.years > MAX_AGE then | ||
return message('mt-invalid-bd-age') | return message('mt-invalid-bd-age') | ||
end | end | ||
| Line 913: | Line 926: | ||
end | end | ||
end | end | ||
local problem, format = dateFormat(args) | local problem, format = dateFormat(args, options) | ||
local result = substituteParameters( | local result = substituteParameters( | ||
mtext['txt-bda'], | mtext['txt-bda'], | ||
| Line 992: | Line 1,005: | ||
years = diff.years | years = diff.years | ||
end | end | ||
if years > | if years > MAX_AGE then | ||
return message('mt-invalid-dates-age') | return message('mt-invalid-dates-age') | ||
end | end | ||
local fmt_date, fmt_ymd, problem | local fmt_date, fmt_ymd, problem | ||
if date1.day then -- y, m, d known | if date1.day then -- y, m, d known | ||
problem, fmt_date = dateFormat(args) | problem, fmt_date = dateFormat(args, options) | ||
fmt_ymd = '%-Y-%m-%d' | fmt_ymd = '%-Y-%m-%d' | ||
elseif date1.month then -- y, m known; d unknown | elseif date1.month then -- y, m known; d unknown | ||
| Line 1,142: | Line 1,155: | ||
local function templateGeneric(frame) | local function templateGeneric(frame) | ||
-- Example: {{#invoke:age||template=age_days|1 Apr 1980|1 Apr 2080|format=raw}} → 36525 | |||
local name = frame.args.template | local name = frame.args.template | ||
if not name then | if not name then | ||
return message('mt-template-x') | return message('mt-template-x') | ||
end | end | ||
return ageGeneric(frame:newChild{title = mw.title.new(name, 10), args = frame.args}) | return ageGeneric(frame:newChild({title = mw.title.new(name, 10), args = frame.args})) | ||
end | end | ||