14,061
edits
m (1 revision) |
m (1 revision) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
-- Replacement for [[Template:Time ago]] | -- Replacement for [[Template:Time ago]] | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local numberSpell = require('Module:NumberSpell')._main | |||
local yesno = require('Module:Yesno') | |||
local p = {} | local p = {} | ||
Line 30: | Line 32: | ||
local min_magnitude = args.min_magnitude | local min_magnitude = args.min_magnitude | ||
local purge = args.purge | local purge = args.purge | ||
local spell_out = args.spellout | |||
local spell_out_max = args.spelloutmax | |||
-- Generate the "ago" string. If ago is the blank string, do nothing - this allows overriding of args.ago | -- Generate the "ago" string. If ago is the blank string, do nothing - this allows overriding of args.ago | ||
Line 48: | Line 52: | ||
-- Check that the entered timestamp is valid. If it isn't, then give an error message. | -- Check that the entered timestamp is valid. If it isn't, then give an error message. | ||
local noError, inputTime = pcall( lang.formatDate, lang, 'U', args[1] ) | local noError, inputTime = pcall( lang.formatDate, lang, 'U', args[1], true ) | ||
if not noError then | if not noError then | ||
return '<strong class="error">Error: first parameter cannot be parsed as a date or time.</strong>' | return '<strong class="error">Error: first parameter cannot be parsed as a date or time.</strong>' | ||
Line 54: | Line 58: | ||
-- Store the difference between the current time and the inputted time, as well as its absolute value. | -- Store the difference between the current time and the inputted time, as well as its absolute value. | ||
local timeDiff = lang:formatDate( 'U' ) - inputTime | local timeDiff = lang:formatDate( 'U', nil, true ) - inputTime | ||
local absTimeDiff = math.abs( timeDiff ) | local absTimeDiff = math.abs( timeDiff ) | ||
Line 63: | Line 67: | ||
-- Calculate the appropriate unit of time if it was not specified as an argument. | -- Calculate the appropriate unit of time if it was not specified as an argument. | ||
local autoMagnitudeData = { | local autoMagnitudeData = { | ||
{denom = 63115200, amn = 31557600}, | { denom = 63115200, amn = 31557600 }, | ||
{denom = 5356800, amn = 2678400}, | { denom = 5356800, amn = 2678400 }, | ||
{denom = 172800, amn = 86400}, | { denom = 172800, amn = 86400 }, | ||
{denom = 7200, amn = 3600}, | { denom = 7200, amn = 3600 }, | ||
{denom = 120, amn = 60} | { denom = 120, amn = 60 } | ||
} | } | ||
for i, t in ipairs(autoMagnitudeData) do | for i, t in ipairs( autoMagnitudeData ) do | ||
if absTimeDiff / t.denom >= 1 then | if absTimeDiff / t.denom >= 1 then | ||
auto_magnitude_num = t.amn | auto_magnitude_num = t.amn | ||
Line 91: | Line 95: | ||
local result_num = math.floor ( absTimeDiff / magnitude_num ) | local result_num = math.floor ( absTimeDiff / magnitude_num ) | ||
local punctuation_key, suffix | |||
if timeDiff >= 0 then -- Past | if timeDiff >= 0 then -- Past | ||
if result_num == 1 then | if result_num == 1 then | ||
punctuation_key = 1 | |||
else | else | ||
punctuation_key = 2 | |||
end | end | ||
suffix = ago | |||
else -- Future | else -- Future | ||
if result_num == 1 then | if result_num == 1 then | ||
punctuation_key = 3 | |||
else | else | ||
punctuation_key = 4 | |||
end | end | ||
suffix = ' time' | |||
end | end | ||
result_unit = timeUnits[ magnitude_num ][ punctuation_key ] | |||
-- Convert numerals to words if appropriate. | |||
spell_out_max = tonumber( spell_out_max ) -- Would cause script errors if not a number. | |||
local result_num_text | |||
if ( spell_out == 'auto' and 1 <= result_num and result_num <= 9 and result_num <= ( spell_out_max or 9 ) ) | |||
or ( yesno( spell_out ) and 1 <= result_num and result_num <= 100 and result_num <= ( spell_out_max or 100 ) ) | |||
then | |||
result_num_text = numberSpell( result_num ) | |||
else | |||
result_num_text = tostring( result_num ) | |||
end | |||
result = result_num_text .. ' ' .. result_unit .. suffix -- Spaces for suffix have been added in earlier. | |||
return result .. purge | return result .. purge | ||
end | end |