本模块用于将各类日期转换为ISO 8601格式或中文日期,以方便{{#time:params}}函数和其他模板调用。

用法 siu-kái

输出ISO日期 siu-kái

使用{{#invoke:Date_Convert|ISODate}}函数。

输入完整日期,只输入年月、只输入年份皆可:

当月和日是个位数时,十位加“0”或不加“0”皆可,模块会自动补“0”,

可以转换英文和非规范的ISO 8601日期:

如果字符串以正确的日期开头,则会转换并忽视后缀文字:

  • {{#invoke:Date_Convert|ISODate|1994年10月26日,武汉}} → 1994
  • {{#invoke:Date_Convert|ISODate|1994年10月26日,武汉|suffix=yes}} → 1994年10月26日,武汉(将suffix字段设为yes展示后缀)

以不合法日期开头则报错:

  • {{#invoke:Date_Convert|ISODate|一九九四年十月二十六日}}错误:时间格式不正确
  • {{#invoke:Date_Convert|ISODate|1994年13月32日}} → 1994
  • {{#invoke:Date_Convert|ISODate|一九九四年十月二十六日|error=ignore}} → 一九九四年十月二十六日(将error字段设为ignore返回输入值)

ISO 8601标准只规定了1583年之后的日期标准,请勿用本模块表示这之前的日期,否则可能会得到不期待的结果:

  • {{#invoke:Date_Convert|ISODate|25年8月5日(东汉建立)|suffix=yes}}错误:时间格式不正确

{{#invoke:ISODate|dates|xxx}}等效於此{{#invoke:Date_Convert|ISODate|xxx}}

输出中文日期 siu-kái

使用{{#invoke:Date_Convert|ChineseDate}}函数。

输入完整日期,只输入年月、只输入年份皆可:

当月和日是个位数时,十位加“0”或不加“0”皆可,模块会自动补“0”,

可以转换英文和非规范的ISO 8601日期:

如果字符串以正确的日期开头,则会转换并忽视后缀文字:

以不合法日期开头则报错:

  • {{#invoke:Date_Convert|ChineseDate|一九九四年十月二十六日}}错误:时间格式不正确
  • {{#invoke:Date_Convert|ChineseDate|1994年13月32日}} → 1994 4î
  • {{#invoke:Date_Convert|ChineseDate|一九九四年十月二十六日|error=ignore}} → 一九九四年十月二十六日(将error字段设为ignore返回输入值)

ISO 8601标准只规定了1583年之后的日期标准,请勿用本模块表示这之前的日期,否则可能会得到不期待的结果:

  • {{#invoke:Date_Convert|ChineseDate|25年8月5日(东汉建立)}}错误:时间格式不正确

{{#invoke:Date_Convert|ChineseDate|xxx}}等效於此{{#invoke:Chinese_date|main|xxx}}

参见 siu-kái


require('Module:No globals')

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

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

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

local function convert(input)
	input = input
		:gsub("&nbsp;"," ")
		:gsub("%s+"," ")
		
		:gsub('(%a+)[ ,]*(%d+)', '%1 %2');
		
	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+) nî (%d%d?) goe̍h (%d%d?) ji̍t (.*)$');
	if y then
		if #y < 4 then
			y = string.rep(0, 4 - #y) .. y
		end
		return time('Y-m-d', y .. '-' .. m .. '-' .. d) , suf
	end

	y, m, suf = string.match(input, '^(%d+) nî (%d%d?) goe̍h (.*)$');
	if y then
		if #y < 4 then
			y = string.rep(0, 4 - #y) .. y
		end
		return time('Y-m', y .. '-' .. m) , suf
	end

	y, suf = string.match(input, '^(%d+) nî (.*)$');
	if y then
		if #y < 4 then
			y = string.rep(0, 4 - #y) .. y
		end
		return time('Y', y) , suf
	end
	
	m, d, suf = string.match(input, '^(%d%d?) goe̍h (%d%d?) ji̍t (.*)$');
	if m and tonumber(m) then
		return time('m-d', m .. '-' .. d) , suf
	end

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

	return time('Y-m-d', 'error')
end

function p._converttime(input, showsuf)
	local date, suf = convert(input)
	if (yesno(showsuf)) then
		return date .. suf
	else
		return date
	end
end


function p._ChineseDate(args, error)
	local date, suffix = convert(args[1])
	local errorMessage = time('Y-m-d', 'error')
	suffix = args.suf and suffix or ''
	if string.match(date, '^%d+%-%d%d%-%d%d$') then
		return time('Y nî Fj ji̍t', date):gsub("^0+","") .. suffix
	end
	if string.match(date, '^%d+%-%d%d$') then
		return time('Y nî F', date):gsub("^0+","") .. suffix
	end
	if string.match(date, '^%d+$') then
		return time('Y nî', date):gsub("^0+","") .. suffix
	end
	if string.match(date, '^%d%d%-%d%d$') then
		return time('Fj ji̍t', date):gsub("^0+","") .. suffix
	end
	if date == errorMessage and args.error == 'ignore' then
		return args[1]
	end
	--- return error('Chinese_date')
	return '<strong class="error">错误:时间格式不正确</strong>[[Category:Template: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 = time('Y-m-d', 'error')

	if returnval == errorMessage and args.error == 'ignore' then
		return args[1]
	end
	if returnval == errorMessage then
		--- return error('ISODate')
		return '<strong class="error">错误:时间格式不正确</strong>[[Category:Template:ISODate使用錯誤]]'
	end
	return returnval .. (args.suffix and suf or '')
end

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

return p