Module:Utility

p = {} -- Gets Parent/child frame

function p.getframe(bParent) local oFrame = mw.getCurrentFrame if bParent and oFrame~=nil and oFrame:getParent then oFrame = mw.getCurrentFrame:getParent end return oFrame end

-- Put all arguments into a real table - Returns Parent, child args table function p.getArgs(frame) local tChild = {} local tParent = {} local tChild = p.fetchArgs(frame.args) if frame and frame.getParent(frame) then tParent = p.fetchArgs(frame.getParent(frame).args) end return tParent, tChild end

function p.fetchArgs(tTmp) local tTab = {} if not(tTmp) then return end for name, value in pairs( tTmp ) do     tTab[name] = value end return tTab end

--Check if args are enough for script to run function p.checkArgs(frame, iArgs,bParent) local iCount =0 local tParent, tChild = p.getArgs(frame) local tArgTable = tParent if not(bParent) then tArgTable = tChild end for name, value in pairs( tArgTable ) do     iCount = iCount + 1 if (iCount) == tonumber(iArgs) then return true end end return false end

--Check if date is in appropriate format --Iso YYYY-MM-DD

function p.checkdate(date) local pat="(%d+)-(%d+)-(%d+)" local year,month,day=date:match(pat) if year and month and day then return true end return false end

-- Check if table is empty

function p.isempty(table) if next(table) == nil then return true end return false end

-- round a number

function p.round(num, idp) return tonumber(string.format("%." .. (idp or 0) .. "f", num)) end

-- Calculate difference between two dates function p.datediff( date1, date2) if p.checkDate(date1) and p.checkDate(date2) then date1 = string.sub(date1,0,10) date2 = string.sub(date2,0,10) local tDate1 =p.getdate(date1) local tDate2 =p.getdate(date2) local days = math.abs((os.difftime(tDate1,tDate2))/3600/24) return days end return 0 end

--Get date using Iso format - returns milliseconds function p.getdate(date) if not(date) then date = os.date("%Y-%m-%d") end local pat="(%d%d%d%d)-(%d%d)-(%d%d)" local year,month,day=date:match(pat) local date1=(os.time({month=month,year=year,day=day}) ) return date1 end -- return p