跳转到内容

模組:Date Convert

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由SunAfterRain留言 | 贡献2019年11月13日 (三) 09:56编辑。这可能和当前版本存在着巨大的差异。

require('Module:No globals')

local getArgs = require('Module:Arguments').getArgs
local p = {}

local function error(template)
	return '<strong class="error">错误:时间格式不正确</strong>[[Category:Template:' .. template .. '使用錯誤]]'
end

local function toChineseDate(format, date, error)
	return mw.getCurrentFrame():callParserFunction('#time', format, date)
end

local function toISO(format, string)
	return mw.getCurrentFrame():callParserFunction('#time', format, string)
end

local function convert(input)
	input = input:gsub("&nbsp;"," ");
	input = input:gsub("%s+"," ");
	local y, m, d, suf
	local datePatternList = {
		-- English date format
		{'%d%d? ?%a+[ ,]*%d+', 'Y-m-d'},	-- 26 Oct 1994
		{'%a+ ?%d%d?[ ,]+%d+', 'Y-m-d'},	-- Oct 26, 1994
		{'%a+[ ,]*%d%d%d%d+', 'Y-m'},		-- Oct 1994
		{'%a+ ?%d%d?', 'Y-m-d'},			-- Oct 26
		{'%d%d? *%a+', 'Y-m-d'},			-- 26 Oct
		-- Slash or hyphen date format
		{'%d+/%d%d?/%d+', 'Y-m-d'},			-- 1994/10/26 or 10/26/1994
		{'%d+%.%d%d?%.%d+', 'Y-m-d'},			-- 1994.10.26 or 26.10.1994
		{'%d%d?/%d%d?', 'Y-m-d'},			-- 10/26
		{'%d+%-%d%d?%-%d+', 'Y-m-d'},		-- 1994-10-26 or 26-10-94
		{'%d%d%d%d+%-%d%d?', 'Y-m'},		-- 1994-10
		{'%d%d%d%d', 'Y'},					-- 1994
	}

	y, m, d, suf = string.match(input, '^(%d+)年(%d%d?)月(%d%d?)日(.*)$');
	if y then
		if #y < 4 then
			y = string.rep(0, 4 - #y) .. y
		end
		return toISO('Y-m-d', y .. '-' .. m .. '-' .. d) , suf
	end

	y, m, suf = string.match(input, '^(%d+)年(%d%d?)月(.*)$');
	if y then
		if #y < 4 then
			y = string.rep(0, 4 - #y) .. y
		end
		return toISO('Y-m', y .. '-' .. m) , suf
	end

	y, suf = string.match(input, '^(%d+)年(.*)$');
	if y then
		if #y < 4 then
			y = string.rep(0, 4 - #y) .. y
		end
		return toISO('Y', y) , suf
	end

	for _, value in ipairs(datePatternList) do
		local str, suf = string.match(input, '^(' .. value[1] .. ')(.*)$');
		if str then
			return toISO(value[2], str), suf
		end
	end

	return error('ISODate')
end


function p._ChineseDate(args, error)
	local date, suffix = convert(args[1])
	local errorMessage = '<strong class="error">错误:时间格式不正确</strong>'
	suffix = args.suf and suffix or ''
	if string.match(date, '^%d+%-%d%d%-%d%d$') then
		return toChineseDate('Y年Fj日', date):gsub("^0+","") .. suffix
	end
	if string.match(date, '^%d+%-%d%d$') then
		return toChineseDate('Y年F', date):gsub("^0+","") .. suffix
	end
	if string.match(date, '^%d+$') then
		return toChineseDate('Y年', date):gsub("^0+","") .. suffix
	end
	if date == error and args.error == 'ignore' then
		return args[1]
	end
	return error('Chinese_date')
end

function p.ChineseDate(frame)
	local args = getArgs(frame)
	return p._ChineseDate(args)
end

function p._ISODate(args, error)
	local returnval, suf = convert(args[1])
	local errorMessage = '<strong class="error">错误:时间格式不正确</strong>'

	if returnval == errorMessage and args.error == 'ignore' then
		return args[1]
	end

	return returnval .. (args.suffix and suf or '')
end

function p.ISODate(frame)
	local args = getArgs(frame)
	return p._ISODate(args)
end

return p