Security vulnerability fix
[openemr.git] / ccr / ccd / lib / date.add.template.xsl
blobd3ea7c34d97db469e53e7597c20302000eb6eebe
1 <?xml version="1.0"?>
3 <!-- This is a public domain script released from http://exslt.org/ -->
5 <xsl:stylesheet version="1.0"
6 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
7 xmlns:date="http://exslt.org/dates-and-times"
8 extension-element-prefixes="date">
10 <date:month-lengths>
11 <date:month>31</date:month>
12 <date:month>28</date:month>
13 <date:month>31</date:month>
14 <date:month>30</date:month>
15 <date:month>31</date:month>
16 <date:month>30</date:month>
17 <date:month>31</date:month>
18 <date:month>31</date:month>
19 <date:month>30</date:month>
20 <date:month>31</date:month>
21 <date:month>30</date:month>
22 <date:month>31</date:month>
23 </date:month-lengths>
25 <xsl:template name="date:add">
26 <xsl:param name="date-time" />
27 <xsl:param name="duration" />
28 <xsl:variable name="dt-neg" select="starts-with($date-time, '-')" />
29 <xsl:variable name="dt-no-neg">
30 <xsl:choose>
31 <xsl:when test="$dt-neg or starts-with($date-time, '+')">
32 <xsl:value-of select="substring($date-time, 2)" />
33 </xsl:when>
34 <xsl:otherwise>
35 <xsl:value-of select="$date-time" />
36 </xsl:otherwise>
37 </xsl:choose>
38 </xsl:variable>
39 <xsl:variable name="dt-no-neg-length" select="string-length($dt-no-neg)" />
40 <xsl:variable name="timezone">
41 <xsl:choose>
42 <xsl:when test="substring($dt-no-neg, $dt-no-neg-length) = 'Z'">Z</xsl:when>
43 <xsl:otherwise>
44 <xsl:variable name="tz" select="substring($dt-no-neg, $dt-no-neg-length - 5)" />
45 <xsl:if test="(substring($tz, 1, 1) = '-' or
46 substring($tz, 1, 1) = '+') and
47 substring($tz, 4, 1) = ':'">
48 <xsl:value-of select="$tz" />
49 </xsl:if>
50 </xsl:otherwise>
51 </xsl:choose>
52 </xsl:variable>
53 <xsl:variable name="new-dt">
54 <xsl:if test="not(string($timezone)) or
55 $timezone = 'Z' or
56 (substring($timezone, 2, 2) &lt;= 23 and
57 substring($timezone, 5, 2) &lt;= 59)">
58 <xsl:variable name="dt" select="substring($dt-no-neg, 1, $dt-no-neg-length - string-length($timezone))" />
59 <xsl:variable name="dt-length" select="string-length($dt)" />
60 <xsl:variable name="du-neg" select="starts-with($duration, '-')" />
61 <xsl:variable name="du">
62 <xsl:choose>
63 <xsl:when test="$du-neg"><xsl:value-of select="substring($duration, 2)" /></xsl:when>
64 <xsl:otherwise><xsl:value-of select="$duration" /></xsl:otherwise>
65 </xsl:choose>
66 </xsl:variable>
67 <xsl:if test="starts-with($du, 'P') and
68 not(translate($du, '0123456789PYMDTHS.', ''))">
69 <xsl:variable name="du-date">
70 <xsl:choose>
71 <xsl:when test="contains($du, 'T')"><xsl:value-of select="substring-before(substring($du, 2), 'T')" /></xsl:when>
72 <xsl:otherwise><xsl:value-of select="substring($du, 2)" /></xsl:otherwise>
73 </xsl:choose>
74 </xsl:variable>
75 <xsl:variable name="du-time">
76 <xsl:if test="contains($du, 'T')"><xsl:value-of select="substring-after($du, 'T')" /></xsl:if>
77 </xsl:variable>
78 <xsl:if test="(not($du-date) or
79 (not(translate($du-date, '0123456789YMD', '')) and
80 not(substring-after($du-date, 'D')) and
81 (contains($du-date, 'D') or
82 (not(substring-after($du-date, 'M')) and
83 (contains($du-date, 'M') or
84 not(substring-after($du-date, 'Y'))))))) and
85 (not($du-time) or
86 (not(translate($du-time, '0123456789HMS.', '')) and
87 not(substring-after($du-time, 'S')) and
88 (contains($du-time, 'S') or
89 not(substring-after($du-time, 'M')) and
90 (contains($du-time, 'M') or
91 not(substring-after($du-time, 'Y'))))))">
92 <xsl:variable name="duy-str">
93 <xsl:choose>
94 <xsl:when test="contains($du-date, 'Y')"><xsl:value-of select="substring-before($du-date, 'Y')" /></xsl:when>
95 <xsl:otherwise>0</xsl:otherwise>
96 </xsl:choose>
97 </xsl:variable>
98 <xsl:variable name="dum-str">
99 <xsl:choose>
100 <xsl:when test="contains($du-date, 'M')">
101 <xsl:choose>
102 <xsl:when test="contains($du-date, 'Y')"><xsl:value-of select="substring-before(substring-after($du-date, 'Y'), 'M')" /></xsl:when>
103 <xsl:otherwise><xsl:value-of select="substring-before($du-date, 'M')" /></xsl:otherwise>
104 </xsl:choose>
105 </xsl:when>
106 <xsl:otherwise>0</xsl:otherwise>
107 </xsl:choose>
108 </xsl:variable>
109 <xsl:variable name="dud-str">
110 <xsl:choose>
111 <xsl:when test="contains($du-date, 'D')">
112 <xsl:choose>
113 <xsl:when test="contains($du-date, 'M')"><xsl:value-of select="substring-before(substring-after($du-date, 'M'), 'D')" /></xsl:when>
114 <xsl:when test="contains($du-date, 'Y')"><xsl:value-of select="substring-before(substring-after($du-date, 'Y'), 'D')" /></xsl:when>
115 <xsl:otherwise><xsl:value-of select="substring-before($du-date, 'D')" /></xsl:otherwise>
116 </xsl:choose>
117 </xsl:when>
118 <xsl:otherwise>0</xsl:otherwise>
119 </xsl:choose>
120 </xsl:variable>
121 <xsl:variable name="duh-str">
122 <xsl:choose>
123 <xsl:when test="contains($du-time, 'H')"><xsl:value-of select="substring-before($du-time, 'H')" /></xsl:when>
124 <xsl:otherwise>0</xsl:otherwise>
125 </xsl:choose>
126 </xsl:variable>
127 <xsl:variable name="dumin-str">
128 <xsl:choose>
129 <xsl:when test="contains($du-time, 'M')">
130 <xsl:choose>
131 <xsl:when test="contains($du-time, 'H')"><xsl:value-of select="substring-before(substring-after($du-time, 'H'), 'M')" /></xsl:when>
132 <xsl:otherwise><xsl:value-of select="substring-before($du-time, 'M')" /></xsl:otherwise>
133 </xsl:choose>
134 </xsl:when>
135 <xsl:otherwise>0</xsl:otherwise>
136 </xsl:choose>
137 </xsl:variable>
138 <xsl:variable name="dus-str">
139 <xsl:choose>
140 <xsl:when test="contains($du-time, 'S')">
141 <xsl:choose>
142 <xsl:when test="contains($du-time, 'M')"><xsl:value-of select="substring-before(substring-after($du-time, 'M'), 'S')" /></xsl:when>
143 <xsl:when test="contains($du-time, 'H')"><xsl:value-of select="substring-before(substring-after($du-time, 'H'), 'S')" /></xsl:when>
144 <xsl:otherwise><xsl:value-of select="substring-before($du-time, 'S')" /></xsl:otherwise>
145 </xsl:choose>
146 </xsl:when>
147 <xsl:otherwise>0</xsl:otherwise>
148 </xsl:choose>
149 </xsl:variable>
150 <xsl:variable name="mult" select="($du-neg * -2) + 1" />
151 <xsl:variable name="duy" select="$duy-str * $mult" />
152 <xsl:variable name="dum" select="$dum-str * $mult" />
153 <xsl:variable name="dud" select="$dud-str * $mult" />
154 <xsl:variable name="duh" select="$duh-str * $mult" />
155 <xsl:variable name="dumin" select="$dumin-str * $mult" />
156 <xsl:variable name="dus" select="$dus-str * $mult" />
158 <xsl:variable name="year" select="substring($dt, 1, 4) * (($dt-neg * -2) + 1)" />
159 <xsl:choose>
160 <xsl:when test="$year and
161 string($duy) = 'NaN' or
162 string($dum) = 'NaN' or
163 string($dud) = 'NaN' or
164 string($duh) = 'NaN' or
165 string($dumin) = 'NaN' or
166 string($dus) = 'NaN'" />
167 <xsl:when test="$dt-length > 4 or
168 $dum or $dud or $duh or $dumin or $dus">
169 <xsl:variable name="month">
170 <xsl:choose>
171 <xsl:when test="$dt-length > 4">
172 <xsl:if test="substring($dt, 5, 1) = '-'">
173 <xsl:value-of select="substring($dt, 6, 2)" />
174 </xsl:if>
175 </xsl:when>
176 <xsl:otherwise>1</xsl:otherwise>
177 </xsl:choose>
178 </xsl:variable>
179 <xsl:choose>
180 <xsl:when test="not($month) or $month > 12" />
181 <xsl:when test="$dt-length > 7 or
182 $dud or $duh or $dumin or $dus">
183 <xsl:variable name="day">
184 <xsl:choose>
185 <xsl:when test="$dt-length > 7">
186 <xsl:if test="substring($dt, 8, 1) = '-'">
187 <xsl:value-of select="substring($dt, 9, 2)" />
188 </xsl:if>
189 </xsl:when>
190 <xsl:otherwise>1</xsl:otherwise>
191 </xsl:choose>
192 </xsl:variable>
193 <xsl:choose>
194 <xsl:when test="not($day) or $day > 31" />
195 <xsl:when test="$dt-length > 10 or
196 $duh or $dumin or $dus">
197 <xsl:if test="$dt-length = 10 or
198 (substring($dt, 11, 1) = 'T' and
199 substring($dt, 14, 1) = ':' and
200 substring($dt, 17, 1) = ':')">
201 <xsl:variable name="hour">
202 <xsl:choose>
203 <xsl:when test="$dt-length > 10"><xsl:value-of select="substring($dt, 12, 2)" /></xsl:when>
204 <xsl:otherwise>0</xsl:otherwise>
205 </xsl:choose>
206 </xsl:variable>
207 <xsl:variable name="minute">
208 <xsl:choose>
209 <xsl:when test="$dt-length > 10"><xsl:value-of select="substring($dt, 15, 2)" /></xsl:when>
210 <xsl:otherwise>0</xsl:otherwise>
211 </xsl:choose>
212 </xsl:variable>
213 <xsl:variable name="second">
214 <xsl:choose>
215 <xsl:when test="$dt-length > 10"><xsl:value-of select="substring($dt, 18)" /></xsl:when>
216 <xsl:otherwise>0</xsl:otherwise>
217 </xsl:choose>
218 </xsl:variable>
219 <xsl:if test="$hour &lt;= 23 and $minute &lt;= 59 and $second &lt;= 60">
220 <xsl:variable name="new-second" select="$second + $dus" />
221 <xsl:variable name="new-minute" select="$minute + $dumin + floor($new-second div 60)" />
222 <xsl:variable name="new-hour" select="$hour + $duh + floor($new-minute div 60)" />
223 <xsl:variable name="new-month" select="$month + $dum" />
224 <xsl:call-template name="date:_add-days">
225 <xsl:with-param name="year" select="$year + $duy + floor(($new-month - 1) div 12)" />
226 <xsl:with-param name="month">
227 <xsl:variable name="m">
228 <xsl:choose>
229 <xsl:when test="$new-month &lt; 1"><xsl:value-of select="$new-month + 12" /></xsl:when>
230 <xsl:otherwise><xsl:value-of select="$new-month" /></xsl:otherwise>
231 </xsl:choose>
232 </xsl:variable>
233 <xsl:choose>
234 <xsl:when test="$m mod 12">
235 <xsl:value-of select="format-number($m mod 12, '00')" />
236 </xsl:when>
237 <xsl:otherwise>12</xsl:otherwise>
238 </xsl:choose>
239 </xsl:with-param>
240 <xsl:with-param name="day" select="$day" />
241 <xsl:with-param name="days" select="$dud + floor($new-hour div 24)" />
242 </xsl:call-template>
243 <xsl:text>T</xsl:text>
244 <xsl:value-of select="format-number(($new-hour + 24) mod 24, '00')" />
245 <xsl:text>:</xsl:text>
246 <xsl:value-of select="format-number($new-minute mod 60, '00')" />
247 <xsl:text>:</xsl:text>
248 <xsl:if test="$new-second mod 60 &lt; 10">0</xsl:if>
249 <xsl:value-of select="$new-second mod 60" />
250 <xsl:value-of select="$timezone" />
251 </xsl:if>
252 </xsl:if>
253 </xsl:when>
254 <xsl:otherwise>
255 <xsl:variable name="new-month" select="$month + $dum" />
256 <xsl:call-template name="date:_add-days">
257 <xsl:with-param name="year" select="$year + $duy + floor(($new-month - 1) div 12)" />
258 <xsl:with-param name="month">
259 <xsl:variable name="m">
260 <xsl:choose>
261 <xsl:when test="$new-month &lt; 1"><xsl:value-of select="$new-month + 12" /></xsl:when>
262 <xsl:otherwise><xsl:value-of select="$new-month" /></xsl:otherwise>
263 </xsl:choose>
264 </xsl:variable>
265 <xsl:choose>
266 <xsl:when test="$m mod 12">
267 <xsl:value-of select="format-number($m mod 12, '00')" />
268 </xsl:when>
269 <xsl:otherwise>12</xsl:otherwise>
270 </xsl:choose>
271 </xsl:with-param>
272 <xsl:with-param name="day" select="$day" />
273 <xsl:with-param name="days" select="$dud" />
274 </xsl:call-template>
275 <xsl:value-of select="$timezone" />
276 </xsl:otherwise>
277 </xsl:choose>
278 </xsl:when>
279 <xsl:otherwise>
280 <xsl:variable name="new-month" select="$month + $dum" />
281 <xsl:value-of select="format-number($year + $duy + floor(($new-month - 1) div 12), '0000')" />
282 <xsl:text>-</xsl:text>
283 <xsl:variable name="m">
284 <xsl:choose>
285 <xsl:when test="$new-month &lt; 1"><xsl:value-of select="$new-month + 12" /></xsl:when>
286 <xsl:otherwise><xsl:value-of select="$new-month" /></xsl:otherwise>
287 </xsl:choose>
288 </xsl:variable>
289 <xsl:choose>
290 <xsl:when test="$m mod 12">
291 <xsl:value-of select="format-number($m mod 12, '00')" />
292 </xsl:when>
293 <xsl:otherwise>12</xsl:otherwise>
294 </xsl:choose>
295 <xsl:value-of select="$timezone" />
296 </xsl:otherwise>
297 </xsl:choose>
298 </xsl:when>
299 <xsl:otherwise>
300 <xsl:value-of select="format-number($year + $duy, '0000')" />
301 <xsl:value-of select="$timezone" />
302 </xsl:otherwise>
303 </xsl:choose>
304 </xsl:if>
305 </xsl:if>
306 </xsl:if>
307 </xsl:variable>
308 <xsl:choose>
309 <xsl:when test="string-length($date-time) > 10">
310 <xsl:value-of select="$new-dt" />
311 </xsl:when>
312 <xsl:otherwise>
313 <xsl:value-of select="substring($new-dt, 1, string-length($date-time))" />
314 </xsl:otherwise>
315 </xsl:choose>
316 </xsl:template>
318 <xsl:template name="date:_add-days">
319 <xsl:param name="year" />
320 <xsl:param name="month" />
321 <xsl:param name="day" />
322 <xsl:param name="days" />
323 <xsl:param name="new-day" select="'NaN'" />
324 <xsl:variable name="leap" select="(not($year mod 4) and $year mod 100) or not($year mod 400)" />
325 <xsl:variable name="month-days" select="document('')/*/date:month-lengths/date:month" />
326 <xsl:variable name="days-in-month">
327 <xsl:choose>
328 <xsl:when test="$month = 2 and $leap">
329 <xsl:value-of select="$month-days[number($month)] + 1" />
330 </xsl:when>
331 <xsl:otherwise>
332 <xsl:value-of select="$month-days[number($month)]" />
333 </xsl:otherwise>
334 </xsl:choose>
335 </xsl:variable>
336 <xsl:choose>
337 <xsl:when test="$new-day = 'NaN'">
338 <xsl:call-template name="date:_add-days">
339 <xsl:with-param name="year" select="$year" />
340 <xsl:with-param name="month" select="$month" />
341 <xsl:with-param name="new-day">
342 <xsl:choose>
343 <xsl:when test="$day > $days-in-month">
344 <xsl:value-of select="$days-in-month + $days" />
345 </xsl:when>
346 <xsl:otherwise><xsl:value-of select="$day + $days" /></xsl:otherwise>
347 </xsl:choose>
348 </xsl:with-param>
349 </xsl:call-template>
350 </xsl:when>
351 <xsl:otherwise>
352 <xsl:choose>
353 <xsl:when test="$new-day &lt; 1">
354 <xsl:call-template name="date:_add-days">
355 <xsl:with-param name="year" select="$year - ($month = 1)" />
356 <xsl:with-param name="month">
357 <xsl:choose>
358 <xsl:when test="$month = 1">12</xsl:when>
359 <xsl:otherwise><xsl:value-of select="$month - 1" /></xsl:otherwise>
360 </xsl:choose>
361 </xsl:with-param>
362 <xsl:with-param name="new-day">
363 <xsl:variable name="days-in-new-month">
364 <xsl:choose>
365 <xsl:when test="$leap and $month = 3">29</xsl:when>
366 <xsl:when test="$month = 1">31</xsl:when>
367 <xsl:otherwise>
368 <xsl:value-of select="$month-days[$month - 1]" />
369 </xsl:otherwise>
370 </xsl:choose>
371 </xsl:variable>
372 <xsl:value-of select="$new-day + $days-in-new-month" />
373 </xsl:with-param>
374 </xsl:call-template>
375 </xsl:when>
376 <xsl:when test="$new-day > $days-in-month">
377 <xsl:call-template name="date:_add-days">
378 <xsl:with-param name="year" select="$year + ($month = 12)" />
379 <xsl:with-param name="month">
380 <xsl:choose>
381 <xsl:when test="$month = 12">1</xsl:when>
382 <xsl:otherwise><xsl:value-of select="$month + 1" /></xsl:otherwise>
383 </xsl:choose>
384 </xsl:with-param>
385 <xsl:with-param name="new-day" select="$new-day - $days-in-month" />
386 </xsl:call-template>
387 </xsl:when>
388 <xsl:otherwise>
389 <xsl:value-of select="format-number($year, '0000')" />
390 <xsl:text>-</xsl:text>
391 <xsl:value-of select="format-number($month, '00')" />
392 <xsl:text>-</xsl:text>
393 <xsl:value-of select="format-number($new-day, '00')" />
394 </xsl:otherwise>
395 </xsl:choose>
396 </xsl:otherwise>
397 </xsl:choose>
398 </xsl:template>
400 </xsl:stylesheet>