Moduł:DNA

Z Wikipedii, wolnej encyklopedii
 Dokumentacja modułu [stwórz] [odśwież]
local p = {}
--[[
  Vide: WP:DNA.
]]

-- trim string
function trim(s)
   return s:gsub("^%s+", ""):gsub("%s+$", "")
end

--[[
	Previous DNA [export].
]] 
function p.prevDNA(frame)
	--local format = trim(frame.args[1])
	return os.date( "%Y-%m-%d", prevDNA() )
end
--[[
	Next or current DNA [export].
]] 
function p.nextCurrentDNA(frame)
	--local format = trim(frame.args[1])
	return os.date( "%Y-%m-%d", nextCurrentDNA() )
end

--[[
	Previous DNA.
]] 
function prevDNA()
	local today = os.date("*t")
	local current = os.date("*t", getDnaDate(0));
	if today.day <= current.day then
		return getDnaDate(-1)
    end
	return getDnaDate(0)
end

--[[
	Next or current DNA.
]] 
function nextCurrentDNA()
	local today = os.date("*t")
	local current = os.date("*t", getDnaDate(0));
	if today.day <= current.day then
		return getDnaDate(0)
    end
	return getDnaDate(1)
end

--[[
	Get DNA date.
	
	@param diff Difference from now.
]]
function getDnaDate(diff)
	-- get date table/object
	local now = os.date("*t");
	local month = now.month + diff;
	if month > 12 then
	    month = month - 12
	    now.year = now.year + 1
	elseif month <= 0 then
	    month = month + 12
	    now.year = now.year - 1
	end
	-- create time
	return os.time{year=now.year, month=month, day=month, hour=0}
end
--[[
--Tests:
print(  os.date( "%Y-%m-%d", getDnaDate(-2) )  )
print(  os.date( "%Y-%m-%d", getDnaDate(-1) )  )
print(  os.date( "%Y-%m-%d", getDnaDate(0) )  )
print(  os.date( "%Y-%m-%d", getDnaDate(1) )  )
print(  os.date( "%Y-%m-%d", getDnaDate(2) )  )
]]

return p