Module:TimeAgo: Difference between revisions

update from Module:TimeAgo/sandbox per WT:Lua#Module:TimeAgo's calculations sometimes off by a week: tweaks + accurate results for weeks/months/years with little extra overhead
m (1 revision imported: Merging)
(update from Module:TimeAgo/sandbox per WT:Lua#Module:TimeAgo's calculations sometimes off by a week: tweaks + accurate results for weeks/months/years with little extra overhead)
Line 1: Line 1:
-- <nowiki>
-- Implement [[Template:Time ago]]
--
 
-- Implements {{time ago}}
local numberSpell, yesno  -- lazy load
--
 
function numberSpell(arg)
numberSpell = require('Module:NumberSpell')._main
return numberSpell(arg)
end
 
function yesno(arg)
yesno = require('Module:Yesno')
return yesno(arg)
end


local p = {}
local p = {}
local yesno = require( 'Module:Yesno' )


-- assumes 31 days in a month (might need tweaking?)
-- Table to convert entered text values to numeric values.
-- assumes 365.25 days in a year to account for leap years
local timeText = {
local convert = {60, 3600, 86400, 604800, 2678400, 31557600}
['seconds'] = 1,
 
['minutes'] = 60,
-- used to convert units to magnitudes
['hours'] = 3600,
local magnitudes = {
['days'] = 86400,
    years = 6,
['weeks'] = 604800,
    months = 5,
['months'] = 2629800, -- 365.25 * 24 * 60 * 60 / 12
    weeks = 4,
['years'] = 31557600
    days = 3,
    hours = 2,
    minutes = 1,
    seconds = 0
}
}


-- units to append to time diff
-- Table containing tables of possible units to use in output.
local units = {
local timeUnits = {
    {'second', 'seconds', 'second\'s', 'seconds\''},
[1] = { 'second', 'seconds', "second's", "seconds'" },
    {'minute', 'minutes', 'minute\'s', 'minutes\''},
[60] = { 'minute', 'minutes', "minutes'", "minutes'" },
    {'hour', 'hours', 'hour\'s', 'hours\''},
[3600] = { 'hour', 'hours', "hour's", "hours'" },
    {'day', 'days', 'day\'s', 'days\''},
[86400] = { 'day', 'days', "day's", "days'" },
    {'week', 'weeks', 'week\'s', 'weeks\''},
[604800] = { 'week', 'weeks', "week's", "weeks'", unit = 'w' },
    {'month', 'months', 'month\'s', 'months\''},
[2629800] = { 'month', 'months', "month's", "months'", unit = 'm' },
    {'year', 'years', 'year\'s', 'years\''}
[31557600] = { 'year', 'years', "year's", "years'", unit = 'y' }
}
}


--
function p._main( args )
-- Converts the input values to the returned string
-- Initialize variables
--
local lang = mw.language.getContentLanguage()
local function core( diff, abs_diff, magnitude, ago )
local auto_magnitude_num
local min_magnitude_num
local magnitude = args.magnitude
local min_magnitude = args.min_magnitude
local purge = args.purge


    local num = math.floor( abs_diff )
-- Add a purge link if something (usually "yes") is entered into the purge parameter
    local unit = 1
if purge then
    local plural = 1
purge = ' <span class="plainlinks">([' .. mw.title.getCurrentTitle():fullUrl('action=purge') .. ' purge])</span>'
else
purge = ''
end


    if magnitude > 0 then
-- Check that the entered timestamp is valid. If it isn't, then give an error message.
        num = math.floor( abs_diff / convert[magnitude] )
local success, inputTime = pcall( lang.formatDate, lang, 'xnU', args[1] )
    end
if not success then
return '<strong class="error">Error: first parameter cannot be parsed as a date or time.</strong>'
end


    if abs_diff > 1 or abs_diff == 0 then
-- Store the difference between the current time and the inputted time, as well as its absolute value.
        plural = plural + 1
local timeDiff = lang:formatDate( 'xnU' ) - inputTime
    end
local absTimeDiff = math.abs( timeDiff )


    if diff >= 0 then
if magnitude then
        ago = ago or 'ago'
auto_magnitude_num = 0
    else
min_magnitude_num = timeText[magnitude]
    plural = plural + 2
else
        ago =  'time'
-- Calculate the appropriate unit of time if it was not specified as an argument.
    end
local autoMagnitudeData = {
{ factor = 2, amn = 31557600 },
{ factor = 2, amn = 2629800 },
{ factor = 2, amn = 86400 },
{ factor = 2, amn = 3600 },
{ factor = 2, amn = 60 }
}
for _, t in ipairs( autoMagnitudeData ) do
if absTimeDiff / t.amn >= t.factor then
auto_magnitude_num = t.amn
break
end
end
auto_magnitude_num = auto_magnitude_num or 1
if min_magnitude then
min_magnitude_num = timeText[min_magnitude]
else
min_magnitude_num = -1
end
end


    return num .. ' ' .. units[magnitude + 1][plural] .. ' ' .. ago
if not min_magnitude_num then
-- Default to seconds if an invalid magnitude is entered.
min_magnitude_num = 1
end


end
local result_num
local magnitude_num = math.max( min_magnitude_num, auto_magnitude_num )
local unit = timeUnits[magnitude_num].unit
if unit and absTimeDiff >= 864000 then
local Date = require('Module:Date')._Date
local input = lang:formatDate('Y-m-d H:i:s', args[1])  -- Date needs a clean date
input = Date(input)
if input then
local id
if input.hour == 0 and input.minute == 0 then
id = 'currentdate'
else
id = 'currentdatetime'
end
result_num = (Date(id) - input):age(unit)
end
end
result_num = result_num or math.floor ( absTimeDiff / magnitude_num )


--
local punctuation_key, suffix
-- Compares arg1 to arg2 and returns the larger number
if timeDiff >= 0 then -- Past
--
if result_num == 1 then
local function max_( arg1, arg2 )
punctuation_key = 1
    if arg1 > arg2 then
else
        return arg1
punctuation_key = 2
    end
end
if args.ago == '' then
suffix = ''
else
suffix = ' ' .. (args.ago or 'ago')
end
else -- Future
if args.ago == '' then
suffix = ''
if result_num == 1 then
punctuation_key = 1
else
punctuation_key = 2
end
else
suffix = ' time'
if result_num == 1 then
punctuation_key = 3
else
punctuation_key = 4
end
end
end
local result_unit = timeUnits[ magnitude_num ][ punctuation_key ]


    return arg2
-- Convert numerals to words if appropriate.
end
local spell_out = args.spellout
local spell_out_max = tonumber(args.spelloutmax)
local result_num_text
if spell_out and (
( 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


--
local result = result_num_text .. ' ' .. result_unit .. suffix -- Spaces for suffix have been added in earlier.
-- Wrapper for use through #invoke
return result .. purge
--
function p.ago( frame )
    return p._ago( frame:getParent().args )
end
end


--
function p.main( frame )
-- Validates arguments and converts them to something that can be process by core
local args = require( 'Module:Arguments' ).getArgs( frame, {
--
valueFunc = function( k, v )
-- @param args[1]            {str} time string
if v then
-- @param args.magnitude    {str} (optional) override the output time's units
v = v:match( '^%s*(.-)%s*$' ) -- Trim whitespace.
-- @param args.min_magnitude {str} (optional) require a minimum unit for the output time
if k == 'ago' or v ~= '' then
-- @param args.ago          {str} (optional) Replace 'ago' with a different string
return v
--                                only used for times in the past
end
-- @param args.purge        {str} (optional) add a purge link to the end of the resulting string
end
--                                will not work when testing from debug console
return nil
--
end,
function p._ago( args )
wrappers = 'Template:Time ago'
    local lang = mw.language.getContentLanguage()
})
    local frame = mw.getCurrentFrame()
return p._main( args )
    local cur_time = lang:formatDate( 'U' )
 
    -- check time argument is a valid time string
    local no_err, time = pcall( lang.formatDate, lang, 'U', args[1] )
    if not no_err then
        return '<strong class="error">Error: first parameter cannot be parsed as a date or time.</strong>'
    end
 
    -- calculate time diff in seconds
    local diff = cur_time - time
    local abs_diff = math.abs( diff )
 
    -- calculate magnitude
    local auto = 0
    local min_ = -1
 
    if args.magnitude then
        -- use the specified magnitude
        min_ = magnitudes[mw.ustring.lower( args.magnitude )]
    else
        -- use the specified minimum magnitude
        -- will only be used if it's higher than the auto-detected magnitude calculated below
        if args.min_magnitude then
            min_ = magnitudes[mw.ustring.lower( args.min_magnitude )]
        end
 
        -- auto detects the magnitude to be used
        -- multiples by two as it's preferred to have something like 43 hours instead of 2 days
        for i = 1, 6 do
            if math.floor( abs_diff / ( convert[i] * 2 ) ) > 0 then
                auto = auto + 1
            else
                break
            end
        end
        -- for some reason the original template didn't detect weeks, using days instead
        -- so preserve that behaviour here
        if auto == 4 then
            auto = 3
        end
    end
 
    magnitude = max_( auto, min_ )
 
    local ret = core( diff, abs_diff, magnitude, args.ago )
 
    if yesno( args.purge ) then
        -- @todo use mw.title for this
        local purgeTxt = args.purgeText or 'update'
        ret = string.format('%s&nbsp;<span class="plainlinks jsPurgeLink">([%s %s])</span>', ret, tostring(mw.uri.canonicalUrl(mw.title.getCurrentTitle().fullText, {action= 'purge'})), purgeTxt)
    end
 
    return ret
end
end


return p
return p
Anonymous user