ሞድዩል:Times
Documentation for this module may be created at ሞድዩል:Times/doc
local z = {}
local months = {
'ጥሪ','ለካቲት','መጋቢት','ሚያዝያ','ግንቦት','ሰነ','ሓምለ','ነሓሰ','መስከረም','ጥቅምቲ','ሕዳር','ታሕሳስ'}
function z.NameOfMonth(month)
-- Function that returns the name of the month, where month is a number between 1 and 12.
-- If not, the month value is returned.
-- For example, 2 --> ለካቲት
-- 02 --> ለካቲት
-- ሚያዝያ --> ሚያዝያ
-- ግንቦት --> ግንቦት
return months[tonumber(month)] or month
end
function z.Time(frame)
-- Function that formats a date
-- The only mandatory parameter is the year or 3.
-- Get the arguments with which the function is called
local arguments = {}
local parent = {}
if frame == mw.getCurrentFrame() then
if frame.args[3] or frame.args["ዓመት"] then
arguments = frame.args
else
parent = frame:getParent()
arguments = parent.args
end
else
arguments = frame
end
local link = arguments["link"] ~= "no"
-- Obtain the day, the name of the month and the year including for negative years a.d.
local day = arguments["መዓልቲ"] or arguments[1] or ''
if day ~='' then
day=tonumber(day) or day -- Remove leading zeros of the day.
end
local month = arguments["ወርሒ"] or arguments[2] or ''
if month~='' then
month =months[tonumber(month)] or month
end
local year=tonumber(arguments["ዓመት"] or arguments[3]) or 0
if year < 0 then
year = -year .. ' ቅ.ክ.'
end
local calendar = ''
if arguments["calendar"] == 'julian' then
calendar = '<sup>[[Julian calendar|jul.]]</sup>'
end
-- Format the date depending on whether the day, month or year are reported
if day ~='' then
if link then
return '[[' .. day .. ' ' .. month .. ']] [[' .. year .. ']]' .. calendar
else
return day .. ' ' .. month .. ' ' .. year .. calendar
end
elseif month~='' then
if arguments["uppercase"] == 'yes' then
month = mw.language.new('ti'):ucfirst(month)
end
if link then
return '[[' .. month .. ']] [[' .. year .. ']]'
else
return month .. '' .. year
end
elseif year ~= 0 then
if link then
return '[[' .. year .. ']]'
else
return tostring(year)
end
end
end
function getNumberMonth(nameMonth)
local numberMonth = ''
local nameMonths = {
['01'] = 'ጥሪ', ['02'] = 'ለካቲት', ['03'] = 'መጋቢት', ['04'] = 'ሚያዝያ', ['05'] = 'ግንቦት', ['06'] = 'ሰነ', ['07'] = 'ሓምለ', ['08'] = 'ነሓሰ',
['09'] = 'መስከረም', ['9'] = 'መስከረም', ['10'] = 'ጥቅምቲ', ['11'] = 'ሕዳር', ['12'] = 'ታሕሳስ'
}
for k, n in pairs(nameMonths) do
if nameMonths[k] == mw.ustring.lower(nameMonth) then
numberMonth = k
end
end
if numberMonth == nil or numberMonth == '' then
numberMonth = '00'
end
return numberMonth
end
function setString(String)
local day, month, year
local error = '<strong class="error">Invalid date string</strong>'
local valueDay = mw.ustring.gsub(String,'(%d+) %a+ ? %d+.*','%1')
if valueDay == nil or valueDay == '' or mw.ustring.len(valueDay) > 2 or type(tonumber(valueDay)) ~= 'number' then
return error
else
day = mw.ustring.format('%02d', tonumber(valueDay))
end
local valueMonth = getNumberMonth(mw.ustring.gsub(String,'%d+ (%a+) ? %d+.*','%1'))
if valueMonth == '00' then
return error
else
month = mw.ustring.format('%02d',tonumber(valueMonth))
end
local valueYear = mw.ustring.gsub(String,'%d+ %a+ ? (%d+).*','%1')
if valueYear == nil or valueYear == '' or type(tonumber(valueYear)) ~= 'number' then
return error
else
year = mw.ustring.format('%04d',tonumber(valueYear))
end
local newString = year..month..day
return newString
end
function z.Numerical(frame)
return setString(frame.args[1])
end
return z