模块:ISOdate

来自萌娘共享
跳转至: 导航搜索

此模块的文档可以在模块:ISOdate/doc创建

  1. --[[
  2. This module is intended for processing of date strings.
  3. Please do not modify this code without applying the changes first at Module:ISOdate/sandbox and testing
  4. at Module:ISOdate/sandbox/testcases and Module talk:ISOdate/sandbox/testcases.
  5. Authors and maintainers:
  6. * User:Parent5446 - original version of the function mimicking template:ISOdate
  7. * User:Jarekt - original version of the functions mimicking template:Date and template:ISOyear
  8. ]]
  9. local p = {}
  10. -- =======================================
  11. -- === Dependencies ======================
  12. -- =======================================
  13. local D = require('Module:Date')
  14. --[[
  15. ISOyear
  16. This function returns year part of date string.
  17. Usage:
  18. {{#invoke:ISOdate|ISOyear|target_string}}
  19. Parameters
  20. 1: The date string
  21. Error Handling:
  22. If the string does not look like it contain the year than the function will not return anything.
  23. That is the preferred treatment for the template:Creator which is the main (only?) template calling it.
  24. ]]
  25. function p.ISOyear( frame )
  26. return p._ISOyear( frame.args[1] )
  27. end
  28. function p._ISOyear( input )
  29. if not input then
  30. return ''
  31. end
  32. input = mw.text.trim( input )
  33. -- if empty string then return it
  34. if input == "" then
  35. return input
  36. end
  37. -- if number then return it
  38. if tonumber( input ) then
  39. return mw.ustring.format( '%04i', input )
  40. end
  41. -- otherwise use regular expression match
  42. input = mw.ustring.match( input, '^+?(-?%d%d?%d?%d?)-' )
  43. if input and tonumber( input ) then
  44. return mw.ustring.format( '%04i', input )
  45. else
  46. return ''
  47. end
  48. end
  49. --[[
  50. ISOdate
  51. This function is the core part of the ISOdate template.
  52. Usage:
  53. {{#invoke:ISOdate|ISOdate|target_string|lang=}}
  54. Parameters:
  55. 1: The date string
  56. lang: The language to display it in
  57. form: Language format (genitive, etc.) for some languages
  58. class: CSS class for the <time> node
  59. Error Handling:
  60. If the string does not look like it contain the proper ISO date than the function will return the original string.
  61. That is the preferred treatment for the template:Information (and similar templates) which calling it.
  62. ]]
  63. function p.ISOdate(frame)
  64. local datestr, succeded
  65. local args = frame.args
  66. if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
  67. args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
  68. end
  69. datestr, succeded = p._ISOdate(
  70. mw.text.trim(args[1]),
  71. args.lang, -- language
  72. args.case or '', -- allows to specify grammatical case for the month for languages that use them
  73. args.class or 'dtstart', -- allows to set the html class of the time node where the date is included.
  74. 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
  75. )
  76. return datestr
  77. end
  78. function p._ISOdate(datestr, lang, case, class, trim_year)
  79. -- pattern: regexp - regular expresion to test; dlen - number of date elements; tail = which element is a "tail" if any
  80. -- regexp hints:
  81. -- 1) Strings starting with "^" and ending with "$" indicate whole string match
  82. -- 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.+)?"
  83. local patterns = {
  84. -- 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
  85. -- date and time can be separated by space or "T" and there could be a "Z" on the end indicating "Zulu" time zone
  86. {dlen=6, tail=7, regexp="^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?):(%d%d?)Z?(%s.*)"},
  87. {dlen=6, tail=0, regexp="^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?):(%d%d?)Z?$"},
  88. -- strings starting with YYYY-MM-DD HH:MM. Year 4 digits, the rest 1-2
  89. -- (if one knows hour and minute than it was probably after a year 1000)
  90. {dlen=5, tail=6, regexp="^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?)(%s.+)"},
  91. {dlen=5, tail=0, regexp="^+?(%d%d%d%d)-(%d%d?)-(%d%d?)[ T](%d%d?):(%d%d?)$"},
  92. -- strings starting with YYYY-MM-DD. Year 1-4 digits, the rest 1-2
  93. {dlen=3, tail=4, regexp="^+?(%d%d?%d?%d?)-(%d%d?)-(%d%d?)(%s.+)"},
  94. {dlen=3, tail=0, regexp="^+?(%d%d?%d?%d?)-(%d%d?)-(%d%d?)$"},
  95. -- strings starting with YYYY-MM. Year 3-4 digits, month 2 digits
  96. -- (want to avoit converting to dates strings like 10-5 = 5
  97. {dlen=2, tail=3, regexp="^+?(%d%d%d%d?)-(%d%d)(%s.+)"},
  98. -- if whole string is in YYYY-MM form: If Year 1-4 digits, month 1-2 digits
  99. {dlen=2, tail=0, regexp="^+?(%d%d?%d?%d?)-(%d%d?)$"},
  100. -- string starts with a number -> it has to be 3 or 4 digit long to be a year
  101. {dlen=1, tail=2, regexp="^+?(%d%d%d%d?)(%s.+)"},
  102. -- if whole string is a number (1-4 digit long) than it will be interpreted as a year
  103. {dlen=1, tail=0, regexp="^+?(%d%d?%d?%d?)$"},
  104. }
  105. -- create datevec based on which variables are provided
  106. local datevec, tail, formatNum
  107. datevec, tail, formatNum = p.test_date_formats(datestr or '', patterns)
  108. if datevec[1]=='' or datevec[1]==nil then
  109. -- quickly return if datestr does not look like date (it could be a template)
  110. return datestr, false
  111. end
  112. -- call p._Date function to format date string
  113. local succeded, datestr2
  114. succeded, datestr2 = pcall( D._Date, datevec, lang, case, class, trim_year)
  115. if succeded and datestr2~='' then
  116. return mw.text.trim( datestr2 .. tail), true
  117. else -- in case of errors return the original string
  118. return datestr, false
  119. end
  120. end
  121. function p.ISOdate_extended(frame)
  122. -- pattern: regexp - regular expresion to test; dlen - number of date elements; tail = which element is a "tail" if any
  123. -- regexp hints:
  124. -- 1) Strings starting with "^" and ending with "$" indicate whole string match
  125. -- 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.+)?"
  126. local datestr, succeded
  127. local args = frame.args
  128. if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
  129. args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
  130. end
  131. datestr, succeded = p._ISOdate(
  132. mw.text.trim(args[1]),
  133. args.lang, -- language
  134. args.case or '', -- allows to specify grammatical case for the month for languages that use them
  135. args.class or 'dtstart', -- allows to set the html class of the time node where the date is included.
  136. 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
  137. )
  138. if succeded then
  139. return datestr
  140. end
  141. local patterns = {
  142. -- Exended set of recognized formats: like MM/DD/YYYY
  143. {dlen=3, tail=4, regexp="^(%d%d?)[-./](%d%d?)[-./](%d%d%d%d)(%s.+)"},
  144. {dlen=3, tail=0, regexp="^(%d%d?)[-./](%d%d?)[-./](%d%d%d%d)$"},
  145. {dlen=3, tail=0, regexp="^(%d%d?)%s(%w+)%s(%d%d%d%d)$"},
  146. {dlen=3, tail=0, regexp="^(%w+)%s(%d%d?),%s(%d%d%d%d)$"},
  147. }
  148. local datevec, tail, formatNum, category = ''
  149. datevec, tail, formatNum = p.test_date_formats(frame.args[1], patterns)
  150. if formatNum==1 or formatNum==2 then
  151. vec = datevec;
  152. if tonumber(datevec[1])>12 then
  153. frame.args[1] = string.format('%04i-%02i-%02i', datevec[3], datevec[2], datevec[1] )
  154. category = '[[Category:Date in DD/MM/YYYY format]]'
  155. return mw.text.trim( p.ISOdate(frame) .. tail);
  156. elseif tonumber(datevec[2])>12 then
  157. frame.args[1] = string.format('%04i-%02i-%02i', datevec[3], datevec[1], datevec[2] )
  158. category = '[[Category:Date in MM/DD/YYYY format]]'
  159. return mw.text.trim( p.ISOdate(frame) .. tail);
  160. end
  161. elseif (formatNum==3 or formatNum==4) and (datevec[3]=='' or datevec[3]~=nil) then
  162. local str = mw.getCurrentFrame():callParserFunction( "#time", { 'Y-m-d', datestr} )
  163. local vec = {str:match( "^(%d%d?%d?%d?)-(%d%d?)-(%d%d?)$" )}
  164. if vec and vec[1]~=nil then
  165. frame.args[1] = string.format('%04i-%02i-%02i', vec[1], vec[2], vec[3] )
  166. category = '[[Category:Date in word format]]'
  167. return p.ISOdate(frame);
  168. end
  169. end
  170. return datestr
  171. end
  172. function p.test_date_formats(datestr, patterns)
  173. -- pattern: regexp - regular expresion to test; dlen - number of date elements; tail = which element is a "tail" if any
  174. local datevec = {'','','','','',''}
  175. local tail = ''
  176. local vec, pat
  177. local formatNum = 0
  178. for i, pat in ipairs( patterns ) do
  179. vec = {datestr:match( pat.regexp )}
  180. if vec and vec[1]~=nil then
  181. for j=1,pat.dlen do
  182. datevec[j] = vec[j]
  183. end
  184. if pat.tail>0 and vec[pat.tail]~=nil then
  185. tail = mw.ustring.gsub(' ' .. vec[pat.tail], ' +', ' ')
  186. end
  187. formatNum = i
  188. break
  189. end
  190. end
  191. return datevec, tail, formatNum
  192. end
  193. return p