Zeile 1: |
Zeile 1: |
− | local p = {} | + | local p = {} |
− | -- 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.
| + | 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 -- Bereits am Stringende |
− | if not pos then
| + | Datum.y = tonumber(str); |
− | return T
| + | if Datum.y then |
− | end
| + | if math.floor(Datum.y) ~= Datum.y then |
− | Teil = mw.ustring.sub(str,1,pos-1)
| + | return false, Datum -- Eine Dezimalzahl ist Unsinn |
− | T.y = tonumber(Teil) or 'x'
| + | end |
− | str = mw.ustring.sub(str,pos+1, -1)
| + | return true, Datum -- Nur eine Jahreszahl |
− | pos = mw.ustring.find(str,'-',1,true);
| + | else |
− | if not pos or pos == 0 then
| + | return false, Datum -- Kein Wert erkennbar |
− | return T
| + | end |
− | 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.y = tonumber(Teil) or 0; |
− | Teil = mw.ustring.sub(str,pos+1, -1)
| + | |
− | if #Teil == 0 then
| + | str = mw.ustring.sub(str,pos+1, -1); |
− | return T
| + | pos = mw.ustring.find(str,'-',1,true); |
− | end
| |
− | if T.m == 0 then
| |
− | T.d = 0
| |
− | else
| |
− | T.d = tonumber(Teil) or 0
| |
− | end
| |
− | return T
| |
− | end
| |
− | --
| |
| | | |
− | local function CheckDate(Tfl)
| + | if not pos or pos == 0 then |
− | 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
| + | Datum.m = tonumber(str) or 0 |
− | return false;
| + | Datum.d = 0; |
− | end
| + | if math.floor(Datum.m) ~= Datum.m then |
− | if Tfl.m == 2 then -- Die greg. Sonderregeln werden ignoriert.
| + | return false, Datum -- Eine Dezimalzahl ist Unsinn |
− | if Tfl.y % 4 ~= 0 and Tfl.d > 28 then return false; end
| + | end |
− | if Tfl.y % 4 == 0 and Tfl.d > 29 then return false; end
| + | return true, Datum; |
− | end
| + | end |
− | -- Hier nur noch 30-Tage-Monate übrig.
| |
− | if Tfl.d > 30 then return false; end
| |
− | return true;
| |
− | end
| |
− | --
| |
| | | |
− | function p.Run(frame)
| + | Teil = mw.ustring.sub(str,1,pos-1) |
− | local T_F = {"Januar","Februar", "März", "April", "Mai","Juni", "Juli", "August","September","Oktober","November","Dezember","Jänner"}
| + | Datum.m = tonumber(Teil) or 0 |
− | local T_M = {"Jan.","Feb.", "Mrz", "Apr.", "Mai","Jun.", "Jul.", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."}
| + | Teil = mw.ustring.sub(str,pos+1, -1) |
− | local T_S = {"Jan.","Feb.", "März", "Apr.", "Mai","Juni", "Juli", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."}
| + | Datum.d = tonumber(Teil) or 0; |
− | local Text = "";
| + | if math.floor(Datum.m) ~= Datum.m then |
− | local AT = false;
| + | return false, Datum -- Eine Dezimalzahl ist Unsinn |
− | local NBSP = false;
| + | end |
− | local LINK = false;
| + | if math.floor(Datum.d) ~= Datum.d then |
− | local VCHR = "";
| + | return false, Datum -- Eine Dezimalzahl ist Unsinn |
− | local STIL = 'F';
| + | end |
− | local Tbl = {}
| + | return true, Datum; |
− |
| + | end |
− | 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])
| + | local function CheckDate(Date) |
− | if Tbl.y == 'x' then
| + | -- Monatspruefung |
− | Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiger Wert für Jahr!</span>'
| + | if Date.m == 0 then -- keine Pruefung |
− | return Text
| + | return true; |
− | end
| + | end |
− | if Tbl.m > 12 or Tbl.m < 0 then
| + | if Date.m > 12 or Date.m < 1 then |
− | Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiger Wert für Monat!</span>'
| + | return false; |
− | return Text
| + | end |
− | 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 TageInMonate(Datum) |
| + | Datum.m = 1; |
| + | if Datum.d > 31 then -- nach Januar |
| + | Datum.m = 2; |
| + | Datum.d = Datum.d - 31; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | if Datum.y % 4 == 0 then -- Die greg. Sonderregeln werden ignoriert. |
| + | if Datum.d > 29 then -- nach Februar (Schaltjahr) |
| + | Datum.m = 3; |
| + | Datum.d = Datum.d - 29; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | else |
| + | if Datum.d > 28 then -- nach Februar (Normaljahr) |
| + | Datum.m = 3; |
| + | Datum.d = Datum.d - 28; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | end |
| + | if Datum.d > 31 then -- nach Maerz |
| + | Datum.m = 4; |
| + | Datum.d = Datum.d - 31; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | if Datum.d > 30 then -- nach April |
| + | Datum.m = 5; |
| + | Datum.d = Datum.d - 30; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | if Datum.d > 31 then -- nach Mai |
| + | Datum.m = 6; |
| + | Datum.d = Datum.d - 31; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | if Datum.d > 30 then -- nach Juni |
| + | Datum.m = 7; |
| + | Datum.d = Datum.d - 30; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | if Datum.d > 31 then -- nach Juli |
| + | Datum.m = 8; |
| + | Datum.d = Datum.d - 31; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | if Datum.d > 31 then -- nach August |
| + | Datum.m = 9; |
| + | Datum.d = Datum.d - 31; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | if Datum.d > 30 then -- nach September |
| + | Datum.m = 10; |
| + | Datum.d = Datum.d - 30; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | if Datum.d > 31 then -- nach Oktober |
| + | Datum.m = 11; |
| + | Datum.d = Datum.d - 31; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | if Datum.d > 30 then -- nach November |
| + | Datum.m = 12; |
| + | Datum.d = Datum.d - 30; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | if Datum.d > 31 then -- nach Dezember = Fehler |
| + | Datum.m = 0; |
| + | Datum.d = 0; |
| + | return false, Datum; |
| + | else |
| + | return true, Datum; |
| + | end |
| + | end |
| + | -- |
| | | |
− | if Tbl.y <= 0 then
| + | local function Run(Args) |
− | Tbl.y = 1 - Tbl.y
| + | local T_L = {"Januar","Februar", "März", "April", "Mai","Juni", "Juli", "August","September","Oktober","November","Dezember","Jänner"} |
− | VCHR = " v. Chr.";
| + | local T_M = {"Jan.","Feb.", "März", "Apr.", "Mai","Juni", "Juli", "Aug.","Sep.","Okt.","Nov.","Dez.","Jän."} |
− | end
| + | 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 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 ""); |
| + | local display = mw.text.trim(Args['display'] or ""); -- Nur zusammen mit sort sinnvoll |
| + | 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['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 .. " "; 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 |
| + | --Tage ohne Monat: Tage in Monat und Tag umrechnen |
| + | if Tbl.m == 0 and Tbl.d ~= 0 then |
| + | IsOk, Tbl = TageInMonate(Tbl) |
| + | end |
| + | if not IsOk then |
| + | Text = '<span class="error">[[Vorlage:FormatDate]]: Kein gültiges ISO-Datum!</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">[[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 |
| | | |
− | if Tbl.d > 0 then
| + | IsOk = CheckDate(Tbl); |
− | if CheckDate(Tbl) then
| + | if not IsOk then |
− | Text = tostring(Tbl.d) .. '. '
| + | Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiges Datum!'.. table.concat(Tbl,'.')..'</span>' |
− | else
| + | return Text |
− | Text = '<span class="error">[[Vorlage:FormatDate]]: Ungültiges Datum!</span>'
| + | end |
− | return Text
| |
− | end
| |
− | end
| |
| | | |
− | if Tbl.m > 0 then
| + | if Tbl.d > 0 then -- Tag angegeben, String erstellen |
| + | TextDay = tostring(Tbl.d) .. '. ' |
| + | if Tbl.d < 10 and Zero then |
| + | TextDay = '<span style="visibility:hidden;">0</span>' .. TextDay; |
| + | end |
| + | else |
| + | TextDay = ''; |
| + | end |
| | | |
− | if LINK then
| + | if Tbl.m > 0 then -- Monat angegeben, String erstellen |
− | if Tbl.d == 0 then
| + | if AT and Tbl.m == 1 then |
− | Linkziel =T_F[Tbl.m]
| + | idxm = 13 |
− | else
| + | else |
− | Linkziel = tostring(Tbl.d) .. ". " .. T_F[Tbl.m]
| + | idxm = Tbl.m; |
− | end
| + | end |
− | end
| |
| | | |
− | if AT and Tbl.m == 1 then
| + | if STIL == 'S' then |
− | Tbl.m = 13
| + | TextMonth = T_S[idxm] ; |
− | end
| + | elseif STIL == 'M' then |
− | if STIL == 'M' then
| + | TextMonth = T_M[idxm] ; |
− | Text = Text .. T_M[Tbl.m]
| + | else |
− | elseif STIL == 'S' then
| + | TextMonth = T_L[idxm] ; |
− | Text = Text .. T_S[Tbl.m]
| + | end |
− | else
| + | |
− | Text = Text .. T_F[Tbl.m]
| + | Text = TextDay .. TextMonth |
− | end
| + | if LINK then |
− | if LINK then
| + | if Tbl.d == 0 then |
− | Text = "[[" .. Linkziel .. "|" .. Text .. "]]"
| + | Linkziel =T_L[Tbl.m] |
− | end
| + | else |
− | if NBSP then
| + | Linkziel = tostring(Tbl.d) .. ". " .. T_L[Tbl.m] |
− | Text = Text .. " "
| + | end |
− | else
| + | Text = '[[' .. Linkziel .. '|' .. Text .. ']]'; |
− | Text = Text .. " "
| + | end |
− | end
| + | 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 = ' ' .. TextYear; |
| + | else |
| + | TextYear = ' ' .. TextYear; |
| + | end |
| + | end |
| + | end |
| + | Text = Text .. TextYear; |
| + | |
| + | if SortIt then |
| + | if VCHR ~= "" then |
| + | Tbl.y = 0 - Tbl.y |
| + | end |
| + | -- Begrenzung auf 3000 v Chr. bis 6999 n. Chr. Vierstellige Sortierung reicht aus |
| + | if Tbl.y > 6999 then |
| + | Tbl.y = 6999; |
| + | end |
| + | -- Nur bei Sortierung sinnvoll: Überschreiben der Textausgabe mit angegebenen String |
| + | if display ~= "" then |
| + | Text = display; |
| + | end |
| + | Numstr = string.format('%d%2.2d%2.2d',3000+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() |
| + | if frame:callParserFunction('int', 'lang' ) == 'de-at' then |
| + | FR.args['AT']='ja'; |
| + | end |
| + | 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 |
| | | |
− | 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 | | return p |