Thismodule is rated asbeta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected.
This Lua module is used onapproximately 249,000 pages. To avoid major disruption and server load, any changes should be tested in the module's/sandbox or/testcases subpages, or in your ownmodule sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on thetalk page before implementing them.
This module implements{{Duration}}. It tries to add a microformat to a duration (hh:mm:ss ormm:ss) whenever possible. If the module is unable to add hAudio microformat (and the input does not contain the microformat) then the page will be included inCategory:Duration without hAudio microformat (2,287).
Usage
This template should normally be called through{{Duration}}, and used as{{#invoke:Duration|main|duration=duration}} when included in other templates.
Valid parameters are numbered parameters 1, 2 and 3, and unnumbered parameters|h=,|m=,|s= and|duration=.|duration= should not be used in{{Duration}}, as|1= can provide the same function.
The two triplets of parameters accept numbers as inputs (hours, minutes, seconds respectively). Only|3= and|s= can contain decimals; the others must be integers. The seconds value must not exceed 60, and the minutes value must not exceed 60 if there is an hours value. If the inputs are not recognized as numbers, an error message is returned.
|1= and|duration= accept strings. If the string contains a microformat then the string will be returned without modification. Otherwise, the module will attempt to add an hAudio microformat to the first unformatted duration found. If the string contains more than one duration, only the first one will have a microformat added.
localp={}functionp._error(error_str)return'[[Category:Duration with input error]]<strong class="error">Error in Module:Duration: '..error_str..'</strong>'endfunctionp.main(frame)localargs=require('Module:Arguments').getArgs(frame,{wrappers={'Template:Duration','Template:Duration/sandbox'}})localtmp=args.durationorargs[1]or''localduration={}iftonumber(args[1])orargs[2]orargs[3]thenifargs[4]thenreturnp._error('Parameter number 4 should not be specified')endifnotargs[1]orargs[1]==''thenduration={args[2]or0,args[3]or0}elseduration={args[1],args[2]or0,args[3]or0}endtmp=nilfork,vinipairs(duration)doduration[k]=tonumber(v)ifnotduration[k]thenreturnp._error('Invalid values')endendelseifargs.horargs.morargs.sthenifnotargs.horargs.h==''thenduration={args.mor0,args.sor0}elseduration={args.h,args.mor0,args.sor0}endtmp=nilfork,vinipairs(duration)doduration[k]=tonumber(v)ifnotduration[k]thenreturnp._error('Invalid values')endendelseifmw.ustring.find(tmp,'class="duration"',1,yes)thenreturntmpend-- if there is already a microformat, don't do anythingduration=mw.text.split(mw.ustring.match(tmp,'%d*:?%d+:%d+%.?%d*')or'',':')-- split into tableifduration[4]thenreturnp._error('Maximum of two colons allowed')endfork,vinipairs(duration)doduration[k]=tonumber(v)or0end-- convert values to numbersendifduration[3]thenif(duration[1]+duration[2]+duration[3])==0thenreturnnilendif(duration[1]~=math.ceil(duration[1]))or(duration[2]~=math.ceil(duration[2]))thenreturnp._error('Hours and minutes values must be integers')endifduration[3]>=60thenreturnp._error('Seconds value must be less than 60')endifduration[2]>=60thenreturnp._error('Minutes value must be less than 60 if hours value is specified')endifduration[2]<10thenduration[2]='0'..duration[2]end-- zero paddingifduration[3]<10thenduration[3]='0'..duration[3]endduration='<span class="duration"><span class="h">'..duration[1]..'</span>:<span class="min">'..duration[2]..'</span>:<span class="s">'..duration[3]..'</span></span>'elseifduration[2]thenif(duration[1]+duration[2])==0thenreturnnilendifduration[1]~=math.ceil(duration[1])thenreturnp._error('Hours and minutes values must be integers')endifduration[2]>=60thenreturnp._error('Seconds value must be less than 60')endifduration[2]<10thenduration[2]='0'..duration[2]end-- zero paddingduration='<span class="duration"><span class="min">'..duration[1]..'</span>:<span class="s">'..duration[2]..'</span></span>'elseduration=''endiftmpandtmp~=''thenifduration~=''thentmp=mw.ustring.gsub(tmp,'%d*:?%d+:%d+%.?%d*',duration,1)elsetmp=tmp..' [[Category:Duration without hAudio microformat]]'endelseifduration~=''thentmp=durationendendreturntmpendreturnp