Mô đun:ViDateTimeParser
Giao diện
Tài liệu mô đun[tạo]
local v = {}
v.months = { "tháng 1", "tháng 2", "tháng 3", "tháng 4", "tháng 5", "tháng 6", "tháng 7", "tháng 8", "tháng 9", "tháng 10", "tháng 11", "tháng 12", "Th1", "Th2", "Th3", "Th4", "Th5", "Th6", "Th7", "Th8", "Th9", "Th10", "Th11", "Th12", "một", "hai", "ba", "tư", "năm", "sáu", "bảy", "tám", "chín", "mười", "mười một", "mười hai" }
v._viAltMonth = { ["giêng"] = "hai", ["bốn"] = "tư" }
function v._monthSearch(t)
local r = {}
for i, mname in ipairs(t) do
r[mname:lower()] = i > 12 and i - 12 > 12 and i - 24 or i - 12 or i
end
return r
end
function v._monthAltNameR(n)
return v._viAltMonth[n] and v._viAltMonth[n] or n
end
v.monthSearch = v._monthSearch(v.months)
function v._parseMonthName(s)
s = s:lower()
local month = v.monthSearch[v._monthAltNameR(s)]
if month then
return month
end
return nil
end
function v._converter (args)
local function parseError()
error(string.format(
"Không thể phân tích cú pháp của ngày '%s'",
date
), 0)
end
if not args[1] and not args.datetime then
error("Ngày giờ cần chuyển đổi không hợp lệ!")
end
local dateTbl = os.date("*t")
local day, month, year = dateTbl.wday, dateTbl.month, dateTbl.year
date = not args[1] and tostring(args.datetime) or tostring(args[1])
local parts = mw.text.split(date, '%W+')
local nParts = #parts
if parts[1] == '' or parts[nParts] == '' or nParts > 7 or nParts < 3 then
parseError()
end
if nParts == 3 or nParts == 4 then
if nParts == 3 then
month, year = date:match('[Tt]háng (%w[%s%S]*)%, (%d[%d]*)')
if not month or not year then
month, year = date:match('(%d[%d]*) [Tt]háng (%w[%s%S]*)')
if not month or not year then
day, month, year = date:match('(%d[%d]*) ([Tt]h%d[%d]*), (%d[%d]*)')
if not day or not month or not year then
parseError()
end
end
end
else
month, year = date:match('[Tt]háng (%w[%s%S]*)% năm (%d[%d]*)')
if not month or not year then
day, month, year = date:match('(%d[%d]*) [Tt]háng (%w[%s%S]*), (%d[%d]*)')
if not day or not month or not year then
parseError()
end
end
end
elseif nParts >= 5 then
day, month, year = date:match('(%d[%d]*) [Tt]háng (%w[%s%S]*) năm (%d[%d]*)')
if not day or not month or not year then
day, month, year = date:match('[Nn]gày (%d[%d]*) [Tt]háng (%w[%s%S]*) năm (%d[%d]*)')
if not day or not month or not year then
parseError()
end
end
end
if v._parseMonthName(month) then
month = v._parseMonthName(month)
end
return year .. "/" .. month .. "/" .. day
end
local p = {}
function p._exportClasses()
return { Vdtp = v }
end
function p._main(args)
local success, ret = pcall(function ()
local vdtp = v._converter(args)
return vdtp
end)
if success then
return ret
end
end
function p.main(frame)
local args = require('Mô đun:Arguments').getArgs(frame)
return p._main(args)
end
return p