This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing.
This Lua module is used on 94,000+ pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.
-- Replacement for [[Template:Time ago]]localnumberSpell=require('Module:NumberSpell')._mainlocalyesno=require('Module:Yesno')localp={}-- Table to convert entered text values to numeric values.localtimeText={['seconds']=1,['minutes']=60,['hours']=3600,['days']=86400,['weeks']=604800,['months']=2678400,['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'"},[2678400]={'month','months',"month's","months'"},[31557600]={'year','years',"year's","years'"}}functionp._main(args)-- Initialize variableslocallang=mw.language.getContentLanguage()localauto_magnitude_numlocalmin_magnitude_numlocalresultlocalresult_unitlocalmagnitude=args.magnitudelocalmin_magnitude=args.min_magnitudelocalpurge=args.purgelocalspell_out=args.spelloutlocalspell_out_max=args.spelloutmax-- Add a purge link if something (usually "yes") is entered into the purge parameterifpurgethenpurge=' <span class="plainlinks">(['..mw.title.getCurrentTitle():fullUrl('action=purge')..' purge])</span>'elsepurge=''end-- Check that the entered timestamp is valid. If it isn't, then give an error message.localnoError,inputTime=pcall(lang.formatDate,lang,'U',args[1],true)ifnotnoErrorthenreturn'<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('U',nil,true)-inputTimelocalabsTimeDiff=math.abs(timeDiff)ifmagnitudethenauto_magnitude_num=0min_magnitude_num=timeText[magnitude]else-- Calculate the appropriate unit of time if it was not specified as an argument.localautoMagnitudeData={{denom=63115200,amn=31557600},{denom=5356800,amn=2678400},{denom=172800,amn=86400},{denom=7200,amn=3600},{denom=120,amn=60}}fori,tinipairs(autoMagnitudeData)doifabsTimeDiff/t.denom>=1thenauto_magnitude_num=t.amnbreakendendauto_magnitude_num=auto_magnitude_numor1ifmin_magnitudethenmin_magnitude_num=timeText[min_magnitude]elsemin_magnitude_num=-1endendifnotmin_magnitude_numthen-- Default to seconds if an invalid magnitude is entered.min_magnitude_num=1endlocalmagnitude_num=math.max(min_magnitude_num,auto_magnitude_num)localresult_num=math.floor(absTimeDiff/magnitude_num)localpunctuation_key,suffixiftimeDiff>=0then-- Pastifresult_num==1thenpunctuation_key=1elsepunctuation_key=2endifargs.ago==''thensuffix=''elsesuffix=' '..(args.agoor'ago')endelse-- Futureifargs.ago==''thensuffix=''ifresult_num==1thenpunctuation_key=1elsepunctuation_key=2endelsesuffix=' time'ifresult_num==1thenpunctuation_key=3elsepunctuation_key=4endendendresult_unit=timeUnits[magnitude_num][punctuation_key]-- Convert numerals to words if appropriate.spell_out_max=tonumber(spell_out_max)-- Would cause script errors if not a number.localresult_num_textif(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))thenresult_num_text=numberSpell(result_num)elseresult_num_text=tostring(result_num)endresult=result_num_text..' '..result_unit..suffix-- Spaces for suffix have been added in earlier.returnresult..purgeendfunctionp.main(frame)localargs=require('Module:Arguments').getArgs(frame,{valueFunc=function(k,v)ifvthenv=v:match('^%s*(.-)%s*$')-- Trim whitespace.ifk=='ago'orv~=''thenreturnvendendreturnnilend,wrappers='Template:Time ago'})returnp._main(args)endreturnp