Modul:Ceribandin/Balyozxane/zaman

Ji Wîkîpediya, ensîklopediya azad.
Belgekirina modulê[biafirîne]
--bu fonksiyon girilen bir yazıda birinci değeri bulup
--onu ikinci değer ile değiştirmeye yarar
local function bulvedeg(yazi, bir, iki)
	return mw.ustring.sub(mw.ustring.gsub(yazi, bir, iki), 1, -1)
end

--yerelde kod kısaltmak için
local bul = mw.ustring.find

function ayarlaMO(yil)
	return bulvedeg(yil, "-([%d])", "MÖ %1")
end

function temizle(yil)
	local son = bulvedeg(yil, "^MÖ ", "-")
	son = bulvedeg(son, "%'l[ea]r$", "")
	son = bulvedeg(son, ". yüzyıl", "")
	son = bulvedeg(son, ". binyıl", "")
	
	return son
end

--bu fonksiyon girilen bir yılın bulunduğu on yılı saptayarak
--onun çıktısını verir, mesela 2019 girilirse 2010
--1888 girilirse de 1880 çıktısını verir
function onYil(yil, arti)
	yil = temizle(yil)
	local tire = bul(yil, "^-") and "-" or nil
	local oy = bulvedeg(yil, "^-", "")
	local bas = string.len(oy) == 1 and "0" or mw.ustring.sub(oy, 1, -2) .. 0
	--eğer yılın sondan bir önceki sayısı "1,3,4,6,9" ise
	--lar eki kullanılacak, bunların dışında ler
	local harf = (bul(bas, "[13469][%d]$") or bas == "0") and "a" or "e"
	--ve eğer yılın bulunduğu onyılı bulmak istiyorsak son rakamı
	--her zaman 0 isteriz, mesela 2015 yılı giriliyse bu yıl,
	--2010 onyılına aittir
	
	local hesap = (tire and tire or "") .. bas
	
	if arti then return onYil(hesap+arti) else return hesap .. "'l" .. harf .. "r" end
end

function yuzYil(yil, arti)
	yil = temizle(yil)
	local tire = bul(yil, "^-") and "-" or nil
	yy = bulvedeg(yil, "^-", "")
	local bas = (string.len(yy) == 1 or string.len(yy) == 2) and 1 or tonumber(mw.ustring.sub(yy, 1, -3)+1)
	
	local hesap = (tire and tire or "") .. tostring(bas)
	
	if arti then return hesap+arti .. ". yüzyıl" else return hesap .. ". yüzyıl" end
end

function binYil(yil, arti)
	yil = temizle(yil)
	local tire = bul(yil, "^-") and "-" or nil
	local by = bulvedeg(yil, "^-", "")
	local ilk = mw.ustring.sub(by, 1, 1)
	local son = mw.ustring.sub(by, -1)
	
	local sonuc = (string.len(by) ~= 1 and son ~= "0") and tonumber(ilk)+1 or tonumber(ilk)
	sonuc = string.len(by) == 1 and 1 or sonuc
	
	local hesap = (tire and tire or "") .. tostring(sonuc)
	
	if arti then return hesap+arti .. ". binyıl" else return hesap .. ". binyıl" end
end

function YIL(frame)
	local args = frame.args[1] and frame.args or frame:getParent().args
	args[1] = temizle(args[1])
	if args[2] then args[1] = tonumber(args[1])+tonumber(args[2]) end
	return args[1] == "0" and "Milat" or ayarlaMO(args[1])
end

function ONYIL(frame)
	local args = frame.args[1] and frame.args or frame:getParent().args
	return ayarlaMO(onYil(args[1], args[2]))
end

function YUZYIL(frame)
	local args = frame.args[1] and frame.args or frame:getParent().args
	return ayarlaMO(yuzYil(args[1], args[2]))
end

function BINYIL(frame)
	local args = frame.args[1] and frame.args or frame:getParent().args
	return ayarlaMO(binYil(args[1], args[2]))
end

return {YIL = YIL, ONYIL = ONYIL, YUZYIL = YUZYIL, BINYIL = BINYIL}