Module:Time ago: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(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: Line 1:
-- Replacement for [[Template:Time ago]]
-- Replacement for [[Template:Time ago]]
local getArgs = require('Module:Arguments').getArgs
local numberSpell = require('Module:NumberSpell')._main
local numberSpell = require('Module:NumberSpell')._main
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')
Line 6: Line 5:
local p = {}
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 )
function p._main( args )
-- Initialize variables
-- Initialize variables
local lang = mw.language.getContentLanguage()
local lang = mw.language.getContentLanguage()
local ago
local auto_magnitude_num
local auto_magnitude_num
local min_magnitude_num
local min_magnitude_num
Line 35: Line 40:
local spell_out_max = args.spelloutmax
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
-- Add a purge link if something (usually "yes") is entered into the purge parameter
if purge then
if purge then
Line 102: Line 98:
punctuation_key = 2
punctuation_key = 2
end
end
suffix = ago
if args.ago == '' then
suffix = ''
else
suffix = ' ' .. (args.ago or 'ago')
end
else -- Future
else -- Future
if result_num == 1 then
if args.ago == '' then
punctuation_key = 3
suffix = ''
if result_num == 1 then
punctuation_key = 1
else
punctuation_key = 2
end
else
else
punctuation_key = 4
suffix = ' time'
if result_num == 1 then
punctuation_key = 3
else
punctuation_key = 4
end
end
end
suffix = ' time'
end
end
result_unit = timeUnits[ magnitude_num ][ punctuation_key ]
result_unit = timeUnits[ magnitude_num ][ punctuation_key ]
Line 128: Line 137:
end
end


function p.main( frame )
-- Table to convert entered text values to numeric values.
local args = 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
return p