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