local p = {}
local p0 = '((%d+)年(%d+)月(%d+)日)'
local p1 = '((%d%d%d%d)(%d%d)(%d%d))'
local p2 = '((%d+)/(%d+)/(%d+))'
local p3 = '((%a+)%.? (%d+)%a*,? (%d+))'
local p4 = '((%d+)%a* (%a+)%.?,? (%d+))'
local months = {
January = 1, February = 2, March = 3, April = 4, May = 5, June = 6, July = 7, August = 8, September = 9, October = 10, November = 11, December = 12,
Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6, Jul = 7, Aug = 8, Sep = 9, Oct = 10, Nov = 11, Dec = 12
}
local monthsName = { 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' }
local monthsNameShort = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' }
function p.formatDate( str, mode )
if str:match( p0 ) ~= nil then
_, y, m, d = str:match( p0 )
-- elseif str:match( p1 ) ~= nil then
-- _, y, m, d = str:match( p1 )
elseif str:match( p2 ) ~= nil then
_, y, m, d = str:match( p2 )
elseif str:match( p3 ) ~= nil then
_, m, d, y = str:match( p3 )
m = months[ m ]
elseif str:match( p4 ) ~= nil then
_, d, m, y = str:match( p4 )
m = months[ m ]
end
if mode == 'en' then
return string.format( '%s %d, %d', monthsName[ tonumber(m) ], tonumber(d), tonumber(y) )
elseif mode == 'short_en' then
return string.format( '%s. %d, %d', monthsNameShort[ tonumber(m) ], tonumber(d), tonumber(y) )
else
return string.format( '%d年%d月%d日', tonumber(y), tonumber(m), tonumber(d) )
end
end
function p.format( str, mode )
for s in str:gmatch( p0 ) do
str = str:gsub( s, p.formatDate( s, mode ) )
end
-- for s in str:gmatch( p1 ) do
-- str = str:gsub( s, p.formatDate( s, mode ) )
-- end
for s in str:gmatch( p2 ) do
str = str:gsub( s, p.formatDate( s, mode ) )
end
for s in str:gmatch( p3 ) do
str = str:gsub( s, p.formatDate( s, mode ) )
end
for s in str:gmatch( p4 ) do
str = str:gsub( s, p.formatDate( s, mode ) )
end
return str
end
function p.base( f )
local args = f
if f == mw.getCurrentFrame() then
args = require( 'モジュール:ProcessArgs' ).merge( true )
end
local mode = nil
if args.en ~= nil then
mode = 'en'
elseif args.short_en ~= nil then
mode ='short_en'
end
return p.format( args[1], mode )
end
return p
Advertisement
モジュール:Date formatting
Advertisement