Modul:Vorlage:FormatDate: Unterschied zwischen den Versionen

Aus FreeWiki
Wechseln zu: Navigation, Suche
te>Hephaion
K (Änderte den Schutz von „Modul:Vorlage:FormatDate“: Entsperrwunsch ([Bearbeiten=Nur angemeldete, nicht neue Benutzer] (unbeschränkt) [Verschieben=Nur angemeldete, nicht neue Benutzer] (unbeschränkt)))
te>Antonsusi
Zeile 1: Zeile 1:
local p = {}  
+
local p = {}
        -- Trennen der Parameter
+
-- Trennen der Parameter
        local function Split(str)
+
local function Split(str)
            local Datum = {}
+
local Datum = {}
            local Teil=""
+
local Teil=""
            local pos = 0
+
local pos = 0
            Datum.y = 0
+
Datum.y = 0
            Datum.m = 0
+
Datum.m = 0
            Datum.d = 0
+
Datum.d = 0
            pos = mw.ustring.find(str,'-',1,true);
+
pos = mw.ustring.find(str,'-',1,true);
            if pos == 1 then -- Minuszeichen am Anfang - nochmal suchen
+
if pos == 1 then -- Minuszeichen am Anfang - nochmal suchen
                pos = mw.ustring.find(str,'-',2,true);
+
pos = mw.ustring.find(str,'-',2,true);
            end
+
end
            if not pos then
+
if not pos then -- Bereits am Stringende
                Datum.y = tonumber(str) or 'x'
+
Datum.y = tonumber(str);
                return false, Datum  -- Kein Wert erkennbar
+
if Datum.y then
            end
+
return true, Datum -- Nur eine Jahreszahl
            Teil  = mw.ustring.sub(str,1,pos-1)
+
else
            Datum.y = tonumber(Teil) or 'x'
+
return false, Datum  -- Kein Wert erkennbar
            str = mw.ustring.sub(str,pos+1,  -1)
+
end
            pos = mw.ustring.find(str,'-',1,true);
+
end
            if not pos or pos == 0 then
+
Teil  = mw.ustring.sub(str,1,pos-1);
                return true, Datum   -- Nur Jahr
+
Datum.y = tonumber(Teil) or 0;
            end
+
str = mw.ustring.sub(str,pos+1,  -1);
            Teil  = mw.ustring.sub(str,1,pos-1)
+
pos = mw.ustring.find(str,'-',1,true);
            Datum.m = tonumber(Teil) or 0
+
if not pos or pos == 0 then
            Teil  = mw.ustring.sub(str,pos+1,  -1)
+
return true, Datum-- Nur Jahr
            if #Teil == 0 then
+
end
                return true, Datum   -- Nur Jahr und Monat
+
Teil  = mw.ustring.sub(str,1,pos-1)
            end
+
Datum.m = tonumber(Teil) or 0
            if Datum.m == 0 then
+
Teil  = mw.ustring.sub(str,pos+1,  -1)
                Datum.d = 0
+
if #Teil == 0 then
            else
+
return true, Datum-- Nur Jahr und Monat
                Datum.d = tonumber(Teil) or 0
+
end
            end
+
if Datum.m == 0 then
            return true, Datum  
+
Datum.d = 0;
        end
+
else
        --
+
Datum.d = tonumber(Teil) or 0;
 +
end
 +
return true, Datum;
 +
end
 +
--
  
        local function CheckDate(Date)
+
local function CheckDate(Date)
            if ( Date.m == 4 or Date.m == 6  or Date.m == 9  or Date.m == 11) and Date.d > 30 then
+
-- Monatspruefung
                return false;
+
if Date.m ==  0 then -- keine Pruefung
            end
+
return true;
            if  Date.m == 2 then -- Die greg. Sonderregeln werden ignoriert.
+
end
                if Date.y % 4 ~= 0 and Date.d > 28 then return false; end
+
if Date.m > 12 or Date.m < 1 then
                if Date.y % 4 == 0 and Date.d > 29 then return false; end
+
return false;
            end
+
end
            -- Hier nur noch 31-Tage-Monate übrig.
+
if Date.d ==  0 then -- nur Monat angegeben, keine Tagespruefung
            if Date.d > 31  then return false; end
+
return true;
            return true;
+
end
        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 -- Die greg. Sonderregeln werden ignoriert.
 +
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
 +
-- Hier nur noch 31-Tage-Monate übrig.
 +
