Module:BT

-- Module: BT (Building Time) -- Author: User:panos78 -- Inspired by: Necessity to generate compound numbers from time durations in seconds -- Version: 1.0

-- local BT = {} local LD = mw.loadData('Module:BT/data') local tTools = require('Dev:TableTools') function BT.main(frame) local secs = (frame.args[1]~=nil and tonumber(frame.args[1])>0) and tonumber(frame.args[1]) or 0 local form = (frame.args[2]==nil or frame.args[2]=='' or tonumber(frame.args[2])==0) and 0 or 1 local precision = (frame.args[3]==nil or frame.args[3]=='' or tonumber(frame.args[3])>6 or tonumber(frame.args[3])<1) and 6 or tonumber(frame.args[3]) local space = (frame.args[4]==nil or frame.args[4]=='' or tostring(frame.args[4])==' ') and ' ' or tostring(frame.args[4]) local clr = (frame.args[5]==nil or frame.args[5]=='') and 'color' or (tostring(frame.args[5])=='grey' and 'grey' or 'black') local units = { {'second',LD['s'],LD['seconds'],LD['second'],'blue',1}, {'minute',LD['m'],LD['minutes'],LD['minute'],'blue',60}, {'hour',LD['h'],LD['hours'],LD['hour'],'blue',60}, {'day',LD['D'],LD['days'],LD['day'],'green',24}, {'month',LD['M'],LD['months'],LD['month'],'orange',30}, {'year',LD['Y'],LD['years'],LD['year'],'red',365} }	local pt = precision local txt = '' local b = {} local c = 1 secs = math.ceil(secs < 0 and -1*secs or secs) for k,v in ipairs(units) do c = c*v[6]/(k==tTools.size(units) and 30 or 1) b[k] = c	end local i = tTools.size(units) local z = {} while(i > 0) do z[i] = b[7-i] i = i - 1 end local arr = {} for k,f in ipairs(z) do local tm = math.floor(secs/z[k]) if tTools.isNan(tm) then return table.concat(txt,space) end if pt > 0 and (tm > 0 or tTools.size(arr) > 0) then secs = secs-tm*z[k] if tm > 0 then txt = txt..space..(''..tm..' '..units[7-k][form==0 and 2 or (tm==1 and 4 or 3)]..' ') end pt = tm==0 and pt or (pt-1) end end txt = txt:gsub("^%s*("..space..")%s*(.-)%s*$", "%2") return txt end return BT --