Modul:Vorlage:FormatDate: Unterschied zwischen den Versionen

K
Dezimalzahlen abfangen
K (1 Version)
 
de>Antonsusi
K (Dezimalzahlen abfangen)
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 = {}
Zeile 15: Zeile 15:
Datum.y = tonumber(str);
Datum.y = tonumber(str);
if Datum.y then
if Datum.y then
if math.floor(Datum.y) ~= Datum.y then
return false, Datum -- Eine Dezimalzahl ist Unsinn
end
return true, Datum -- Nur eine Jahreszahl
return true, Datum -- Nur eine Jahreszahl
else
else
Zeile 22: Zeile 25:
Teil  = mw.ustring.sub(str,1,pos-1);
Teil  = mw.ustring.sub(str,1,pos-1);
Datum.y = tonumber(Teil) or 0;
Datum.y = tonumber(Teil) or 0;
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 true, Datum; -- Nur Jahr
Datum.m = tonumber(str) or 0
Datum.d = 0;
if math.floor(Datum.m) ~= Datum.m then
return false, Datum -- Eine Dezimalzahl ist Unsinn
end
return true, Datum;
end
end
Teil  = mw.ustring.sub(str,1,pos-1)
Teil  = mw.ustring.sub(str,1,pos-1)
Datum.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
Datum.d = tonumber(Teil) or 0;
return true, Datum-- Nur Jahr und Monat
if math.floor(Datum.m) ~= Datum.m then
return false, Datum -- Eine Dezimalzahl ist Unsinn
end
end
if Datum.m == 0 then
if math.floor(Datum.d) ~= Datum.d then
Datum.d = 0;
return false, Datum -- Eine Dezimalzahl ist Unsinn
else
Datum.d = tonumber(Teil) or 0;
end
end
return true, Datum;
return true, Datum;
Zeile 63: Zeile 73:
if Date.d > 31  then return false; end
if Date.d > 31  then return false; end
return true;
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
end
--
--
Zeile 80: Zeile 176:
local VCHR = "";
local VCHR = "";
local STIL = 'L';
local STIL = 'L';
local IsOk = true
local IsOk = true;
local Tbl = {}
local Tbl = {}
local SortIt = false;
local SortIt = false;
Zeile 90: Zeile 186:
local Arg4 = mw.ustring.lower(mw.text.trim(Args[4] or ""));
local Arg4 = mw.ustring.lower(mw.text.trim(Args[4] or ""));
local davor = mw.text.trim(Args['davor'] 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 == "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 == "link"  or Arg3 == "link"  or Arg4 == "link" then LINK = true; end
Zeile 104: Zeile 201:
return Text
return Text
end
end
--Tag ohne Monat nicht erlaubt: Auch Tag auf Null setzen (= nur Jahr)
--Tage ohne Monat: Tage in Monat und Tag umrechnen
if Tbl.m == 0 then
if Tbl.m == 0 and Tbl.d ~= 0  then
Tbl.d = 0;
IsOk, Tbl = TageInMonate(Tbl)
end
if not  IsOk then
Text = '<span class="error">[[Vorlage:FormatDate]]: Kein gültiges ISO-Datum!</span>'
return Text
end
end
Tbl.y = tonumber(Tbl.y) or 0;
Tbl.y = tonumber(Tbl.y) or 0;
Zeile 180: Zeile 281:
Tbl.y = 0 - Tbl.y
Tbl.y = 0 - Tbl.y
end
end
Numstr = string.format('%d%2.2d%2.2d',5000+Tbl.y,Tbl.m,Tbl.d);
-- 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>';
SortTag='<span style="display:none" class="sortkey">' .. Numstr .. '</span>';
Text = SortTag .. davor .. Text;
Text = SortTag .. davor .. Text;
Zeile 187: Zeile 296:
end
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)
function p.Execute(frame)
local FR = frame:getParent()
local FR = frame:getParent()
if frame:callParserFunction('int', 'lang' ) == 'de-at' then
FR.args['AT']='ja';
end
return Run(FR.args)
return Run(FR.args)
end
end
Zeile 198: Zeile 358:
return Run(FR.args)
return Run(FR.args)
end
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
return p
Anonymer Benutzer