if Date.d > 31  then return false; end
 +
return true;
 +
end
 +
--
  
        local function Run(Args)
+
local function Run(Args)
            local T_F = {"Januar","Februar", "März", "April", "Mai","Juni", "Juli", "August","September","Oktober","November","Dezember","Jänner"}
+
local T_L = {"Januar","Februar", "März", "April", "Mai","Juni", "Juli", "August","September","Oktober","November","Dezember","Jänner"}
            local T_M  = {"Jan.","Feb.", "März", "Apr.", "Mai","Juni", "Juli", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."}
+
local T_M  = {"Jan.","Feb.", "März", "Apr.", "Mai","Juni", "Juli", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."}
            local T_S  = {"Jan.","Feb.",  "Mrz", "Apr.", "Mai","Jun.", "Jul.", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."}
+
local T_S  = {"Jan.","Feb.",  "Mrz", "Apr.", "Mai","Jun.", "Jul.", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."}
            local Text = "";
+
local Text = "";
            local  AT = false;
+
local TextYear  = "";
            local NBSP = false;
+
local TextMonth = "";
            local LINK = false;
+
local TextDay  = "";
            local VCHR = "";
+
local idxm = 0 ;  --  Wegen der AT-Regel ein Extraindex fuer den Monat.
            local STIL = 'F';
+
local  AT = false;
            local IsOk = true
+
local NBSP = false;
            local Tbl = {}
+
local LINK = false;
            local SortIt = false;
+
local VCHR = "";
            local Numstr = "";
+
local STIL = 'L';
            local SortTag= "";
+
local IsOk = true
            local Arg2 = mw.ustring.lower(mw.text.trim(Args[2] or ""));
+
local Tbl = {}
            local Arg3 = mw.ustring.lower(mw.text.trim(Args[3] or ""));
+
local SortIt = false;
            local Arg4 = mw.ustring.lower(mw.text.trim(Args[4] or ""));
+
local Numstr = "";
            if Arg2 == "nbsp"  or Arg3 == "nbsp"  or Arg4 == "nbsp" then NBSP = true; end
+
local SortTag= "";
            if Arg2 == "link"  or Arg3 == "link"  or Arg4 == "link" then LINK = true; end
+
local Arg2 = mw.ustring.lower(mw.text.trim(Args[2] or ""));
            if Arg2 == "m"    or Arg3 == "m"    or Arg4 == "m"    then STIL = 'M'; end
+
local Arg3 = mw.ustring.lower(mw.text.trim(Args[3] or ""));
            if Arg2 == "s"    or Arg3 == "s"    or Arg4 == "s"    then STIL = 'S'; end
+
local Arg4 = mw.ustring.lower(mw.text.trim(Args[4] or ""));
            if mw.ustring.lower(mw.text.trim(Args['AT'] or "")) == "ja" then AT = true; end
+
if Arg2 == "nbsp"  or Arg3 == "nbsp"  or Arg4 == "nbsp" then NBSP = true; end
            if mw.ustring.lower(mw.text.trim(Args['Sort'] or "")) ~= "" then SortIt = 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['AT'] or "")) == "ja" then AT = true; end
 +
if mw.ustring.lower(mw.text.trim(Args['Sort'] or "")) ~= "" then SortIt = true; end
 +
 +
IsOk, Tbl = Split(Args[1])
 +
if not  IsOk then
 +
Text = '<span class="error">[[Vorlage:FormatDate]]: Kein gültiges ISO-Datum!</span>'
 +
return Text
 +
end
 +
--Tag ohne Monat nicht erlaubt: Auch Tag auf Null setzen (= nur Jahr)
 +
if Tbl.m == 0 then
 +
Tbl.d = 0;
 +
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">[[Vorlage:FormatDate]]: Ungültiger Wert ("0-0-0") für das Datum! </span>'
 +
return Text
 +
end
 +
if Tbl.y  < 0 then -- Jahr Null reserviert fuer "Nur Tag und Monat"
 +
Tbl.y = 0 - Tbl.y
 +
VCHR = ' v. Chr.';
 +
end
  
            IsOk, Tbl = Split(Args[1])
+
IsOk = CheckDate(Tbl);
 +
if not IsOk then
 +
Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiges Datum!'.. table.concat(Tbl,'.')..'</span>'
 +
return Text
 +
end
  
            if Tbl.y == 'x' then
+
if Tbl.d > 0 then -- Tag angegeben, String erstellen
                Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiger Wert für Jahr!</span>'
+
TextDay = tostring(Tbl.d) .. '.&nbsp;'
                return Text
+
if Tbl.d < 10 and SortIt then
            end
+
TextDay = '&nbsp;' .. TextDay;
            if Tbl.m > 12 or Tbl.m < 0 then
+
end
                Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiger Wert für Monat!</span>'
+
else
                return Text
+
TextDay = '';
            end
+
end
            if Tbl.y <= 0 then
 
                Tbl.y = 1 - Tbl.y
 
                VCHR = " v. Chr.";
 
            end
 
            if Tbl.d > 0 then
 
                if CheckDate(Tbl) then
 
                    Text = tostring(Tbl.d) .. '.&nbsp;'
 
                else
 
                    Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiges Datum!'.. table.concat(Tbl,'.')..'</span>'
 
                    return Text
 
                end
 
            end
 
            if Tbl.m > 0 then
 
                if LINK then
 
                    if Tbl.d == 0 then
 
                        Linkziel =T_F[Tbl.m]
 
                    else
 
                        Linkziel = tostring(Tbl.d) .. ". " .. T_F[Tbl.m]
 
                    end
 
                end
 
                if AT and Tbl.m == 1 then
 
                    Tbl.m = 13
 
                end
 
                if STIL == 'M' then
 
                    Text = Text .. T_M[Tbl.m]
 
                elseif STIL == 'S' then
 
                    Text = Text .. T_S[Tbl.m]
 
                else
 
                    Text = Text .. T_F[Tbl.m]
 
                end
 
                if LINK then
 
                    Text = "[[" .. Linkziel .. "|" .. Text .. "]]"
 
                end
 
                if NBSP then
 
                    Text = Text .. "&nbsp;"
 
                else
 
                    Text = Text .. " "
 
                end
 
            end
 
            if LINK then
 
                Text = Text .. "[[" .. tostring(Tbl.y) .. VCHR .. "]]"
 
            else
 
                Text = Text .. tostring(Tbl.y) .. VCHR
 
            end
 
            if SortIt then
 
                if Tbl.m == 13 then Tbl.m = 1 end
 
                if tonumber(Tbl.y) and  tonumber(Tbl.m) and  tonumber(Tbl.d) then
 
                    if VCHR ~= "" then
 
                        Tbl.y = 1 - 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 .. Text
 
                end
 
            end
 
  
            return Text
+
if Tbl.m > 0 then -- Monat angegeben, String erstellen
        end
+
if AT and Tbl.m == 1 then
        --
+
idxm = 13
 +
else
 +
idxm =  Tbl.m;
 +
end
  
        function p.Execute(frame)
+
if STIL == 'S' then
            local FR = frame:getParent()
+
TextMonth = T_S[idxm] ;
            return  Run(FR.args)
+
elseif STIL == 'M' then
        end
+
TextMonth = T_M[idxm] ;
 +
else
 +
TextMonth = T_L[idxm] ;
 +
end
  
        function p.Sort(frame)
+
Text = TextDay .. TextMonth
            local FR = frame:getParent()
+
if LINK then
            FR.args.Sort='1'
+
if Tbl.d == 0 then
            return Run(FR.args)
+
Linkziel =T_L[Tbl.m]
        end
+
else
 +
Linkziel = tostring(Tbl.d) .. ". " .. T_L[Tbl.m]
 +
end
 +
Text = '[[' .. Linkziel .. '|' .. Text .. ']]';
 +
end
 +
end
 +
-- hier Tag und Monat zusammen, evtl. verlinkt
 +
 
 +
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 .. Text
 +
end
 +
return Text
 +
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
 
return p
 
return p

Version vom 11. September 2013, 13:56 Uhr

Die Dokumentation für dieses Modul kann unter Modul:Vorlage:FormatDate/Doku erstellt werden

local p = {}
	-- Trennen der Parameter
	local function Split(str)
		local Datum = {}
		local Teil=""
		local pos = 0
		Datum.y = 0
		Datum.m = 0
		Datum.d = 0
		pos = mw.ustring.find(str,'-',1,true);
		if pos == 1 then -- Minuszeichen am Anfang - nochmal suchen
			pos = mw.ustring.find(str,'-',2,true);
		end
		if not pos then -- Bereits am Stringende
			Datum.y = tonumber(str);
			if Datum.y then
				return true, Datum -- Nur eine Jahreszahl
			else
				return false, Datum  -- Kein Wert erkennbar
			end
		end
		Teil  = mw.ustring.sub(str,1,pos-1);
		Datum.y = tonumber(Teil) or 0;
		str = mw.ustring.sub(str,pos+1,   -1);
		pos = mw.ustring.find(str,'-',1,true);
		if not pos or pos == 0 then
			return true, Datum;  -- Nur Jahr
		end
		Teil  = mw.ustring.sub(str,1,pos-1)
		Datum.m = tonumber(Teil) or 0
		Teil  = mw.ustring.sub(str,pos+1,   -1)
		if #Teil == 0 then
			return true, Datum;  -- Nur Jahr und Monat
		end
		if Datum.m == 0 then
			Datum.d = 0;
		else
			Datum.d = tonumber(Teil) or 0;
		end
		return true, Datum;
	end
	--

	local function CheckDate(Date)
		-- Monatspruefung
		if Date.m ==  0 then -- keine Pruefung
			return true;
		end
		if Date.m > 12 or Date.m < 1 then
			return false;
		end
		if Date.d ==  0 then -- nur Monat angegeben, keine Tagespruefung
			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 -- Die greg. Sonderregeln werden ignoriert.
			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
		-- Hier nur noch 31-Tage-Monate übrig.
		if Date.d > 31  then return false; end
		return true;
	end
	--

	local function Run(Args)
		local T_L  = {"Januar","Februar", "März", "April", "Mai","Juni", "Juli", "August","September","Oktober","November","Dezember","Jänner"}
		local T_M  = {"Jan.","Feb.", "März", "Apr.", "Mai","Juni", "Juli", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."}
		local T_S  = {"Jan.","Feb.",  "Mrz", "Apr.", "Mai","Jun.", "Jul.", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."}
		local Text = "";
		local TextYear  = "";
		local TextMonth = "";
		local TextDay   = "";
			local idxm = 0 ;   --  Wegen der AT-Regel ein Extraindex fuer den Monat.
		local   AT = false;
		local NBSP = false;
		local LINK = false;
		local VCHR = "";
		local STIL = 'L';
		local IsOk = true
		local Tbl = {}
		local SortIt = 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 ""));
		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['AT'] or "")) == "ja" then AT = true; end
		if mw.ustring.lower(mw.text.trim(Args['Sort'] or "")) ~= "" then SortIt = true; end
		
		IsOk, Tbl = Split(Args[1])
		if not  IsOk then
			Text = '<span class="error">[[Vorlage:FormatDate]]: Kein gültiges ISO-Datum!</span>'
			return Text
		end
		--Tag ohne Monat nicht erlaubt: Auch Tag auf Null setzen (= nur Jahr)
		if Tbl.m == 0 then
			Tbl.d = 0;
		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">[[Vorlage:FormatDate]]: Ungültiger Wert ("0-0-0") für das Datum! </span>'
			return Text
		end
		if Tbl.y  < 0 then -- Jahr Null reserviert fuer "Nur Tag und Monat"
			Tbl.y = 0 - Tbl.y
			VCHR = ' v. Chr.';
		end

		IsOk = CheckDate(Tbl);
		if not IsOk then
			Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiges Datum!'.. table.concat(Tbl,'.')..'</span>'
		return Text
		end

		if Tbl.d > 0 then	-- Tag angegeben, String erstellen
			TextDay =  tostring(Tbl.d) .. '.&nbsp;'
			if Tbl.d < 10 and SortIt then
				TextDay = '&nbsp;' .. TextDay;
			end
		else
			TextDay = '';
		end

		if Tbl.m > 0 then	-- Monat angegeben, String erstellen
			if AT and Tbl.m == 1 then
				idxm = 13
			else
				idxm =  Tbl.m;
			end

			if STIL == 'S' then
				TextMonth = T_S[idxm] ;
			elseif STIL == 'M' then
				TextMonth = T_M[idxm] ;
			else
				TextMonth = T_L[idxm] ;
			end

			Text = TextDay .. TextMonth
			if LINK then
				if Tbl.d == 0 then
					Linkziel =T_L[Tbl.m]
				else
					Linkziel = tostring(Tbl.d) .. ". " .. T_L[Tbl.m]
				end
				Text = '[[' .. Linkziel .. '|' .. Text .. ']]';
			end
		end
		-- hier Tag und Monat zusammen, evtl. verlinkt

		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 .. Text
		end
		return Text
	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
return p