Modul:Vorlage:FormatDate: Unterschied zwischen den Versionen

Aus FreeWiki
Wechseln zu: Navigation, Suche
te>Antonsusi
K
te>Antonsusi
K
Zeile 2: Zeile 2:
 
         -- Trennen der Parameter
 
         -- Trennen der Parameter
 
         local function Split(str)
 
         local function Split(str)
             local T = {}
+
             local Datum = {}
 
             local Teil=""
 
             local Teil=""
 
             local pos = 0
 
             local pos = 0
             T.y = 0
+
             Datum.y = 0
             T.m = 0
+
             Datum.m = 0
             T.d = 0
+
             Datum.d = 0
 
             -- str = mw.ustring.gsub(str,'/','-');  -- Evtl. Erweiterung auf Slashstring.
 
             -- str = mw.ustring.gsub(str,'/','-');  -- Evtl. Erweiterung auf Slashstring.
 
             pos = mw.ustring.find(str,'-',1,true);
 
             pos = mw.ustring.find(str,'-',1,true);
Zeile 14: Zeile 14:
 
             end
 
             end
 
             if not pos then
 
             if not pos then
                 return T
+
                 return Datum
 
             end
 
             end
 
             Teil  = mw.ustring.sub(str,1,pos-1)
 
             Teil  = mw.ustring.sub(str,1,pos-1)
             T.y = tonumber(Teil) or 'x'
+
             Datum.y = tonumber(Teil) or 'x'
 
             str = mw.ustring.sub(str,pos+1,  -1)
 
             str = mw.ustring.sub(str,pos+1,  -1)
 
             pos = mw.ustring.find(str,'-',1,true);
 
             pos = mw.ustring.find(str,'-',1,true);
 
             if not pos or pos == 0 then
 
             if not pos or pos == 0 then
                 return T
+
                 return Datum
 
             end
 
             end
 
             Teil  = mw.ustring.sub(str,1,pos-1)
 
             Teil  = mw.ustring.sub(str,1,pos-1)
             T.m = tonumber(Teil) or 0
+
             Datum.m = tonumber(Teil) or 0
 
             Teil  = mw.ustring.sub(str,pos+1,  -1)
 
             Teil  = mw.ustring.sub(str,pos+1,  -1)
 
             if #Teil == 0 then
 
             if #Teil == 0 then
                 return T
+
                 return Datum
 
             end
 
             end
             if T.m == 0 then
+
             if Datum.m == 0 then
                 T.d = 0
+
                 Datum.d = 0
 
             else
 
             else
                 T.d = tonumber(Teil) or 0
+
                 Datum.d = tonumber(Teil) or 0
 
             end
 
             end
             return T
+
             return Datum
 
         end
 
         end
 
         --
 
         --
  
         local function CheckDate(Tfl)
+
         local function CheckDate(Datum)
             if ( Tfl.m == 1 or Tfl.m == 3  or Tfl.m == 5  or Tfl.m == 7  or Tfl.m == 8  or Tfl.m == 10  or Tfl.m == 12 ) and Tfl.d > 31 then
+
             if ( Datum.m == 1 or Datum.m == 3  or Datum.m == 5  or Datum.m == 7  or Datum.m == 8  or Datum.m == 10  or Datum.m == 12 ) and Datum.d > 31 then
 
                 return false;
 
                 return false;
 
             end
 
             end
             if  Tfl.m == 2 then -- Die greg. Sonderregeln werden ignoriert.
+
             if  Datum.m == 2 then -- Die greg. Sonderregeln werden ignoriert.
                 if Tfl.y % 4 ~= 0 and Tfl.d > 28 then return false; end
+
                 if Datum.y % 4 ~= 0 and Datum.d > 28 then return false; end
                 if Tfl.y % 4 == 0 and Tfl.d > 29 then return false; end
+
                 if Datum.y % 4 == 0 and Datum.d > 29 then return false; end
 
             end
 
             end
 
             -- Hier nur noch 30-Tage-Monate übrig.
 
             -- Hier nur noch 30-Tage-Monate übrig.
             if Tfl.d > 30  then return false; end
+
             if Datum.d > 30  then return false; end
 
             return true;
 
             return true;
 
         end
 
         end

Version vom 2. Juni 2013, 02:29 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
            -- str = mw.ustring.gsub(str,'/','-');  -- Evtl. Erweiterung auf Slashstring.
            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
                return Datum
            end
            Teil  = mw.ustring.sub(str,1,pos-1)
            Datum.y = tonumber(Teil) or 'x'
            str = mw.ustring.sub(str,pos+1,   -1)
            pos = mw.ustring.find(str,'-',1,true);
            if not pos or pos == 0 then
                return Datum
            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 Datum
            end
            if Datum.m == 0 then
                Datum.d = 0
            else
                Datum.d = tonumber(Teil) or 0
            end
            return Datum
        end
        --

        local function CheckDate(Datum)
            if ( Datum.m == 1 or Datum.m == 3  or Datum.m == 5  or Datum.m == 7  or Datum.m == 8  or Datum.m == 10  or Datum.m == 12 ) and Datum.d > 31 then
                return false;
            end
            if  Datum.m == 2 then -- Die greg. Sonderregeln werden ignoriert.
                if Datum.y % 4 ~= 0 and Datum.d > 28 then return false; end
                if Datum.y % 4 == 0 and Datum.d > 29 then return false; end
            end
            -- Hier nur noch 30-Tage-Monate übrig.
            if Datum.d > 30  then return false; end
            return true;
        end
        --

        function p.Run(frame)
            local T_F  = {"Januar","Februar", "März", "April", "Mai","Juni", "Juli", "August","September","Oktober","November","Dezember","Jänner"}
            local T_M  = {"Jan.","Feb.",  "Mrz", "Apr.", "Mai","Jun.", "Jul.", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."}
            local T_S  = {"Jan.","Feb.", "März", "Apr.", "Mai","Juni", "Juli", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."}
            local Text = "";
            local   AT = false;
            local NBSP = false;
            local LINK = false;
            local VCHR = "";
            local STIL = 'F';
            local Tbl = {}
            
            if mw.ustring.lower(frame.args[2] or "") == "nbsp"  or mw.ustring.lower(frame.args[3] or "") == "nbsp"  or mw.ustring.lower(frame.args[4] or "") == "nbsp" then NBSP = true; end
            if mw.ustring.lower(frame.args[2] or "") == "link"  or mw.ustring.lower(frame.args[3] or "") == "link"  or mw.ustring.lower(frame.args[4] or "") == "link" then LINK = true; end
            if mw.ustring.upper(frame.args[2] or "") == "M"     or mw.ustring.upper(frame.args[3] or "") == "M"     or mw.ustring.upper(frame.args[4] or "") == "M"    then STIL = 'M'; end
            if mw.ustring.upper(frame.args[2] or "") == "S"     or mw.ustring.upper(frame.args[3] or "") == "S"     or mw.ustring.upper(frame.args[4] or "") == "S"    then STIL = 'S'; end
            if mw.ustring.lower(frame.args['AT'] or "") == "ja" then AT = true; end

            Tbl = Split(frame.args[1])
            if Tbl.y == 'x' then
                Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiger Wert für Jahr!</span>'
                return Text
            end
            if Tbl.m > 12 or Tbl.m < 0 then
                Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiger Wert für Monat!</span>'
                return Text
            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!</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
            return Text
        end
        --
    function p.Execute(frame)
        return p.Run(frame:getParent())
    end

return p