Module:Age/sandbox: Difference between revisions

Content deleted Content added
new collection; try different birth date and age procedures: reject empty birth date; show age in days if months < 1, or months if years < 2, otherwise years
convert to camelCase per dominant style
Line 2:
 
local _Date, _current_date
local function get_exportsgetExports(frame)
-- Return objects exported from the date module or its sandbox.
if not _Date then
Line 39:
Collection.__index = Collection
 
local function strip_to_nilstripToNil(text)
-- If text is a string, return its trimmed content, or nil if empty.
-- Otherwise return text (which may, for example, be nil).
Line 94:
end
 
local function make_sortmakeSort(value, sortable)
-- Return a sort key in a span if specified.
-- Assume value is a valid number which has not overflowed.
Line 119:
end
 
local translate_parameterstranslateParameters = {
abbr = {
off = 'abbr_off',
Line 177:
}
 
local function date_extractdateExtract(frame)
-- Return part of a date after performing an optional operation.
local Date = get_exportsgetExports(frame)
local args = frame:getParent().args
local parms = {}
Line 195:
return message('Need valid date')
end
local add = strip_to_nilstripToNil(args.add)
if add then
for item in add:gmatch('%S+') do
Line 205:
end
local prefix, result
local sortable = translate_parameterstranslateParameters.sortable[args.sortable]
if sortable then
local value = (date.partial and date.partial.first or date).jdz
prefix = make_sortmakeSort(value, sortable)
end
local show = strip_to_nilstripToNil(args.show) or 'dmy'
if show ~= 'hide' then
result = date[show]
Line 224:
end
 
local function make_textmakeText(values, components, names, options)
-- Return wikitext representing an age or duration.
local text = Collection.new()
Line 295:
end
 
local function date_differencedateDifference(parms)
-- Return a formatted date difference using the given parameters
-- which have been validated.
Line 335:
local show = parms.show -- may be nil; default is set below
local abbr = parms.abbr or 'abbr_off'
local default_joindefaultJoin
if abbr ~= 'abbr_off' then
default_joindefaultJoin = 'sep_space'
end
if not show then
Line 343:
if parms.disp == 'disp_age' then
if diff.years < 3 then
default_joindefaultJoin = 'sep_space'
if diff.years >= 1 then
show = 'ym'
Line 355:
end
if type(show) ~= 'table' then
show = translate_parameterstranslateParameters.show[show]
end
if parms.disp == 'disp_raw' then
default_joindefaultJoin = 'sep_space'
abbr = 'abbr_raw'
elseif parms.want_sc then
default_joindefaultJoin = 'sep_serialcomma'
end
local diff_optionsdiffOptions = {
round = parms.round,
duration = parms.want_duration,
Line 374:
value = -value
end
prefix = make_sortmakeSort(value, parms.sortable)
end
local text_optionstextOptions = {
prefix = prefix,
suffix = parms.suffix, -- not currently used
format = parms.format,
join = parms.sep or default_joindefaultJoin,
isnegative = diff.isnegative,
range = parms.range,
Line 387:
return prefix or ''
end
local values = { diff:age(show.id, diff_optionsdiffOptions) }
if values[1] then
return make_textmakeText(values, show, names[abbr], text_optionstextOptions)
end
return message('Parameter show=' .. show.id .. ' is not supported here')
end
 
local function get_datesgetDates(frame, getopt)
-- Parse template parameters and return one of:
-- * date (a date table, if single)
Line 404:
-- specified/current y/m/d as has been done by some "age" templates.
-- Some results may be placed in table getopt.
local Date, current_datecurrentDate = get_exportsgetExports(frame)
getopt = getopt or {}
local fix = getopt.fix and 'fix' or ''
Line 410:
local args = frame:getParent().args
local fields = {}
local is_namedisNamed = args.year or args.year1 or args.year2 or
args.month or args.month1 or args.month2 or
args.day or args.day1 or args.day2
if is_namedisNamed then
fields[1] = args.year1 or args.year
fields[2] = args.month1 or args.month
Line 427:
local imax = 0
for i = 1, 6 do
fields[i] = strip_to_nilstripToNil(fields[i])
if fields[i] then
imax = i
Line 434:
local nodefault = imax == 0 and getopt.nomissing
local dates = {}
if is_namedisNamed or imax >= 3 then
local nr_datesnrDates = getopt.single and 1 or 2
if getopt.want_mixture then
-- Cannot be partial since empty fields are set from current.
local components = { 'year', 'month', 'day' }
for i = 1, nr_datesnrDates * 3 do
fields[i] = fields[i] or current_datecurrentDate[components[i > 3 and i - 3 or i]]
end
for i = 1, nr_datesnrDates do
local index = i == 1 and 1 or 4
dates[i] = Date(fields[index], fields[index+1], fields[index+2])
end
else
for i = 1, nr_datesnrDates do
local index = i == 1 and 1 or 4
local y, m, d = fields[index], fields[index+1], fields[index+2]
Line 476:
end
 
local function age_genericageGeneric(frame)
-- Return the result required by the specified template.
-- Can use sortable=x where x = on/off/debug in any supported template.
Line 579:
end
if name == 'age_days' then
local su = strip_to_nilstripToNil(args['show unit'])
if su then
if su == 'abbr' or su == 'full' then
Line 593:
want_mixture = spec.want_mixture,
}
local date1, date2 = get_datesgetDates(frame, getopt)
if type(date1) == 'string' then
return date1
end
local format = strip_to_nilstripToNil(args.format)
if format then
format = 'format_' .. format
Line 614:
round = yes(args.round),
sep = spec.sep,
sortable = translate_parameterstranslateParameters.sortable[args.sortable or spec.sortable],
}
if (spec.negative or frame.args.negative) == 'error' and parms.diff.isnegative then
return message('The second date should not be before the first date')
end
return date_differencedateDifference(parms)
end
 
Line 625:
-- Implement [[Template:Birth date and age]].
local args = frame:getParent().args
local date = get_datesgetDates(frame, { nomissing=true, single=true })
if type(date) == 'string' then
return date -- error text
end
local Date = get_exportsgetExports(frame)
local now = Date('currentdate')
local diff = now - date
Line 644:
end
end
local df = strip_to_nilstripToNil(args.df) -- day first (dmy); default is month first (mdy)
local result = df and
'(<span class="bday">%-Y-%m-%d</span>) </span>%-d %B %-Y' or
Line 652:
'<span class="noprint ForceAgeToShow"> ' ..
'(age&nbsp;' ..
date_differencedateDifference({
diff = diff,
show = show,
Line 663:
end
 
local function date_to_gsddateToGsd(frame)
-- Implement [[Template:Gregorian serial date]].
-- Return Gregorian serial date of the given date, or the current date.
-- The returned value is negative for dates before 1 January 1 AD
-- despite the fact that GSD is not defined for such dates.
local date = get_datesgetDates(frame, { want_mixture=true, single=true })
if type(date) == 'string' then
return date
Line 675:
end
 
local function jd_to_datejdToDate(frame)
-- Return formatted date from a Julian date.
-- The result includes a time if the input includes a fraction.
-- The word 'Julian' is accepted for the Julian calendar.
local Date = get_exportsgetExports(frame)
local args = frame:getParent().args
local date = Date('juliandate', args[1], args[2])
Line 688:
end
 
local function date_to_jddateToJd(frame)
-- Return Julian date (a number) from a date which may include a time,
-- or the current date ('currentdate') or current date and time ('currentdatetime').
-- The word 'Julian' is accepted for the Julian calendar.
local Date = get_exportsgetExports(frame)
local args = frame:getParent().args
local date = Date(args[1], args[2], args[3], args[4], args[5], args[6], args[7])
Line 701:
end
 
local function time_intervaltimeInterval(frame)
-- Implement [[Template:Time interval]].
-- There are two positional arguments: date1, date2.
-- The default for each is the current date and time.
-- Result is date2 - date1 formatted.
local Date = get_exportsgetExports(frame)
local args = frame:getParent().args
local parms = {
Line 714:
}
local fix = yes(args.fix) and 'fix' or ''
local date1 = Date(fix, 'partial', strip_to_nilstripToNil(args[1]) or 'currentdatetime')
if not date1 then
return message('Invalid start date in first parameter')
end
local date2 = Date(fix, 'partial', strip_to_nilstripToNil(args[2]) or 'currentdatetime')
if not date2 then
return message('Invalid end date in second parameter')
end
parms.diff = date2 - date1
for argname, translate in pairs(translate_parameterstranslateParameters) do
local parm = strip_to_nilstripToNil(args[argname])
if parm then
parm = translate[parm]
Line 742:
end
else
parms.show = translate_parameterstranslateParameters.show[round]
end
end
parms.round = true
end
return date_differencedateDifference(parms)
end
 
return {
age_generic = age_genericageGeneric, -- can emulate several age templates
birth_date_and_age = bda, -- Template:Birth_date_and_age
gsd = date_to_gsddateToGsd, -- Template:Gregorian_serial_date
extract = date_extractdateExtract, -- Template:Extract
jd_to_date = jd_to_datejdToDate, -- Template:?
JULIANDAY = date_to_jddateToJd, -- Template:JULIANDAY
time_interval = time_intervaltimeInterval, -- Template:Time_interval
}