https://de.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3AISOdateModul:ISOdate - Versionsgeschichte2025-04-07T00:14:05ZVersionsgeschichte dieser Seite in WikipediaMediaWiki 1.44.0-wmf.23https://de.wikipedia.org/w/index.php?title=Modul:ISOdate&diff=195600578&oldid=prevEderporto: 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('Module:DateI18n') -- 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 ''<br />
end<br />
input = mw.text.trim( input )<br />
<br />
-- if empty string then return it<br />
if input == "" then<br />
return input<br />
end<br />
<br />
-- if number then return it<br />
if tonumber( input ) then<br />
return mw.ustring.format( '%04i', input )<br />
end<br />
<br />
-- otherwise use regular expression match<br />
input = mw.ustring.match( input, '^+?(-?%d%d?%d?%d?)-' )<br />
if input and tonumber( input ) then<br />
return mw.ustring.format( '%04i', input )<br />
else<br />
return ''<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 <time> 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( "int", "lang" ) -- get user's chosen language <br />
end<br />
datestr, succeded = p._ISOdate(<br />
mw.text.trim(args[1]),<br />
args.lang, -- language<br />
args.case or '', -- allows to specify grammatical case for the month for languages that use them<br />
args.class or 'dtstart', -- allows to set the html class of the time node where the date is included. <br />
args.trim_year or '100-999' -- 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 "tail" if any<br />
-- regexp hints:<br />
-- 1) Strings starting with "^" and ending with "$" 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 "(\s.+)?"<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 "T" and there could be a "Z" on the end indicating "Zulu" time zone<br />
{dlen=6, tail=7, regexp="^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?):(%d%d?)Z?(%s.*)"}, <br />
{dlen=6, tail=0, regexp="^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?):(%d%d?)Z?$"}, <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="^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?)(%s.+)"},<br />
{dlen=5, tail=0, regexp="^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?)$"},<br />
-- strings starting with YYYY-MM-DD. Year 1-4 digits, the rest 1-2<br />
{dlen=3, tail=4, regexp="^+?(%d%d?%d?%d?)-(%d%d?)-(%d%d?)(%s.+)"},<br />
{dlen=3, tail=0, regexp="^+?(%d%d?%d?%d?)-(%d%d?)-(%d%d?)$"},<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="^+?(%d%d%d%d?)-(%d%d)(%s.+)"}, <br />
-- if whole string is in YYYY-MM form: If Year 1-4 digits, month 1-2 digits<br />
{dlen=2, tail=0, regexp="^+?(%d%d?%d?%d?)-(%d%d?)$"}, <br />
-- string starts with a number -> it has to be 3 or 4 digit long to be a year<br />
{dlen=1, tail=2, regexp="^+?(%d%d%d%d?)(%s.+)"}, <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="^+?(%d%d?%d?%d?)$"},<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 '', patterns)<br />
if datevec[1]=='' 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~='' 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 "tail" if any<br />
-- regexp hints:<br />
-- 1) Strings starting with "^" and ending with "$" 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 "(\s.+)?"<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( "int", "lang" ) -- get user's chosen language <br />
end<br />
datestr, succeded = p._ISOdate(<br />
mw.text.trim(args[1]),<br />
args.lang, -- language<br />
args.case or '', -- allows to specify grammatical case for the month for languages that use them<br />
args.class or 'dtstart', -- allows to set the html class of the time node where the date is included. <br />
args.trim_year or '100-999' -- 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="^(%d%d?)[-./](%d%d?)[-./](%d%d%d%d)(%s.+)"},<br />
{dlen=3, tail=0, regexp="^(%d%d?)[-./](%d%d?)[-./](%d%d%d%d)$"},<br />
{dlen=3, tail=0, regexp="^(%d%d?)%s(%w+)%s(%d%d%d%d)$"},<br />
{dlen=3, tail=0, regexp="^(%w+)%s(%d%d?),%s(%d%d%d%d)$"},<br />
}<br />
<br />
local datevec, tail, formatNum, category = ''<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])>12 then<br />
frame.args[1] = string.format('%04i-%02i-%02i', datevec[3], datevec[2], datevec[1] )<br />
category = '[[Category:Date in DD/MM/YYYY format]]'<br />
return mw.text.trim( p.ISOdate(frame) .. tail);<br />
elseif tonumber(datevec[2])>12 then<br />
frame.args[1] = string.format('%04i-%02i-%02i', datevec[3], datevec[1], datevec[2] )<br />
category = '[[Category:Date in MM/DD/YYYY format]]'<br />
return mw.text.trim( p.ISOdate(frame) .. tail);<br />
end<br />
elseif (formatNum==3 or formatNum==4) and (datevec[3]=='' or datevec[3]~=nil) then<br />
local str = mw.getCurrentFrame():callParserFunction( "#time", { 'Y-m-d', datestr} )<br />
local vec = {str:match( "^(%d%d?%d?%d?)-(%d%d?)-(%d%d?)$" )}<br />
if vec and vec[1]~=nil then<br />
frame.args[1] = string.format('%04i-%02i-%02i', vec[1], vec[2], vec[3] )<br />
category = '[[Category:Date in word format]]'<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 "tail" if any<br />
<br />
local datevec = {'','','','','',''}<br />
local tail = ''<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>0 and vec[pat.tail]~=nil then<br />
tail = mw.ustring.gsub(' ' .. vec[pat.tail], ' +', ' ')<br />
end<br />
formatNum = i<br />
break<br />
end<br />
end<br />
return datevec, tail, formatNum<br />
end<br />
<br />
return p</div>Ederporto