Old School RuneScape Wiki
Advertisement

Documentation for this module may be created at Module:TimeAgo/doc

-- <nowiki>
--
-- Implements {{time ago}}
--

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

-- assumes 31 days in a month
-- assumes 365.25 days in a year to account for leap years
local convert = {60, 3600, 86400, 604800, 2678400, 31557600}

-- used to convert units to magnitudes
local magnitudes = {
    years = 6,
    months = 5,
    weeks = 4,
    days = 3,
    hours = 2,
    minutes = 1,
    seconds = 0
}

-- units to append to time diff
local units = {
    {'second', 'seconds', 'second\'s', 'seconds\''},
    {'minute', 'minutes', 'minute\'s', 'minutes\''},
    {'hour', 'hours', 'hour\'s', 'hours\''},
    {'day', 'days', 'day\'s', 'days\''},
    {'week', 'weeks', 'week\'s', 'weeks\''},
    {'month', 'months', 'month\'s', 'months\''},
    {'year', 'years', 'year\'s', 'years\''}
}

--
-- <core>
--
local function core( diff, abs_diff, magnitude, ago )

    local num = math.floor( abs_diff )
    local unit = 1
    local plural = 1

    if magnitude > 0 then
        num = math.floor( abs_diff / convert[magnitude] )
    end

    if abs_diff > 1 or abs_diff == 0 then
        plural = plural + 1
    end

    if diff > 0 then
        ago = ago or 'ago'
    else
    	plural = plural + 2
        ago =  'time'
    end

    -- return abs_diff .. ' ' .. units[magnitude + 1] .. ' ' .. ago
    return abs_diff .. ' ' .. ago
end

--
-- Compares arg1 to arg2 and returns the larger number
--
local function max( arg1, arg2 )
    if arg1 > arg2 then
        return arg1
    end

    return arg2
end

--
-- <ago>
--
function p.ago( frame )
    return p._ago( frame:getParent().args )
end

function p._ago( args )
    local lang = mw.language.getContentLanguage()
    local frame = mw.getCurrentFrame()
    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">some error code</strong>'
    end

    -- calculate time diff in seconds
    local diff = cur_time - time
    local abs_diff = math.abs( diff )

    -- calculate magnitude
    if not args.magnitude then
        local auto = 0
        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 or 0, min or -1 )

    local ret = core( diff, abs_diff, magnitude, args.ago )

    if yesno( args.purge ) and frame then
        -- @todo use mw.title for this
        ret = ret .. '&nbsp;<span class="plainlinks">([' .. frame:preprocess( '{{fullurl:{{FULLPAGENAME}}|action=purge}}' ) .. ' purge])</span>'
    end

    return ret
end

return p
Advertisement