Jump to content

Module:Time ago

Permanently protected module
From Wikipedia, the free encyclopedia

-- Implement [[Template:Time ago]]

localnumberSpell,yesno-- lazy load

functionnumberSpell(arg)
numberSpell=require('Module:NumberSpell')._main
returnnumberSpell(arg)
end

functionyesno(arg)
yesno=require('Module:Yesno')
returnyesno(arg)
end

localp={}

-- Table to convert entered text values to numeric values.
localtimeText={
['seconds']=1,
['minutes']=60,
['hours']=3600,
['days']=86400,
['weeks']=604800,
['months']=2629800,-- 365.25 * 24 * 60 * 60 / 12
['years']=31557600
}

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

functionp._main(args)
-- Initialize variables
locallang=mw.language.getContentLanguage()
localauto_magnitude_num
localmin_magnitude_num
localmagnitude=args.magnitude
localmin_magnitude=args.min_magnitude
localpurge=args.purge

-- Add a purge link if something (usually "yes" ) is entered into the purge parameter
ifpurgethen
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.
localsuccess,inputTime=pcall(lang.formatDate,lang,'xnU',args[1])
ifnotsuccessthen
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.
localtimeDiff=lang:formatDate('xnU')-inputTime
localabsTimeDiff=math.abs(timeDiff)

ifmagnitudethen
auto_magnitude_num=0
min_magnitude_num=timeText[magnitude]
else
-- Calculate the appropriate unit of time if it was not specified as an argument.
localautoMagnitudeData={
{factor=2,amn=31557600},
{factor=2,amn=2629800},
{factor=2,amn=86400},
{factor=2,amn=3600},
{factor=2,amn=60}
}
for_,tinipairs(autoMagnitudeData)do
ifabsTimeDiff/t.amn>=t.factorthen
auto_magnitude_num=t.amn
break
end
end
auto_magnitude_num=auto_magnitude_numor1
ifmin_magnitudethen
min_magnitude_num=timeText[min_magnitude]
else
min_magnitude_num=-1
end
end

ifnotmin_magnitude_numthen
-- Default to seconds if an invalid magnitude is entered.
min_magnitude_num=1
end

localresult_num
localmagnitude_num=math.max(min_magnitude_num,auto_magnitude_num)
localunit=timeUnits[magnitude_num].unit
ifunitandabsTimeDiff>=864000then
localDate=require('Module:Date')._Date
localinput=lang:formatDate('Y-m-d H:i:s',args[1])-- Date needs a clean date
input=Date(input)
ifinputthen
localid
ifinput.hour==0andinput.minute==0then
id='currentdate'
else
id='currentdatetime'
end
result_num=(Date(id)-input):age(unit)
end
end
result_num=result_numormath.floor(absTimeDiff/magnitude_num)

localpunctuation_key,suffix
iftimeDiff>=0then-- Past
ifresult_num==1then
punctuation_key=1
else
punctuation_key=2
end
ifargs.ago==''then
suffix=''
else
suffix=' '..(args.agoor'ago')
end
else-- Future
ifargs.ago==''then
suffix=''
ifresult_num==1then
punctuation_key=1
else
punctuation_key=2
end
else
suffix=' time'
ifresult_num==1then
punctuation_key=3
else
punctuation_key=4
end
end
end
localresult_unit=timeUnits[magnitude_num][punctuation_key]

-- Convert numerals to words if appropriate.
localspell_out=args.spellout
localspell_out_max=tonumber(args.spelloutmax)
localresult_num_text
ifspell_outand(
(spell_out=='auto'and1<=result_numandresult_num<=9andresult_num<=(spell_out_maxor9))or
(yesno(spell_out)and1<=result_numandresult_num<=100andresult_num<=(spell_out_maxor100))
)
then
result_num_text=numberSpell(result_num)
else
result_num_text=tostring(result_num)
end

-- numeric or string
localnumeric_out=args.numeric
localresult=""
ifnumeric_outthen
result=tostring(result_num)
else
result=result_num_text..' '..result_unit..suffix-- Spaces for suffix have been added in earlier.
end

returnresult..purge
end

functionp.main(frame)
localargs=require('Module:Arguments').getArgs(frame,{
valueFunc=function(k,v)
ifvthen
v=v:match('^%s*(.-)%s*$')-- Trim whitespace.
ifk=='ago'orv~=''then
returnv
end
end
returnnil
end,
wrappers='Template:Time ago'
})
returnp._main(args)
end

returnp