https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3ADatesWD%2Fsandbox Module:DatesWD/sandbox - Revision history 2025-06-04T04:26:20Z Revision history for this page on the wiki MediaWiki 1.45.0-wmf.3 https://en.wikipedia.org/w/index.php?title=Module:DatesWD/sandbox&diff=1149482175&oldid=prev Lemondoge: Slight simplifications 2023-04-12T14:34:34Z <p>Slight simplifications</p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 14:34, 12 April 2023</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 7:</td> <td colspan="2" class="diff-lineno">Line 7:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if str == "" or str == nil then</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if str == "" or str == nil then</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> return nil</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> return nil</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> elseif <del style="font-weight: bold; text-decoration: none;">type(</del>tonumber(str)<del style="font-weight: bold; text-decoration: none;">) == "number"</del> then</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> elseif tonumber(str) then</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> return math.floor(tonumber(str))</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> return math.floor(tonumber(str))</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> </tr> <tr> <td colspan="2" class="diff-lineno">Line 16:</td> <td colspan="2" class="diff-lineno">Line 16:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>local function inbord(val, down, up)</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>local function inbord(val, down, up)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> <del style="font-weight: bold; text-decoration: none;">if</del> type(up) ~= "number" or type(down) ~= "number" or type(val) ~= "number" or up &lt; down or val &lt; down or val &gt; up<del style="font-weight: bold; text-decoration: none;"> then</del></div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> <ins style="font-weight: bold; text-decoration: none;">return</ins> <ins style="font-weight: bold; text-decoration: none;">not (</ins>type(up) ~= "number" or type(down) ~= "number" or type(val) ~= "number" or up &lt; down or val &lt; down or val &gt; up<ins style="font-weight: bold; text-decoration: none;">)</ins></div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> return false</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> return true</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> end</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>end</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>end</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>local function isdate ( chain )</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>local function isdate ( chain )</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> <del style="font-weight: bold; text-decoration: none;">if</del> (<del style="font-weight: bold; text-decoration: none;">not </del>type(chain<del style="font-weight: bold; text-decoration: none;">)</del> == "table")</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> <ins style="font-weight: bold; text-decoration: none;">return</ins> (type(chain == "table")</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"> or (not</del> inbord(chain.year,-9999,9999<del style="font-weight: bold; text-decoration: none;">)</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> and</ins> inbord(chain.year,-9999,9999)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"> or (not</del> inbord(chain.month,1,12<del style="font-weight: bold; text-decoration: none;">)</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> and</ins> inbord(chain.month,1,12)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"> or (not</del> inbord(chain.day,1,31))</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> and</ins> inbord(chain.day,1,31))</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> then return false</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> else return true end</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- more detailed check for 31.02.0000 needed</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- more detailed check for 31.02.0000 needed</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- check for other calendars needed</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- check for other calendars needed</div></td> </tr> </table> Lemondoge https://en.wikipedia.org/w/index.php?title=Module:DatesWD/sandbox&diff=1149480823&oldid=prev Lemondoge: Create sandbox version of Module:DatesWD 2023-04-12T14:25:03Z <p>Create sandbox version of <a href="/wiki/Module:DatesWD" title="Module:DatesWD">Module:DatesWD</a></p> <p><b>New page</b></p><div>local p = {}<br /> local bool_to_number={ [true]=1, [false]=0 }<br /> local getArgs = require(&#039;Module:Arguments&#039;).getArgs<br /> local err = &quot;-&quot;<br /> <br /> local function purif(str)<br /> if str == &quot;&quot; or str == nil then<br /> return nil<br /> elseif type(tonumber(str)) == &quot;number&quot; then<br /> return math.floor(tonumber(str))<br /> else<br /> return nil<br /> end<br /> -- need .5 -- ,5 number format converter?<br /> end<br /> <br /> local function inbord(val, down, up)<br /> if type(up) ~= &quot;number&quot; or type(down) ~= &quot;number&quot; or type(val) ~= &quot;number&quot; or up &lt; down or val &lt; down or val &gt; up then<br /> return false<br /> else<br /> return true<br /> end<br /> end<br /> <br /> local function isdate ( chain )<br /> if (not type(chain) == &quot;table&quot;)<br /> or (not inbord(chain.year,-9999,9999))<br /> or (not inbord(chain.month,1,12))<br /> or (not inbord(chain.day,1,31))<br /> then return false<br /> else return true end<br /> -- more detailed check for 31.02.0000 needed<br /> -- check for other calendars needed<br /> end<br /> <br /> local function numstr2date(datein)<br /> local nums = {}<br /> local dateout = {}<br /> for num in string.gmatch(datein,&quot;(%d+)&quot;) do<br /> table.insert(nums,purif(num))<br /> end<br /> if #nums ~= 3 then error(&quot;Wrong format: 3 numbers expected&quot;)<br /> elseif not inbord(nums[2],1,12) then error(&quot;Wrong month&quot;)<br /> elseif not inbord(nums[3],1,31) then<br /> dateout = {[&quot;year&quot;]=nums[3], [&quot;month&quot;]=nums[2], [&quot;day&quot;]=nums[1]}<br /> elseif not inbord(nums[1],1,31) then<br /> dateout = {[&quot;year&quot;]=nums[1], [&quot;month&quot;]=nums[2], [&quot;day&quot;]=nums[3]}<br /> else<br /> -- local lang = mw.getContentLanguage()<br /> -- implement lang:formatDate(format,datein,true) here<br /> return error(&quot;Unable to recognize date&quot;)<br /> end<br /> return dateout<br /> end<br /> <br /> local function date2str(datein)<br /> if not isdate(datein) then return error(&quot;Wrong date&quot;) end<br /> local dateout = os.date(&quot;%Y-%m-%d&quot;, os.time(datein))<br /> return dateout<br /> end<br /> <br /> function gri2jd( datein )<br /> if not isdate(datein) then return error(&quot;Wrong date&quot;) end<br /> local year = datein.year<br /> local month = datein.month<br /> local day = datein.day<br /> -- jd calculation<br /> local a = math.floor((14 - month)/12)<br /> local y = year + 4800 - a<br /> local m = month + 12*a - 3<br /> local offset = math.floor(y/4) - math.floor(y/100) + math.floor(y/400) - 32045<br /> local jd = day + math.floor((153*m + 2)/5) + 365*y + offset<br /> -- jd validation<br /> local low, high = -1931076.5, 5373557.49999<br /> if not (low &lt;= jd and jd &lt;= high) then<br /> return error(&quot;Wrong date&quot;)<br /> end<br /> return jd<br /> end<br /> <br /> function jd2jul( jd )<br /> if type(jd) ~= &quot;number&quot; then return error(&quot;Wrong jd&quot;) end<br /> -- calendar date calculation<br /> local c = jd + 32082<br /> local d = math.floor((4*c + 3)/1461)<br /> local e = c - math.floor(1461*d/4)<br /> local m = math.floor((5*e + 2)/153)<br /> local year_out = d - 4800 + math.floor(m/10)<br /> local month_out = m + 3 - 12*math.floor(m/10)<br /> local day_out = e - math.floor((153*m + 2)/5) + 1<br /> -- output<br /> local dateout = {[&quot;year&quot;]=year_out, [&quot;month&quot;]=month_out, [&quot;day&quot;]=day_out}<br /> return dateout<br /> end<br /> <br /> function jul2jd( datein )<br /> if not isdate(datein) then return error(&quot;Wrong date&quot;) end<br /> local year = datein.year<br /> local month = datein.month<br /> local day = datein.day<br /> -- jd calculation<br /> local a = math.floor((14 - month)/12)<br /> local y = year + 4800 - a<br /> local m = month + 12*a - 3<br /> local offset = math.floor(y/4) - 32083<br /> local jd = day + math.floor((153*m + 2)/5) + 365*y + offset<br /> -- jd validation<br /> local low, high = -1930999.5, 5373484.49999<br /> if not (low &lt;= jd and jd &lt;= high) then<br /> return error(&quot;Wrong date&quot;)<br /> end<br /> return jd<br /> end<br /> <br /> function jd2gri( jd )<br /> -- calendar date calculation<br /> local a = jd + 32044<br /> local b = math.floor((4*a + 3) / 146097)<br /> local c = a - math.floor(146097*b/4)<br /> local d = math.floor((4*c+3)/1461)<br /> local e = c - math.floor(1461*d/4)<br /> local m = math.floor((5*e+2)/153)<br /> local day_out = e - math.floor((153*m+2)/5)+1<br /> local month_out = m + 3 - 12*math.floor(m/10)<br /> local year_out = 100*b + d - 4800 + math.floor(m/10)<br /> -- output<br /> local dateout = {[&quot;year&quot;]=year_out, [&quot;month&quot;]=month_out, [&quot;day&quot;]=day_out}<br /> return dateout<br /> end<br /> <br /> -- =p.NthDay(mw.getCurrentFrame():newChild{title=&quot;1&quot;,args={&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;2020&quot;,&quot;%Y-%m-%d&quot;}}) <br /> <br /> function p.NthDay( frame )<br /> local args = getArgs(frame, { frameOnly = true })<br /> local num, wday, mont, yea, format = <br /> purif(args[1]), purif(args[2]), purif(args[3]), purif(args[4]), args[5]<br /> if not format then format = &quot;%Y-%m-%d&quot; end<br /> if not inbord(num,-5,5) then <br /> return error(&quot;The number must be between -5 and 5&quot;)<br /> elseif num == 0 then <br /> return error(&quot;The number must not be zero&quot;) end<br /> if not inbord(wday,0,6) then <br /> return error(&quot;The day of the week must be between 0 and 6&quot;) end<br /> if not inbord(mont,1,12) then <br /> return error(&quot;The month must be between 1 and 12&quot;) end<br /> if not inbord(yea,0,9999) then <br /> return error(&quot;Wrong year number&quot;) end<br /> if inbord(num,1,5) then<br /> local m_start = os.time{year=yea, month=mont, day=1, hour=0}<br /> local m_wds = tonumber(os.date(&quot;%w&quot;, m_start)) <br /> local start_shift = (<br /> (num - bool_to_number[wday &gt;= m_wds]) * 7 <br /> - (m_wds - wday)<br /> ) * 24 * 60 * 60<br /> local tim = m_start + start_shift<br /> if tonumber(os.date(&quot;%m&quot;, tim)) == mont then<br /> return (os.date(format, tim))<br /> else<br /> return (err)<br /> end<br /> elseif inbord(num,-5,-1) then<br /> local m_end = os.time{year = yea, month = mont + 1, day = 1, hour = 0} - 24 * 60 * 60<br /> local m_wde = tonumber(os.date(&quot;%w&quot;, m_end))<br /> local end_shift = ((math.abs(num + 1) + bool_to_number[wday &gt; m_wde]) * 7 <br /> + (m_wde - wday)) * 24 * 60 * 60<br /> local tim = m_end - end_shift<br /> if tonumber(os.date(&quot;%m&quot;, tim)) == mont then<br /> return (os.date(format, tim))<br /> else<br /> return (err)<br /> end<br /> end<br /> end<br /> <br /> -- =p.test(mw.getCurrentFrame():newChild{title=&quot;1&quot;,args={&quot;1.1.2020&quot;}}) <br /> <br /> function p.test(frame)<br /> local args = getArgs(frame, { frameOnly = true })<br /> local input = args[1]<br /> if not input then<br /> return &quot;&quot;<br /> else<br /> local datetest = numstr2date(input)<br /> if isdate(datetest) then<br /> strout = datetest.day .. &quot;.&quot; .. datetest.month .. &quot;.&quot; .. datetest.year .. &quot; = &quot; .. gri2jd(datetest)<br /> return strout<br /> else error(&quot;Not a date&quot;)<br /> end<br /> end<br /> error(&quot;You shouldn&#039;t read this too&quot;)<br /> end<br /> <br /> return p</div> Lemondoge