모듈:Date formatting
둘러보기로 이동
검색으로 이동
local p = {}
-- 일본어 위키에서 가져온 모듈입니다.
-- 정규식 패턴
-- %d: 숫자, %a: 알파벳
local p0 = '((%d+)년 (%d+)월 (%d+)일)'
local p1 = '((%d%d%d%d)(%d%d)(%d%d))' -- yyyymmdd
local p2 = '((%d+)/(%d+)/(%d+))' -- yyyy/mm/dd
local p3 = '((%a+)%.? (%d+)%a*,? (%d+))' -- Month(.) day(st/nd/rd/th)(,) year
local p4 = '((%d+)%a* (%a+)%.?,? (%d+))' -- day(st/nd/rd/th) Month(.)(,) year
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', Sept = '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 )
local matched, y, m, d = '', '', '', ''
if str:match( p0 ) ~= nil then
matched, y, m, d = str:match( p0 )
-- elseif str:match( p1 ) ~= nil then
-- matched, y, m, d = str:match( p1 )
elseif str:match( p2 ) ~= nil then
matched, y, m, d = str:match( p2 )
elseif str:match( p3 ) ~= nil then
matched, m, d, y = str:match( p3 )
m = months[ m ]
elseif str:match( p4 ) ~= nil then
matched, d, m, y = str:match( p4 )
m = months[ m ]
end
-- 실패 시 그대로 반환
if matched == nil or m == nil then
return str
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