Module:Time ago: Difference between revisions

94 bytes removed ,  8 years ago
allow args.ago to suppress "time" in future dates, and do some refactoring
(add ability to spell out numbers with new parameters "spellout" and "spelloutmax")
(allow args.ago to suppress "time" in future dates, and do some refactoring)
Line 1:
-- Replacement for [[Template:Time ago]]
local getArgs = require('Module:Arguments').getArgs
local numberSpell = require('Module:NumberSpell')._main
local yesno = require('Module:Yesno')
Line 6 ⟶ 5:
local p = {}
 
-- Table to convert entered text values to numeric values.
function p.main( frame )
timeText = {
local args = getArgs( frame, {
['seconds'] = 1,
valueFunc = function( k, v )
['minutes'] = 60,
if v then
['hours'] = 3600,
v = v:match( '^%s*(.-)%s*$' ) -- Trim whitespace.
['days'] = 86400,
if k == 'ago' or v ~= '' then
['weeks'] = 604800,
return v
['months'] = 2678400,
end
['years'] = 31557600
end
return nil
 
end
-- Table containing tables of possible units to use in output.
})
timeUnits = {
return p._main( args )
[1] = { 'second', 'seconds', "second's", "seconds'" },
end
[60] = { 'minute', 'minutes', "minutes'", "minutes'" },
[3600] = { 'hour', 'hours', "hour's", "hours'" },
[86400] = { 'day', 'days', "day's", "days'" },
[604800] = { 'week', 'weeks', "week's", "weeks'" },
[2678400] = { 'month', 'months', "month's", "months'" },
[31557600] = { 'year', 'years', "year's", "years'" }
 
function p._main( args )
-- Initialize variables
local lang = mw.language.getContentLanguage()
local ago
local auto_magnitude_num
local min_magnitude_num
Line 35 ⟶ 40:
local spell_out_max = args.spelloutmax
-- Generate the "ago" string. If ago is the blank string, do nothing - this allows overriding of args.ago
-- in cases where the module is used to generate something like "where he has worked for the past 20 years."
ago = args.ago
if ago and ago ~= '' then
ago = ' ' .. ago
elseif not ago then
ago = ' ago'
end
 
-- Add a purge link if something (usually "yes") is entered into the purge parameter
if purge then
Line 102 ⟶ 98:
punctuation_key = 2
end
suffixif args.ago == ago'' then
suffix = ' time'
else
suffix = ' ' .. (args.ago or 'ago')
end
else -- Future
if result_numargs.ago == 1'' then
punctuation_keysuffix = 3''
if result_num == 1 then
punctuation_key = 1
else
punctuation_key = 2
end
else
punctuation_keysuffix = 4' time'
if result_num == 1 then
punctuation_key = 3
else
punctuation_key = 4
end
end
suffix = ' time'
end
result_unit = timeUnits[ magnitude_num ][ punctuation_key ]
Line 128 ⟶ 137:
end
 
function p.main( frame )
-- Table to convert entered text values to numeric values.
local getArgsargs = require( 'Module:Arguments' ).getArgs( frame, {
timeText = {
valueFunc = function( k, v )
['seconds'] = 1,
if v then
['minutes'] = 60,
v = v:match( '^%s*(.-)%s*$' ) -- Trim whitespace.
['hours'] = 3600,
if k == 'ago' or v ~= '' then
['days'] = 86400,
return v
['weeks'] = 604800,
end
['months'] = 2678400,
end
['years'] = 31557600
return nil
end,
 
wrappers = 'Template:Time ago'
-- Table containing tables of possible units to use in output.
})
timeUnits = {
return p._main( args )
[1] = { 'second', 'seconds', "second's", "seconds'" },
end
[60] = { 'minute', 'minutes', "minutes'", "minutes'" },
[3600] = { 'hour', 'hours', "hour's", "hours'" },
[86400] = { 'day', 'days', "day's", "days'" },
[604800] = { 'week', 'weeks', "week's", "weeks'" },
[2678400] = { 'month', 'months', "month's", "months'" },
[31557600] = { 'year', 'years', "year's", "years'" }
 
return p