<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.runerealm.org/index.php?action=history&amp;feed=atom&amp;title=Module%3ATimeAgo</id>
	<title>Module:TimeAgo - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.runerealm.org/index.php?action=history&amp;feed=atom&amp;title=Module%3ATimeAgo"/>
	<link rel="alternate" type="text/html" href="https://wiki.runerealm.org/index.php?title=Module:TimeAgo&amp;action=history"/>
	<updated>2026-05-14T13:27:48Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.1</generator>
	<entry>
		<id>https://wiki.runerealm.org/index.php?title=Module:TimeAgo&amp;diff=451&amp;oldid=prev</id>
		<title>Alex: Created page with &quot;-- &lt;nowiki&gt; -- -- Implements {{time ago}} --  local p = {} local yesno = require( &#039;Module:Yesno&#039; )  -- 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 dif...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.runerealm.org/index.php?title=Module:TimeAgo&amp;diff=451&amp;oldid=prev"/>
		<updated>2024-10-11T22:10:51Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;-- &amp;lt;nowiki&amp;gt; -- -- Implements {{time ago}} --  local p = {} local yesno = require( &amp;#039;Module:Yesno&amp;#039; )  -- 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 dif...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;-- &amp;lt;nowiki&amp;gt;&lt;br /&gt;
--&lt;br /&gt;
-- Implements {{time ago}}&lt;br /&gt;
--&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
local yesno = require( &amp;#039;Module:Yesno&amp;#039; )&lt;br /&gt;
&lt;br /&gt;
-- assumes 31 days in a month (might need tweaking?)&lt;br /&gt;
-- assumes 365.25 days in a year to account for leap years&lt;br /&gt;
local convert = {60, 3600, 86400, 604800, 2678400, 31557600}&lt;br /&gt;
&lt;br /&gt;
-- used to convert units to magnitudes&lt;br /&gt;
local magnitudes = {&lt;br /&gt;
    years = 6,&lt;br /&gt;
    months = 5,&lt;br /&gt;
    weeks = 4,&lt;br /&gt;
    days = 3,&lt;br /&gt;
    hours = 2,&lt;br /&gt;
    minutes = 1,&lt;br /&gt;
    seconds = 0&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
-- units to append to time diff&lt;br /&gt;
local units = {&lt;br /&gt;
    {&amp;#039;second&amp;#039;, &amp;#039;seconds&amp;#039;, &amp;#039;second\&amp;#039;s&amp;#039;, &amp;#039;seconds\&amp;#039;&amp;#039;},&lt;br /&gt;
    {&amp;#039;minute&amp;#039;, &amp;#039;minutes&amp;#039;, &amp;#039;minute\&amp;#039;s&amp;#039;, &amp;#039;minutes\&amp;#039;&amp;#039;},&lt;br /&gt;
    {&amp;#039;hour&amp;#039;, &amp;#039;hours&amp;#039;, &amp;#039;hour\&amp;#039;s&amp;#039;, &amp;#039;hours\&amp;#039;&amp;#039;},&lt;br /&gt;
    {&amp;#039;day&amp;#039;, &amp;#039;days&amp;#039;, &amp;#039;day\&amp;#039;s&amp;#039;, &amp;#039;days\&amp;#039;&amp;#039;},&lt;br /&gt;
    {&amp;#039;week&amp;#039;, &amp;#039;weeks&amp;#039;, &amp;#039;week\&amp;#039;s&amp;#039;, &amp;#039;weeks\&amp;#039;&amp;#039;},&lt;br /&gt;
    {&amp;#039;month&amp;#039;, &amp;#039;months&amp;#039;, &amp;#039;month\&amp;#039;s&amp;#039;, &amp;#039;months\&amp;#039;&amp;#039;},&lt;br /&gt;
    {&amp;#039;year&amp;#039;, &amp;#039;years&amp;#039;, &amp;#039;year\&amp;#039;s&amp;#039;, &amp;#039;years\&amp;#039;&amp;#039;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
--&lt;br /&gt;
-- Converts the input values to the returned string&lt;br /&gt;
--&lt;br /&gt;
local function core( diff, abs_diff, magnitude, ago )&lt;br /&gt;
&lt;br /&gt;
    local num = math.floor( abs_diff )&lt;br /&gt;
    local unit = 1&lt;br /&gt;
    local plural = 1&lt;br /&gt;
&lt;br /&gt;
    if magnitude &amp;gt; 0 then&lt;br /&gt;
        num = math.floor( abs_diff / convert[magnitude] )&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if abs_diff &amp;gt; 1 or abs_diff == 0 then&lt;br /&gt;
        plural = plural + 1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    if diff &amp;gt;= 0 then&lt;br /&gt;
        ago = ago or &amp;#039;ago&amp;#039;&lt;br /&gt;
    else&lt;br /&gt;
    	plural = plural + 2&lt;br /&gt;
        ago =  &amp;#039;time&amp;#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return num .. &amp;#039; &amp;#039; .. units[magnitude + 1][plural] .. &amp;#039; &amp;#039; .. ago&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--&lt;br /&gt;
-- Compares arg1 to arg2 and returns the larger number&lt;br /&gt;
--&lt;br /&gt;
local function max_( arg1, arg2 )&lt;br /&gt;
    if arg1 &amp;gt; arg2 then&lt;br /&gt;
        return arg1&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return arg2&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--&lt;br /&gt;
-- Wrapper for use through #invoke&lt;br /&gt;
--&lt;br /&gt;
function p.ago( frame )&lt;br /&gt;
    return p._ago( frame:getParent().args )&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--&lt;br /&gt;
-- Validates arguments and converts them to something that can be process by core&lt;br /&gt;
--&lt;br /&gt;
-- @param args[1]            {str} time string&lt;br /&gt;
-- @param args.magnitude     {str} (optional) override the output time&amp;#039;s units&lt;br /&gt;
-- @param args.min_magnitude {str} (optional) require a minimum unit for the output time&lt;br /&gt;
-- @param args.ago           {str} (optional) Replace &amp;#039;ago&amp;#039; with a different string&lt;br /&gt;
--                                 only used for times in the past&lt;br /&gt;
-- @param args.purge         {str} (optional) add a purge link to the end of the resulting string&lt;br /&gt;
--                                 will not work when testing from debug console&lt;br /&gt;
--&lt;br /&gt;
function p._ago( args )&lt;br /&gt;
    local lang = mw.language.getContentLanguage()&lt;br /&gt;
    local frame = mw.getCurrentFrame()&lt;br /&gt;
    local cur_time = lang:formatDate( &amp;#039;U&amp;#039; )&lt;br /&gt;
&lt;br /&gt;
    -- check time argument is a valid time string&lt;br /&gt;
    local no_err, time = pcall( lang.formatDate, lang, &amp;#039;U&amp;#039;, args[1] )&lt;br /&gt;
    if not no_err then&lt;br /&gt;
        return &amp;#039;&amp;lt;strong class=&amp;quot;error&amp;quot;&amp;gt;Error: first parameter cannot be parsed as a date or time.&amp;lt;/strong&amp;gt;&amp;#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- calculate time diff in seconds&lt;br /&gt;
    local diff = cur_time - time&lt;br /&gt;
    local abs_diff = math.abs( diff )&lt;br /&gt;
&lt;br /&gt;
    -- calculate magnitude&lt;br /&gt;
    local auto = 0&lt;br /&gt;
    local min_ = -1&lt;br /&gt;
&lt;br /&gt;
    if args.magnitude then&lt;br /&gt;
        -- use the specified magnitude&lt;br /&gt;
        min_ = magnitudes[mw.ustring.lower( args.magnitude )]&lt;br /&gt;
    else&lt;br /&gt;
        -- use the specified minimum magnitude&lt;br /&gt;
        -- will only be used if it&amp;#039;s higher than the auto-detected magnitude calculated below&lt;br /&gt;
        if args.min_magnitude then&lt;br /&gt;
            min_ = magnitudes[mw.ustring.lower( args.min_magnitude )]&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        -- auto detects the magnitude to be used&lt;br /&gt;
        -- multiples by two as it&amp;#039;s preferred to have something like 43 hours instead of 2 days&lt;br /&gt;
        for i = 1, 6 do&lt;br /&gt;
            if math.floor( abs_diff / ( convert[i] * 2 ) ) &amp;gt; 0 then&lt;br /&gt;
                auto = auto + 1&lt;br /&gt;
            else&lt;br /&gt;
                break&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
        -- for some reason the original template didn&amp;#039;t detect weeks, using days instead&lt;br /&gt;
        -- so preserve that behaviour here&lt;br /&gt;
        if auto == 4 then&lt;br /&gt;
            auto = 3&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    magnitude = max_( auto, min_ )&lt;br /&gt;
&lt;br /&gt;
    local ret = core( diff, abs_diff, magnitude, args.ago )&lt;br /&gt;
&lt;br /&gt;
    if yesno( args.purge ) then&lt;br /&gt;
        -- @todo use mw.title for this&lt;br /&gt;
        ret = ret .. &amp;#039;&amp;amp;nbsp;&amp;lt;span class=&amp;quot;plainlinks&amp;quot;&amp;gt;([&amp;#039; .. frame:preprocess( &amp;#039;{{fullurl:{{FULLPAGENAME}}|action=purge}}&amp;#039; ) .. &amp;#039; update])&amp;lt;/span&amp;gt;&amp;#039;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    return ret&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Alex</name></author>
	</entry>
</feed>