Content deleted Content added
implement sortable=on |
handle partial dates; option range=on/dash if want two values in the result; handle {{age in years}} |
||
Line 1:
-- Implement various "age of" and other date-related templates.
local _Date, _current_date
Line 65 ⟶ 61:
end
local function formatnumber(
-- Return the given number formatted with commas as group separators,
-- given that
local numstr = tostring(number)
local length = #numstr
local places = collection()
Line 118 ⟶ 115:
local text = collection()
local count = #values
local plural = names.plural or ''▼
local sep = names.sep or ''
for i, v in ipairs(values) do
if (islist or v > 0) or (text.n == 0 and i == count) or (text.n > 0 and components.keepzero) then
local fmt, vstr
if i == 1 and options.format == 'format_commas' then
-- Numbers after the first should be small and not need formatting.
else
fmt = tostring
end
if islist then
local join = options.range == 'dash' and '–' or ' or '
vstr = fmt(v[1]) .. join .. fmt(v[2])
else
vstr = fmt(v)
end
local name = names[components[i]]
if name then
text:add(vstr .. sep .. name .. (v == 1 and '' or plural))▼
if not plural or (islist and v[2] or v) == 1 then
plural = ''
end
else
text:add(vstr)
Line 228 ⟶ 238:
-- Return a formatted date difference using the given parameters
-- which have been validated.
local names = {
abbr_off = {
Line 292 ⟶ 301:
end
local options = {
round = parms.round,
duration = parms.want_duration,
range = parms.range and true or nil,
}
options = {
prefix = parms.sortable and make_sort(diff, parms.sortable) or nil,
suffix = parms.suffix, -- not currently used
Line 297 ⟶ 312:
join = parms.sep or default_join,
isnegative = diff.isnegative,
range = parms.range,
}
▲ local values = { diff:age(show.id, parms.round, parms.want_duration) }
return make_text(values, show, names[abbr], options)
end
Line 313 ⟶ 328:
-- Some results may be placed in table getopt.
local Date, current_date = get_exports(frame)
local partial = getopt.range and 'partial' or ''
local args = frame:getParent().args
local fields = {}
Line 341 ⟶ 357:
local nr_dates = 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_dates * 3 do
Line 353 ⟶ 370:
local index = i == 1 and 1 or 4
local y, m, d = fields[index], fields[index+1], fields[index+2]
if (partial and y) or (y and m and d) then
dates[i] = Date(partial, y, m, d)
elseif not (y or m or d) then
dates[i] = Date('currentdate')
Line 362 ⟶ 379:
else
getopt.textdates = true
dates[1] = Date(partial, fields[1] or 'currentdate')
if not single then
dates[2] = Date(partial, fields[2] or 'currentdate')
end
end
Line 389 ⟶ 406:
local args = frame:getParent().args
local specs = {
age_days = { -- {{age in days}}
show = 'd',
disp = 'disp_raw',
},
age_full_years = { -- {{age}}
show = 'y',
abbr = 'abbr_raw',
},
age_in_years = { -- {{age in years}}
show = 'y',
abbr = 'abbr_raw',
range = 'dash',
},
age_infant = { -- {{age for infant}}
-- Do not set show because special processing is done later.
abbr = yes(args.abbr) and 'abbr_infant' or 'abbr_off',
Line 404 ⟶ 426:
sortable = 'on',
},
age_m = { -- {{age in months}}
show = 'm',
disp = 'disp_raw',
},
age_w = { -- {{age in weeks}}
show = 'w',
disp = 'disp_raw',
},
age_wd = { -- {{age in weeks and days}}
show = 'wd',
},
age_yd = { -- {{age in years and days}}
show = 'yd',
sep = args.sep ~= 'and' and 'sep_comma' or nil,
sortable = 'on',
},
age_ym = { -- {{age in years and months}}
show = 'ym',
sep = 'sep_comma',
},
age_ymd = { -- {{age in years, months and days}}
show = 'ymd',
},
age_ymwd = { -- {{age in years, months, weeks and days}}
show = 'ymwd',
want_mixture = true,
},
}
Line 445 ⟶ 468:
end
end
local range = spec.range or yes(args.range) or (args.range == 'dash' and 'dash' or nil)
local getopt = { want_mixture = spec.want_mixture, range = range }
local date1, date2 = get_dates(frame, getopt)
if type(date1) == 'string' then
Line 459 ⟶ 483:
diff = date2 - date1,
want_duration = yes(args.duration),
range = range,
want_sc = yes(args.sc),
show = spec.show,
Line 518 ⟶ 543:
local parms = {
want_duration = yes(args.duration),
range = yes(args.range) or (args.range == 'dash' and 'dash' or nil),
want_sc = yes(args.sc),
}
local date1 = Date('partial', strip_to_nil(args[1]) or 'currentdatetime')
if not date1 then
return message('Invalid start date in first parameter')
end
local date2 = Date('partial', strip_to_nil(args[2]) or 'currentdatetime')
if not date2 then
return message('Invalid end date in second parameter')
Line 533 ⟶ 559:
if parm then
parm = translate[parm]
if parm == nil then -- test for nil because false is a valid setting
return message('Parameter ' .. argname .. '=' .. args[argname] .. ' is invalid')
end
|