模組:ISODate
外观
local getArgs = require('Module:Arguments').getArgs
local p = {}
function p.dates(frame)
local args = getArgs(frame)
return p._dates(args)
end
function p._dates(args)
args[1] = args[1]:gsub(" "," ");
-- Converts Chinese language dates into ISO 8601 format
local y, m, d
y, m, d = string.match(args[1], '^(%d+)年-(%d%d?)月(%d%d?)日$');
if y then
return mw.getCurrentFrame():callParserFunction('#time', 'Y-m-d', y .. '-' .. m .. '-' .. d)
end
y, m = string.match(args[1], '^(%d+)年(%d%d?)月$');
if y then
return mw.getCurrentFrame():callParserFunction('#time', 'Y-m', y .. '-' .. m)
end
y = string.match(args[1], '^(%d+)年$');
if y then
return mw.getCurrentFrame():callParserFunction('#time', 'Y', y)
end
-- Converts English language dates into ISO 8601 format
m, y = string.match(args[1], '^(%a+) (%d+)$');
if y then
return mw.getCurrentFrame():callParserFunction('#time', 'Y-m', args[1])
end
d, m, y = string.match(args[1], '^(%d%d?) (%a+) (%d+)$');
if y then
return mw.getCurrentFrame():callParserFunction('#time', 'Y-m-d', args[1])
end
m, d, y = string.match(args[1], '^(%a+) (%d%d?), (%d+)$');
if y then
return mw.getCurrentFrame():callParserFunction('#time', 'Y-m-d', args[1])
end
-- Tries to fix non-standard ISO 8601 format
y = string.match(args[1], '^(%d+)$');
if y then
return mw.getCurrentFrame():callParserFunction('#time', 'Y', args[1])
end
if string.match(args[1], '^%d+%-%d%d?%-%d%d?$') then
return mw.getCurrentFrame():callParserFunction('#time', 'Y-m-d', args[1])
end
if string.match(args[1], '^%d+%-%d%d?$') then
return mw.getCurrentFrame():callParserFunction('#time', 'Y-m', args[1])
end
-- Other cases
return mw.getCurrentFrame():callParserFunction('#time', 'Y-m-d', 'error')
end
return p