Module:TimeAgo: Difference between revisions

From WIDEVERSE Wiki
Jump to navigation Jump to search
m (1 revision imported)
 
m (1 revision imported: Merging)
Line 1: Line 1:
-- Replacement for [[Template:Time ago]]
-- <nowiki>
local numberSpell = require('Module:NumberSpell')._main
--
local yesno = require('Module:Yesno')
-- Implements {{time ago}}
--


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


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


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


function p._main( args )
--
-- Initialize variables
-- Converts the input values to the returned string
local lang = mw.language.getContentLanguage()
--
local auto_magnitude_num
local function core( diff, abs_diff, magnitude, ago )
local min_magnitude_num
 
local result
    local num = math.floor( abs_diff )
local result_unit
    local unit = 1
local magnitude = args.magnitude
    local plural = 1
local min_magnitude = args.min_magnitude
local purge = args.purge
local spell_out = args.spellout
local spell_out_max = args.spelloutmax
-- Add a purge link if something (usually "yes") is entered into the purge parameter
if purge then
purge = ' <span class="plainlinks">([' .. mw.title.getCurrentTitle():fullUrl('action=purge') .. ' purge])</span>'
else
purge = ''
end


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


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


if magnitude then
    if diff >= 0 then
auto_magnitude_num = 0
        ago = ago or 'ago'
min_magnitude_num = timeText[magnitude]
    else
else
    plural = plural + 2
-- Calculate the appropriate unit of time if it was not specified as an argument.
        ago = 'time'
local autoMagnitudeData = {
    end
{ denom = 63115200, amn = 31557600 },
{ denom = 5356800, amn = 2678400 },
{ denom = 172800, amn = 86400 },
{ denom = 7200, amn = 3600 },
{ denom = 120, amn = 60 }
}
for i, t in ipairs( autoMagnitudeData ) do
if absTimeDiff / t.denom >= 1 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


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


local magnitude_num = math.max( min_magnitude_num, auto_magnitude_num )
end
local result_num = math.floor ( absTimeDiff / magnitude_num )


local punctuation_key, suffix
--
if timeDiff >= 0 then -- Past
-- Compares arg1 to arg2 and returns the larger number
if result_num == 1 then
--
punctuation_key = 1
local function max_( arg1, arg2 )
else
    if arg1 > arg2 then
punctuation_key = 2
        return arg1
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
result_unit = timeUnits[ magnitude_num ][ punctuation_key ]


-- Convert numerals to words if appropriate.
    return arg2
spell_out_max = tonumber( spell_out_max ) -- Would cause script errors if not a number.
end
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
-- Wrapper for use through #invoke
--
function p.ago( frame )
    return p._ago( frame:getParent().args )
end
end


function p.main( frame )
--
local args = require( 'Module:Arguments' ).getArgs( frame, {
-- Validates arguments and converts them to something that can be process by core
valueFunc = function( k, v )
--
if v then
-- @param args[1]            {str} time string
v = v:match( '^%s*(.-)%s*$' ) -- Trim whitespace.
-- @param args.magnitude    {str} (optional) override the output time's units
if k == 'ago' or v ~= '' then
-- @param args.min_magnitude {str} (optional) require a minimum unit for the output time
return v
-- @param args.ago          {str} (optional) Replace 'ago' with a different string
end
--                                only used for times in the past
end
-- @param args.purge        {str} (optional) add a purge link to the end of the resulting string
return nil
--                                will not work when testing from debug console
end,
--
wrappers = 'Template:Time ago'
function p._ago( args )
})
    local lang = mw.language.getContentLanguage()
return p._main( args )
    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">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

Revision as of 17:46, 1 October 2018

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 (might need tweaking?)
-- 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\''}
}

--
-- Converts the input values to the returned string
--
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 num .. ' ' .. units[magnitude + 1][plural] .. ' ' .. 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

--
-- Wrapper for use through #invoke
--
function p.ago( frame )
    return p._ago( frame:getParent().args )
end

--
-- Validates arguments and converts them to something that can be process by core
--
-- @param args[1]            {str} time string
-- @param args.magnitude     {str} (optional) override the output time's units
-- @param args.min_magnitude {str} (optional) require a minimum unit for the output time
-- @param args.ago           {str} (optional) Replace 'ago' with a different string
--                                 only used for times in the past
-- @param args.purge         {str} (optional) add a purge link to the end of the resulting string
--                                 will not work when testing from debug console
--
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">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

return p