Documentation for this module may be created at ሞድዩል:Time/doc

local z = {}

local months = {
       'ጥሪ','ለካቲት','መጋቢት','ሚያዝያ','ግንቦት','ሰነ','ሓምለ','ነሓሰ','መስከረም','ጥቅምቲ','ሕዳር','ታሕሳስ'}

function z.currentDate()
	local d = os.date('!*t')
	
	local Date = {}
	Date.year   = d.year
	Date.month  = d.month
	Date.day    = d.day
	Date.hour   = d.hour
	Date.minute = d.min
	Date.second = d.sec
		
	return Date
end

function validate(Date)
	Date.year   = tonumber(Date.year)
	Date.month  = tonumber(Date.month)
	Date.day    = tonumber(Date.day)
	Date.hour   = tonumber(Date.hour)
	Date.minute = tonumber(Date.minute)
	Date.second = tonumber(Date.second)
	
	-- It remains to validate that it is a valid date
end

function z.age(date1, date2)
	--Function that returns the age in years between two dates
	
	--Dates are assumed to have been previously validated.
	
	if not date1 then
		return -- missing to return an error
	end
	
	if not date2 then
		date2=z.currentDate()
	end
	
	local years = date2.year - date1.year
	
	-- Year 0 does not exist, so it should not be counted
	if date1.year < 0 and date2.year > 0 then
		years = years - 1
	end
	
	--if true then return require('Module:Tables').tostring(date2) end
	
	if date2.month < date1.month or
	   (date2.month == date1.month and date2.day < date1.day) then
	   	years = years - 1
	end
	
	if years < 0 then
		return -- missing to return an error
	elseif years == 0 then
		return 'ዓመት ኣብ ዘይመልእ ግዜ'
	elseif years == 1 then
		return 'ሓደ ዓመት'
	else
		return years .. ' ዓመታት'
	end
end

function z.callFromATemplate(frame)
	function getDate(day, month, year)
		local result={}
	
		if day then
			result.day  = day
			result.month  = month
			result.year = year
			
			validate(result)
			
			return result
		end
	end

	local args = frame.args
	
	local task = z[args[1]]
	local date1  = getDate(args[2], args[3], args[4])
	local date2  = getDate(args[5], args[6], args[7])

	return task(date1, date2)
end
       
return z