模块:Timebomb

来自 WinStory Wiki
跳转到导航 跳转到搜索

此模块的文档可以在 手动:Timebomb/doc 创建

require('strict')
local getArgs = require('Module:Arguments').getArgs
local lang = mw.language.new('zh-CN')
local frame = mw.getCurrentFrame()

local p = { }

local function trim(str)
	if str then
		return str:gsub("^%s*", ""):gsub("%s*$", "")
	else
		return nil
	end
end

local function length(timebomb, compiled)
	local ts1 = lang:formatDate('U', timebomb)
	local ts2 = lang:formatDate('U', compiled)
	
	return ts1 - ts2
end

local function fixed(timebomb, compiled)
	local length = length(timebomb, compiled)
	local clean = lang:formatDate('Y 年 n 月 j 日',timebomb)
	
	if length < 0 and compiled then
		error('时间炸弹日期早于编译日期')
	end

	if compiled then
		local msg = mw.message.new('timebomb fixed', clean,lang:formatDuration(length, {"days"}):gsub('day','天'))
		return msg:plain()
	else
		return clean
	end
end

local function dynamic(timebomb)
	local length = length(timebomb, 'now')
	local msg = mw.message.new('timebomb dynamic', lang:formatDuration(length, {"days"}):gsub('day','天'))
	return msg:plain()
end

function p.main(frame)
	local args = getArgs(frame)
	local expiry = trim(frame.args[1] or args.timebomb)
	
	if not expiry or expiry:len() == 0 then
		return ""
	end
	
	if expiry:byte() == 43 then
		return dynamic(expiry)
	else
		return fixed(expiry, args.compiled)
	end
end

return p