Mô đun:Mẫu Lập lịch theo tuần

Tài liệu mô đun[tạo]
local p = {};
local vi = mw.language.new( "vi" )

p.init = function( frame )
	local year = mw.text.trim( frame.args[1] )
	local out = {}

	local weeks_of_year = {}
	local days_of_week = {}
	local current_weeknum
	
	for daynum = 1, max_daynum( year ), 1 do
		local weeknum = trim_leading( vi:formatDate( "W", year .. "-01-01 + " .. daynum - 1 .. " days" ) )
		if weeknum ~= current_weeknum then
			current_weeknum = weeknum
			days_of_week = { week = current_weeknum }
			table.insert( weeks_of_year, days_of_week )
		end
		table.insert( days_of_week, daynum )
	end
	
	local result = ""
	for _, days in pairs( weeks_of_year ) do
    	result = result .. "| <!--@ Tuần " .. days.week .. " @--> " .. table.concat( days, '|' ) .. " = \n"
	end

	return mw.text.trim( result )
end

function trim_leading( weeknum )
	local result
	if tonumber( weeknum ) < 10 then
		result = mw.text.trim( weeknum , "0" )
	else
		result = weeknum
	end
	return result
end

function max_daynum( year )
	local leap = vi:formatDate( "L", year )
	if leap == "1" then
		return 366
	else
		return 365
	end
end

return p