https://de.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3AISOdate Modul:ISOdate - Versionsgeschichte 2025-04-07T00:14:05Z Versionsgeschichte dieser Seite in Wikipedia MediaWiki 1.44.0-wmf.23 https://de.wikipedia.org/w/index.php?title=Modul:ISOdate&diff=195600578&oldid=prev Ederporto: Modul: WikidataIB hat dieses Modul als Abhängigkeit, daher importiere ich das englische Äquivalent, damit es richtig funktioniert. 2020-01-07T16:55:49Z <p>Modul: WikidataIB hat dieses Modul als Abhängigkeit, daher importiere ich das englische Äquivalent, damit es richtig funktioniert.</p> <p><b>Neue Seite</b></p><div>--[[ <br /> <br /> This module is intended for processing of date strings.<br /> <br /> Please do not modify this code without applying the changes first at Module:ISOdate/sandbox and testing <br /> at Module:ISOdate/sandbox/testcases and Module talk:ISOdate/sandbox/testcases.<br /> <br /> Authors and maintainers:<br /> * User:Parent5446 - original version of the function mimicking template:ISOdate<br /> * User:Jarekt - original version of the functions mimicking template:Date and template:ISOyear<br /> <br /> ]]<br /> <br /> <br /> local p = {}<br /> <br /> -- =======================================<br /> -- === Dependencies ======================<br /> -- =======================================<br /> local D = require(&#039;Module:DateI18n&#039;) -- the enwp version of c:Module:Date<br /> <br /> --[[<br /> ISOyear<br /> <br /> This function returns year part of date string.<br /> <br /> Usage:<br /> {{#invoke:ISOdate|ISOyear|target_string}}<br /> <br /> Parameters<br /> 1: The date string <br /> <br /> Error Handling:<br /> If the string does not look like it contain the year than the function will not return anything.<br /> That is the preferred treatment for the template:Creator which is the main (only?) template calling it.<br /> ]]<br /> function p.ISOyear( frame )<br /> return p._ISOyear( frame.args[1] )<br /> end<br /> <br /> function p._ISOyear( input )<br /> if not input then<br /> return &#039;&#039;<br /> end<br /> input = mw.text.trim( input )<br /> <br /> -- if empty string then return it<br /> if input == &quot;&quot; then<br /> return input<br /> end<br /> <br /> -- if number then return it<br /> if tonumber( input ) then<br /> return mw.ustring.format( &#039;%04i&#039;, input )<br /> end<br /> <br /> -- otherwise use regular expression match<br /> input = mw.ustring.match( input, &#039;^+?(-?%d%d?%d?%d?)-&#039; )<br /> if input and tonumber( input ) then<br /> return mw.ustring.format( &#039;%04i&#039;, input )<br /> else<br /> return &#039;&#039;<br /> end<br /> end<br /> <br /> --[[<br /> ISOdate<br /> <br /> This function is the core part of the ISOdate template. <br /> <br /> Usage:<br /> {{#invoke:ISOdate|ISOdate|target_string|lang=}}<br /> <br /> Parameters:<br /> 1: The date string <br /> lang: The language to display it in<br /> form: Language format (genitive, etc.) for some languages<br /> class: CSS class for the &lt;time&gt; node<br /> <br /> Error Handling:<br /> If the string does not look like it contain the proper ISO date than the function will return the original string.<br /> <br /> That is the preferred treatment for the template:Information (and similar templates) which calling it.<br /> ]]<br /> function p.ISOdate(frame)<br /> local datestr, succeded<br /> local args = frame.args<br /> if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then <br /> args.lang = frame:callParserFunction( &quot;int&quot;, &quot;lang&quot; ) -- get user&#039;s chosen language <br /> end<br /> datestr, succeded = p._ISOdate(<br /> mw.text.trim(args[1]),<br /> args.lang, -- language<br /> args.case or &#039;&#039;, -- allows to specify grammatical case for the month for languages that use them<br /> args.class or &#039;dtstart&#039;, -- allows to set the html class of the time node where the date is included. <br /> args.trim_year or &#039;100-999&#039; -- by default pad one and 2 digit years to be 4 digit long, while keeping 3 digit years as is <br /> )<br /> return datestr<br /> end<br /> <br /> function p._ISOdate(datestr, lang, case, class, trim_year)<br /> <br /> -- pattern: regexp - regular expresion to test; dlen - number of date elements; tail = which element is a &quot;tail&quot; if any<br /> -- regexp hints:<br /> -- 1) Strings starting with &quot;^&quot; and ending with &quot;$&quot; indicate whole string match<br /> -- 2) optional tail part copied as-is and following the main parsed part of the date have to be separated from the date by a whitespace, so &quot;(\s.+)?&quot;<br /> local patterns = {<br /> -- strings starting with YYYY-MM-DD HH:MM:SS. Year 4 digits (if we know seconds than it was within the last 100 years), the rest 1-2<br /> -- date and time can be separated by space or &quot;T&quot; and there could be a &quot;Z&quot; on the end indicating &quot;Zulu&quot; time zone<br /> {dlen=6, tail=7, regexp=&quot;^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?):(%d%d?)Z?(%s.*)&quot;}, <br /> {dlen=6, tail=0, regexp=&quot;^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?):(%d%d?)Z?$&quot;}, <br /> -- strings starting with YYYY-MM-DD HH:MM. Year 4 digits, the rest 1-2<br /> -- (if one knows hour and minute than it was probably after a year 1000)<br /> {dlen=5, tail=6, regexp=&quot;^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?)(%s.+)&quot;},<br /> {dlen=5, tail=0, regexp=&quot;^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?)$&quot;},<br /> -- strings starting with YYYY-MM-DD. Year 1-4 digits, the rest 1-2<br /> {dlen=3, tail=4, regexp=&quot;^+?(%d%d?%d?%d?)-(%d%d?)-(%d%d?)(%s.+)&quot;},<br /> {dlen=3, tail=0, regexp=&quot;^+?(%d%d?%d?%d?)-(%d%d?)-(%d%d?)$&quot;},<br /> -- strings starting with YYYY-MM. Year 3-4 digits, month 2 digits<br /> -- (want to avoit converting to dates strings like 10-5 = 5<br /> {dlen=2, tail=3, regexp=&quot;^+?(%d%d%d%d?)-(%d%d)(%s.+)&quot;}, <br /> -- if whole string is in YYYY-MM form: If Year 1-4 digits, month 1-2 digits<br /> {dlen=2, tail=0, regexp=&quot;^+?(%d%d?%d?%d?)-(%d%d?)$&quot;}, <br /> -- string starts with a number -&gt; it has to be 3 or 4 digit long to be a year<br /> {dlen=1, tail=2, regexp=&quot;^+?(%d%d%d%d?)(%s.+)&quot;}, <br /> -- if whole string is a number (1-4 digit long) than it will be interpreted as a year<br /> {dlen=1, tail=0, regexp=&quot;^+?(%d%d?%d?%d?)$&quot;},<br /> }<br /> <br /> -- create datevec based on which variables are provided<br /> local datevec, tail, formatNum<br /> datevec, tail, formatNum = p.test_date_formats(datestr or &#039;&#039;, patterns)<br /> if datevec[1]==&#039;&#039; or datevec[1]==nil then<br /> -- quickly return if datestr does not look like date (it could be a template)<br /> return datestr, false<br /> end<br /> <br /> -- call p._Date function to format date string<br /> local succeded, datestr2<br /> succeded, datestr2 = pcall( D._Date, datevec, lang, case, class, trim_year)<br /> if succeded and datestr2~=&#039;&#039; then<br /> return mw.text.trim( datestr2 .. tail), true<br /> else -- in case of errors return the original string<br /> return datestr, false<br /> end <br /> end<br /> <br /> function p.ISOdate_extended(frame)<br /> -- pattern: regexp - regular expresion to test; dlen - number of date elements; tail = which element is a &quot;tail&quot; if any<br /> -- regexp hints:<br /> -- 1) Strings starting with &quot;^&quot; and ending with &quot;$&quot; indicate whole string match<br /> -- 2) optional tail part copied as-is and following the main parsed part of the date have to be separated from the date by a whitespace, so &quot;(\s.+)?&quot;<br /> <br /> local datestr, succeded<br /> local args = frame.args<br /> if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then <br /> args.lang = frame:callParserFunction( &quot;int&quot;, &quot;lang&quot; ) -- get user&#039;s chosen language <br /> end<br /> datestr, succeded = p._ISOdate(<br /> mw.text.trim(args[1]),<br /> args.lang, -- language<br /> args.case or &#039;&#039;, -- allows to specify grammatical case for the month for languages that use them<br /> args.class or &#039;dtstart&#039;, -- allows to set the html class of the time node where the date is included. <br /> args.trim_year or &#039;100-999&#039; -- by default pad one and 2 digit years to be 4 digit long, while keeping 3 digit years as is <br /> )<br /> if succeded then<br /> return datestr<br /> end<br /> <br /> local patterns = {<br /> -- Exended set of recognized formats: like MM/DD/YYYY<br /> {dlen=3, tail=4, regexp=&quot;^(%d%d?)[-./](%d%d?)[-./](%d%d%d%d)(%s.+)&quot;},<br /> {dlen=3, tail=0, regexp=&quot;^(%d%d?)[-./](%d%d?)[-./](%d%d%d%d)$&quot;},<br /> {dlen=3, tail=0, regexp=&quot;^(%d%d?)%s(%w+)%s(%d%d%d%d)$&quot;},<br /> {dlen=3, tail=0, regexp=&quot;^(%w+)%s(%d%d?),%s(%d%d%d%d)$&quot;},<br /> }<br /> <br /> local datevec, tail, formatNum, category = &#039;&#039;<br /> datevec, tail, formatNum = p.test_date_formats(frame.args[1], patterns)<br /> if formatNum==1 or formatNum==2 then<br /> vec = datevec;<br /> if tonumber(datevec[1])&gt;12 then<br /> frame.args[1] = string.format(&#039;%04i-%02i-%02i&#039;, datevec[3], datevec[2], datevec[1] )<br /> category = &#039;[[Category:Date in DD/MM/YYYY format]]&#039;<br /> return mw.text.trim( p.ISOdate(frame) .. tail);<br /> elseif tonumber(datevec[2])&gt;12 then<br /> frame.args[1] = string.format(&#039;%04i-%02i-%02i&#039;, datevec[3], datevec[1], datevec[2] )<br /> category = &#039;[[Category:Date in MM/DD/YYYY format]]&#039;<br /> return mw.text.trim( p.ISOdate(frame) .. tail);<br /> end<br /> elseif (formatNum==3 or formatNum==4) and (datevec[3]==&#039;&#039; or datevec[3]~=nil) then<br /> local str = mw.getCurrentFrame():callParserFunction( &quot;#time&quot;, { &#039;Y-m-d&#039;, datestr} )<br /> local vec = {str:match( &quot;^(%d%d?%d?%d?)-(%d%d?)-(%d%d?)$&quot; )}<br /> if vec and vec[1]~=nil then<br /> frame.args[1] = string.format(&#039;%04i-%02i-%02i&#039;, vec[1], vec[2], vec[3] )<br /> category = &#039;[[Category:Date in word format]]&#039;<br /> return p.ISOdate(frame);<br /> end<br /> end <br /> return datestr<br /> end<br /> <br /> function p.test_date_formats(datestr, patterns)<br /> -- pattern: regexp - regular expresion to test; dlen - number of date elements; tail = which element is a &quot;tail&quot; if any<br /> <br /> local datevec = {&#039;&#039;,&#039;&#039;,&#039;&#039;,&#039;&#039;,&#039;&#039;,&#039;&#039;}<br /> local tail = &#039;&#039;<br /> local vec, pat<br /> local formatNum = 0<br /> for i, pat in ipairs( patterns ) do<br /> vec = {datestr:match( pat.regexp )}<br /> if vec and vec[1]~=nil then<br /> for j=1,pat.dlen do<br /> datevec[j] = vec[j]<br /> end<br /> if pat.tail&gt;0 and vec[pat.tail]~=nil then<br /> tail = mw.ustring.gsub(&#039; &#039; .. vec[pat.tail], &#039; +&#039;, &#039; &#039;)<br /> end<br /> formatNum = i<br /> break<br /> end<br /> end<br /> return datevec, tail, formatNum<br /> end<br /> <br /> return p</div> Ederporto