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