◆42Lab是非盈利性、非官方的云图计划维基百科。
◆诚邀指挥官们加入编辑部共同建设wiki。点这里加入42Lab
如果您发现某些内容错误/空缺,请勇于修正/添加!参与进来其实很容易!
◆有任何意见、建议、纠错,欢迎在任意评论区评论
◆编辑讨论QQ群741423564 微博@GFwiki少前百科 欢迎您

模块:Duration

来自42LAB

可在模块:Duration/doc创建此模块的帮助文档

local p = {}
local lastdays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

local function _countGameDays(init, final)
	local diff = 0
	if init[1] == final[1] then
		if init[2] == final[2] then diff = final[3] - init[3] + 1
		else
			diff = final[3] + lastdays[init[2]] - init[3] + 1
			if init[2] == 2 and init[1] % 4 == 0 then diff = diff + 1 end
			for i = init[2] + 1, final[2] - 1 do
				diff = diff + lastdays[i]
			end
		end
		if init[4] < 5 then diff = diff + 1 end
		if final[4] < 5 then diff = diff - 1 end
	else
		diff = _countGameDays(init, {init[1], 12, 31, 12, 0}) +
			_countGameDays({final[1], 1, 1, 12, 0}, final)
		for i = init[1] + 1, final[1] - 1 do
			diff = diff + (i % 4 == 0 and 366 or 365)
		end
	end
	return diff
end

local function str2table(str)
	local t = {}
	for s in mw.text.gsplit(str, '-', true) do
		table.insert(t, tonumber(s))
	end
	return t
end

function p.duration(frame)
	local args = frame == mw.getCurrentFrame() and frame:getParent().args or frame
	return mw.getContentLanguage():formatDuration(tonumber(args[1]))
end

function p.durationTemplate(frame)
	return mw.getContentLanguage():formatDuration(tonumber(mw.text.trim(frame.args[1])))
end

function p.countGameDays(frame)
	local args = frame == mw.getCurrentFrame() and frame:getParent().args or frame
	local init, final = str2table(args[1]), str2table(args[2])
	return _countGameDays(init, final)
end

function p.countGameDaysTemplate(frame)
	local init, final = str2table(mw.text.trim(frame.args[1])), str2table(mw.text.trim(frame.args[2]))
	return _countGameDays(init, final)
end
return p