Documentation for this module may be created at Mòideal:Teamplaid:FormatDate/doc

local p = {}
 	-- Sgaradh nam paramadairean
	local function Split(str)
		local Date = {}
		local Part=""
		local pos = 0
		Date.y = 0
		Date.m = 0
		Date.d = 0
		pos = mw.ustring.find(str,'-',1,true);
		if pos == 1 then -- Samhla minus aig an toiseach - lorg a-rithist
			pos = mw.ustring.find(str,'-',2,true);
		end
		if not pos then -- Aig deireadh na sreinge mar-thà
			Date.y = tonumber(str);
			if Date.y then
				return true, Date -- Chan eil ach àireamh bliadhna ann
			else
				return false, Date  -- Cha ghabh luach aithneachadh
			end
		end
		Part  = mw.ustring.sub(str,1,pos-1);
		Date.y = tonumber(Part) or 0;
		str = mw.ustring.sub(str,pos+1,   -1);
		pos = mw.ustring.find(str,'-',1,true);

		if not pos or pos == 0 then
			Date.m = tonumber(str) or 0
			Date.d = 0;
			return true, Date;
		end


		Part  = mw.ustring.sub(str,1,pos-1)
		Date.m = tonumber(Part) or 0
		Part  = mw.ustring.sub(str,pos+1,   -1)
		Date.d = tonumber(Part) or 0;
		return true, Date;
	end
	--

	local function CheckDate(Date)
		-- Dearbhadh a' mhìosa
		if Date.m ==  0 then -- gun dearbhadh
			return true;
		end
		if Date.m > 12 or Date.m < 1 then
			return false;
		end
		if Date.d ==  0 then -- cha deach ach mìos a shònrachadh, gun dearbhadh an latha
			return true;
		end
		if ( Date.m == 4 or Date.m == 6  or Date.m == 9  or Date.m == 11) and Date.d > 30 then
			return false;
		end
		if  Date.m == 2 then -- Leigidh sinn seachad na riaghailtean sònraichte Grìogaireach.
			if Date.y % 4 ~= 0 and Date.d > 28 then return false; end
			if Date.y % 4 == 0 and Date.d > 29 then return false; end
		end
		-- Chan eil ach mìosan le 31 latha air am fàgail.
		if Date.d > 31  then return false; end
		return true;
	end
	--
	local function Days2Months(Date)
		Date.m = 1;
		if Date.d > 31 then -- às dèidh an Fhaoillich
			Date.m = 2;
			Date.d = Date.d - 31;
		else
			return true, Date;
		end
		if Date.y % 4 == 0 then -- Leigidh sinn seachad na riaghailtean sònraichte Grìogaireach.
			if Date.d > 29 then  -- às dèidh a' Ghearrain (bliadhna-leum)
				Date.m = 3;
				Date.d = Date.d - 29;
			else
				return true, Date;
			end
		else
			if Date.d > 28 then  -- às dèidh a' Ghearrain (bliadhna àbhaisteach)
				Date.m = 3;
				Date.d = Date.d - 28;
			else
				return true, Date;
			end
		end
		if Date.d > 31 then -- às dèidh a' Mhàirt
			Date.m = 4;
			Date.d = Date.d - 31;
		else
			return true, Date;
		end
		if Date.d > 30 then -- às dèidh a' Ghiblein
			Date.m = 5;
			Date.d = Date.d - 30;
		else
			return true, Date;
		end
		if Date.d > 31 then -- às dèidh a' Chèitein
			Date.m = 6;
			Date.d = Date.d - 31;
		else
			return true, Date;
		end
		if Date.d > 30 then -- às dèidh an Ògmhios
			Date.m = 7;
			Date.d = Date.d - 30;
		else
			return true, Date;
		end
		if Date.d > 31 then -- às dèidh an Iuchair
			Date.m = 8;
			Date.d = Date.d - 31;
		else
			return true, Date;
		end
		if Date.d > 31 then -- às dèidh an Lùnastail
			Date.m = 9;
			Date.d = Date.d - 31;
		else
			return true, Date;
		end
		if Date.d > 30 then -- às dèidh t-Sultain
			Date.m = 10;
			Date.d = Date.d - 30;
		else
			return true, Date;
		end
		if Date.d > 31 then -- às dèidh Dàmhair
			Date.m = 11;
			Date.d = Date.d - 31;
		else
			return true, Date;
		end
		if Date.d > 30 then -- às dèidh t-Samhain
			Date.m = 12;
			Date.d = Date.d - 30;
		else
			return true, Date;
		end
		if Date.d > 31 then -- às dèidh an Dùbhlachd = mearachd
			Date.m = 0;
			Date.d = 0;
			return false, Date;
		else
			return true, Date;
		end
	end
	--

	local function Run(Args)
		-- Cruth a' mhìosa mura deach latha a shònrachadh
		local T_L  = {"Am Faoilleach","An Gearran", "Am Màrt", "An Giblean", "An Cèitean","An t-Ògmhios", "An t-Iuchar", "An Lùnasdal","An t-Sultain","An Dàmhair","An t-Samhain","An Dùbhlachd"}
		-- Cruth a' mhìosa ma chaidh latha a shònrachadh
		local T_L_rel  = {"dhen Fhaoilleach","dhen Ghearran", "dhen Mhàrt", "dhen Ghiblean", "dhen Chèitean","dhen Ògmhios", "dhen Iuchar", "dhen Lùnasdal","dhen t-Sultain","dhen Dàmhair","dhen t-Samhain","dhen Dùbhlachd"}
		local T_M  = {"Faoi.","Gearr.", "Màrt", "Gibl.", "Cèit","Ògmh.", "Iuch.", "Lùna.","Sult.","Dàmh.","Samh.","Dùbh."}
		local T_S  = {"Faoi.","Gearr.",  "Màrt", "Gibl.", "Cèit","Ògmh.", "Iuch.", "Lùna.","Sult.","Dàmh.","Samh.","Dùbh."}
		local Text = "";
		local TextYear  = "";
		local TextMonth = "";
		local TextDay   = "";
		local NBSP = false;
		local LINK = false;
		local VCHR = "";
		local STIL = 'L';
		local IsOk = true;
		local Tbl = {}
		local SortIt = false;
		local Zero = false;
		local Numstr = "";
		local SortTag= "";
		local Arg2 = mw.ustring.lower(mw.text.trim(Args[2] or ""));
		local Arg3 = mw.ustring.lower(mw.text.trim(Args[3] or ""));
		local Arg4 = mw.ustring.lower(mw.text.trim(Args[4] or ""));
		local davor = mw.text.trim(Args['davor'] or "");
		if Arg2 == "nbsp"  or Arg3 == "nbsp"  or Arg4 == "nbsp" then NBSP = true; end
		if Arg2 == "link"  or Arg3 == "link"  or Arg4 == "link" then LINK = true; end
		if Arg2 == "l"     or Arg3 == "l"     or Arg4 == "l"    then STIL = 'L'; end
		if Arg2 == "m"     or Arg3 == "m"     or Arg4 == "m"    then STIL = 'M'; end
		if Arg2 == "s"     or Arg3 == "s"     or Arg4 == "s"    then STIL = 'S'; end
		if mw.ustring.lower(mw.text.trim(Args['Zero'] or "")) ~= "" then Zero = true; end
		if mw.ustring.lower(mw.text.trim(Args['Sort'] or "")) ~= "" then SortIt = true; Zero = true; end
		if davor ~="" then davor = davor .. "&nbsp;"; end
		IsOk, Tbl = Split(Args[1])
		if not  IsOk then
			Text = '<span class="error">[[Teamplaid:FormatDate]]: Chan e ceann-là ISO dligheach a tha ann!</span>'
			return Text
		end
		--Làihean gun mhìos: Àireamhaich mìos is làithean dhe na làithean
		if Tbl.m == 0 and Tbl.d ~= 0  then
			IsOk, Tbl = Days2Months(Tbl)
		end
		if not  IsOk then
			Text = '<span class="error">[[Teamplaid:FormatDate]]: Chan e ceann-là ISO dligheach a tha ann!</span>'
			return Text
		end
		Tbl.y = tonumber(Tbl.y) or 0;
		if Tbl.y == 0 and Tbl.m  == 0 and Tbl.d == 0 then
			Text = '<span class="error">[[Teamplaid:FormatDate]]: Luach mì-dligheach ("0-0-0") airson a\' chinn-là! </span>'
			return Text
		end
		if Tbl.y  < 0 then -- Bhliadhna neoni ga glèidheadh airson "Là is mìos a-mhàin"
			Tbl.y = 0 - Tbl.y
			VCHR = ' v. Chr.';
		end

		IsOk = CheckDate(Tbl);
		if not IsOk then
			Text = '<span class="error">[[Teamplaid:FormatDate]]: Ceann-là mì-dhligheach!'.. table.concat(Tbl,'.')..'</span>'
		return Text
		end

		if Tbl.d > 0 then	-- Là air a sonrachadh, dèan sreang
			if Tbl.d == 1 then
				TextDay =  tostring(Tbl.d) .. 'd&nbsp;'
			elseif Tbl.d == 2 then
				TextDay =  tostring(Tbl.d) .. 'na&nbsp;'
			elseif Tbl.d == 3 then
				TextDay =  tostring(Tbl.d) .. 's&nbsp;'
			else
				TextDay =  tostring(Tbl.d) .. 'mh&nbsp;'
			end
			if Tbl.d < 10 and Zero then
				TextDay = '<span style="visibility:hidden;">0</span>' .. TextDay;
			end
		else
			TextDay = '';
		end

		if Tbl.m > 0 then	-- Mìos air a sonrachadh, dèan sreang
			if STIL == 'S' then
				TextMonth = T_S[Tbl.m] ;
			elseif STIL == 'M' then
				TextMonth = T_M[Tbl.m] ;
			else
				if Tbl.d == 0 then
					TextMonth =T_L[Tbl.m]
				else
					TextMonth = T_L_rel[Tbl.m] ;
				end
			end

			Text = TextDay .. TextMonth
			if LINK then
				if Tbl.d == 0 then
					Linkziel =T_L[Tbl.m]
				elseif Tbl.d == 1 then
					Linkziel = tostring(Tbl.d) .. "d " .. T_L_rel[Tbl.m]
				elseif Tbl.d == 2 then
					Linkziel = tostring(Tbl.d) .. "na " .. T_L_rel[Tbl.m]
				elseif Tbl.d == 3 then
					Linkziel = tostring(Tbl.d) .. "s " .. T_L_rel[Tbl.m]
				else
					Linkziel = tostring(Tbl.d) .. "mh " .. T_L_rel[Tbl.m]
				end
				Text = '[[' .. Linkziel .. '|' .. Text .. ']]';
			end
		end
		-- Là is mìos còmhla an-seo, le ceangal ma dh'fhaoidte

		if Tbl.y ~= 0 then
			if LINK then
				TextYear = '[[' .. tostring(Tbl.y) .. VCHR .. ']]';
			else
				TextYear = tostring(Tbl.y) .. VCHR;
			end
			if Tbl.m > 0 then
				if NBSP then
					TextYear = '&nbsp;' .. TextYear;
				else
					TextYear = ' ' .. TextYear;
				end
			end
		end
		Text = Text .. TextYear;

		if SortIt then
			if VCHR ~= "" then
				Tbl.y = 0 - Tbl.y
			end
			Numstr = string.format('%d%2.2d%2.2d',5000+Tbl.y,Tbl.m,Tbl.d);
			SortTag='<span style="display:none" class="sortkey">' .. Numstr .. '</span>';
			Text = SortTag .. davor .. Text;
		end
		return Text
	end
	--

	local function GetYear(Args)
		local Tbl = {}
		local IsOk = true;
		local Year = 0;
		IsOk, Tbl = Split(Args[1])
		if not IsOk or Tbl.y  == 0 then
			Year = 0;
			return Year;
		end
		return Tbl.y;
	end
	
	local function CountDays(Args)
		local Tbl = {}
		local IsOk = true;
		local Days = 0;
		IsOk, Tbl = Split(Args[1])
		if not IsOk or Tbl.y  == 0 then
			Days = 0;
			return Days;
		end
		if Tbl.m == 0 or  Tbl.m == 1 then
			Days = Tbl.d;
			return Days;
		end
		if Tbl.m == 2 then
			Days = 31 + Tbl.d; 
			return Days;
		end
		if Tbl.y % 4 == 0 then
			Days = 60
		else
			Days = 59;
		end
		if Tbl.m ==  3 then Days = Days + Tbl.d; end
		if Tbl.m ==  4 then Days = Days +  31 + Tbl.d; end
		if Tbl.m ==  5 then Days = Days +  61 + Tbl.d; end
		if Tbl.m ==  6 then Days = Days +  92 + Tbl.d; end
		if Tbl.m ==  7 then Days = Days + 122 + Tbl.d; end
		if Tbl.m ==  8 then Days = Days + 153 + Tbl.d; end
		if Tbl.m ==  9 then Days = Days + 184 + Tbl.d; end
		if Tbl.m == 10 then Days = Days + 214 + Tbl.d; end
		if Tbl.m == 11 then Days = Days + 245 + Tbl.d; end
		if Tbl.m == 12 then Days = Days + 275 + Tbl.d; end
		return Days;
	end


	function p.Execute(frame)
		local FR = frame:getParent()
		return Run(FR.args)
	end

	function p.Sort(frame)
		local FR = frame:getParent()
		FR.args.Sort='1'
		return Run(FR.args)
	end

	function p.DayInYear(frame)
		local FR = frame:getParent()
		local Number = CountDays(FR.args);
		return tostring(Number);
	end

	function p.YearFromISO(frame)
		local FR = frame:getParent()
		local Number = GetYear(FR.args);
		return tostring(Number);
	end


return p