CCR-CCD module (still in development):
[openemr.git] / ccr / ccd / lib / date.format-date.template.xsl
blob105b31e27728f97f8025a9f33d4abe3e0c8c91dd
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 xmlns:str="http://exslt.org/strings"
9 extension-element-prefixes="date str">
11 <xsl:import href="./str.padding.template.xsl" />
13 <date:months>
14 <date:month length="31" abbr="Jan">January</date:month>
15 <date:month length="28" abbr="Feb">February</date:month>
16 <date:month length="31" abbr="Mar">March</date:month>
17 <date:month length="30" abbr="Apr">April</date:month>
18 <date:month length="31" abbr="May">May</date:month>
19 <date:month length="30" abbr="Jun">June</date:month>
20 <date:month length="31" abbr="Jul">July</date:month>
21 <date:month length="31" abbr="Aug">August</date:month>
22 <date:month length="30" abbr="Sep">September</date:month>
23 <date:month length="31" abbr="Oct">October</date:month>
24 <date:month length="30" abbr="Nov">November</date:month>
25 <date:month length="31" abbr="Dec">December</date:month>
26 </date:months>
28 <date:days>
29 <date:day abbr="Sun">Sunday</date:day>
30 <date:day abbr="Mon">Monday</date:day>
31 <date:day abbr="Tue">Tuesday</date:day>
32 <date:day abbr="Wed">Wednesday</date:day>
33 <date:day abbr="Thu">Thursday</date:day>
34 <date:day abbr="Fri">Friday</date:day>
35 <date:day abbr="Sat">Saturday</date:day>
36 </date:days>
38 <xsl:template name="date:format-date">
39 <xsl:param name="date-time" />
40 <xsl:param name="pattern" />
41 <xsl:variable name="formatted">
42 <xsl:choose>
43 <xsl:when test="starts-with($date-time, '---')">
44 <xsl:call-template name="date:_format-date">
45 <xsl:with-param name="year" select="'NaN'" />
46 <xsl:with-param name="month" select="'NaN'" />
47 <xsl:with-param name="day" select="number(substring($date-time, 4, 2))" />
48 <xsl:with-param name="pattern" select="$pattern" />
49 </xsl:call-template>
50 </xsl:when>
51 <xsl:when test="starts-with($date-time, '--')">
52 <xsl:call-template name="date:_format-date">
53 <xsl:with-param name="year" select="'NaN'" />
54 <xsl:with-param name="month" select="number(substring($date-time, 3, 2))" />
55 <xsl:with-param name="day" select="number(substring($date-time, 6, 2))" />
56 <xsl:with-param name="pattern" select="$pattern" />
57 </xsl:call-template>
58 </xsl:when>
59 <xsl:otherwise>
60 <xsl:variable name="neg" select="starts-with($date-time, '-')" />
61 <xsl:variable name="no-neg">
62 <xsl:choose>
63 <xsl:when test="$neg or starts-with($date-time, '+')">
64 <xsl:value-of select="substring($date-time, 2)" />
65 </xsl:when>
66 <xsl:otherwise>
67 <xsl:value-of select="$date-time" />
68 </xsl:otherwise>
69 </xsl:choose>
70 </xsl:variable>
71 <xsl:variable name="no-neg-length" select="string-length($no-neg)" />
72 <xsl:variable name="timezone">
73 <xsl:choose>
74 <xsl:when test="substring($no-neg, $no-neg-length) = 'Z'">Z</xsl:when>
75 <xsl:otherwise>
76 <xsl:variable name="tz" select="substring($no-neg, $no-neg-length - 5)" />
77 <xsl:if test="(substring($tz, 1, 1) = '-' or
78 substring($tz, 1, 1) = '+') and
79 substring($tz, 4, 1) = ':'">
80 <xsl:value-of select="$tz" />
81 </xsl:if>
82 </xsl:otherwise>
83 </xsl:choose>
84 </xsl:variable>
85 <xsl:if test="not(string($timezone)) or
86 $timezone = 'Z' or
87 (substring($timezone, 2, 2) &lt;= 23 and
88 substring($timezone, 5, 2) &lt;= 59)">
89 <xsl:variable name="dt" select="substring($no-neg, 1, $no-neg-length - string-length($timezone))" />
90 <xsl:variable name="dt-length" select="string-length($dt)" />
91 <xsl:choose>
92 <xsl:when test="substring($dt, 3, 1) = ':' and
93 substring($dt, 6, 1) = ':'">
94 <xsl:variable name="hour" select="substring($dt, 1, 2)" />
95 <xsl:variable name="min" select="substring($dt, 4, 2)" />
96 <xsl:variable name="sec" select="substring($dt, 7)" />
97 <xsl:if test="$hour &lt;= 23 and
98 $min &lt;= 59 and
99 $sec &lt;= 60">
100 <xsl:call-template name="date:_format-date">
101 <xsl:with-param name="year" select="'NaN'" />
102 <xsl:with-param name="month" select="'NaN'" />
103 <xsl:with-param name="day" select="'NaN'" />
104 <xsl:with-param name="hour" select="$hour" />
105 <xsl:with-param name="minute" select="$min" />
106 <xsl:with-param name="second" select="$sec" />
107 <xsl:with-param name="timezone" select="$timezone" />
108 <xsl:with-param name="pattern" select="$pattern" />
109 </xsl:call-template>
110 </xsl:if>
111 </xsl:when>
112 <xsl:otherwise>
113 <xsl:variable name="year" select="substring($dt, 1, 4) * (($neg * -2) + 1)" />
114 <xsl:choose>
115 <xsl:when test="not(number($year))" />
116 <xsl:when test="$dt-length = 4">
117 <xsl:call-template name="date:_format-date">
118 <xsl:with-param name="year" select="$year" />
119 <xsl:with-param name="timezone" select="$timezone" />
120 <xsl:with-param name="pattern" select="$pattern" />
121 </xsl:call-template>
122 </xsl:when>
123 <xsl:when test="substring($dt, 5, 1) = '-'">
124 <xsl:variable name="month" select="substring($dt, 6, 2)" />
125 <xsl:choose>
126 <xsl:when test="not($month &lt;= 12)" />
127 <xsl:when test="$dt-length = 7">
128 <xsl:call-template name="date:_format-date">
129 <xsl:with-param name="year" select="$year" />
130 <xsl:with-param name="month" select="$month" />
131 <xsl:with-param name="timezone" select="$timezone" />
132 <xsl:with-param name="pattern" select="$pattern" />
133 </xsl:call-template>
134 </xsl:when>
135 <xsl:when test="substring($dt, 8, 1) = '-'">
136 <xsl:variable name="day" select="substring($dt, 9, 2)" />
137 <xsl:if test="$day &lt;= 31">
138 <xsl:choose>
139 <xsl:when test="$dt-length = 10">
140 <xsl:call-template name="date:_format-date">
141 <xsl:with-param name="year" select="$year" />
142 <xsl:with-param name="month" select="$month" />
143 <xsl:with-param name="day" select="$day" />
144 <xsl:with-param name="timezone" select="$timezone" />
145 <xsl:with-param name="pattern" select="$pattern" />
146 </xsl:call-template>
147 </xsl:when>
148 <xsl:when test="substring($dt, 11, 1) = 'T' and
149 substring($dt, 14, 1) = ':' and
150 substring($dt, 17, 1) = ':'">
151 <xsl:variable name="hour" select="substring($dt, 12, 2)" />
152 <xsl:variable name="min" select="substring($dt, 15, 2)" />
153 <xsl:variable name="sec" select="substring($dt, 18)" />
154 <xsl:if test="$hour &lt;= 23 and
155 $min &lt;= 59 and
156 $sec &lt;= 60">
157 <xsl:call-template name="date:_format-date">
158 <xsl:with-param name="year" select="$year" />
159 <xsl:with-param name="month" select="$month" />
160 <xsl:with-param name="day" select="$day" />
161 <xsl:with-param name="hour" select="$hour" />
162 <xsl:with-param name="minute" select="$min" />
163 <xsl:with-param name="second" select="$sec" />
164 <xsl:with-param name="timezone" select="$timezone" />
165 <xsl:with-param name="pattern" select="$pattern" />
166 </xsl:call-template>
167 </xsl:if>
168 </xsl:when>
169 </xsl:choose>
170 </xsl:if>
171 </xsl:when>
172 </xsl:choose>
173 </xsl:when>
174 </xsl:choose>
175 </xsl:otherwise>
176 </xsl:choose>
177 </xsl:if>
178 </xsl:otherwise>
179 </xsl:choose>
180 </xsl:variable>
181 <xsl:value-of select="$formatted" />
182 </xsl:template>
184 <xsl:template name="date:_format-date">
185 <xsl:param name="year" />
186 <xsl:param name="month" select="1" />
187 <xsl:param name="day" select="1" />
188 <xsl:param name="hour" select="0" />
189 <xsl:param name="minute" select="0" />
190 <xsl:param name="second" select="0" />
191 <xsl:param name="timezone" select="'Z'" />
192 <xsl:param name="pattern" select="''" />
193 <xsl:variable name="char" select="substring($pattern, 1, 1)" />
194 <xsl:choose>
195 <xsl:when test="not($pattern)" />
196 <xsl:when test='$char = "&apos;"'>
197 <xsl:choose>
198 <xsl:when test='substring($pattern, 2, 1) = "&apos;"'>
199 <xsl:text>&apos;</xsl:text>
200 <xsl:call-template name="date:_format-date">
201 <xsl:with-param name="year" select="$year" />
202 <xsl:with-param name="month" select="$month" />
203 <xsl:with-param name="day" select="$day" />
204 <xsl:with-param name="hour" select="$hour" />
205 <xsl:with-param name="minute" select="$minute" />
206 <xsl:with-param name="second" select="$second" />
207 <xsl:with-param name="timezone" select="$timezone" />
208 <xsl:with-param name="pattern" select="substring($pattern, 3)" />
209 </xsl:call-template>
210 </xsl:when>
211 <xsl:otherwise>
212 <xsl:variable name="literal-value" select='substring-before(substring($pattern, 2), "&apos;")' />
213 <xsl:value-of select="$literal-value" />
214 <xsl:call-template name="date:_format-date">
215 <xsl:with-param name="year" select="$year" />
216 <xsl:with-param name="month" select="$month" />
217 <xsl:with-param name="day" select="$day" />
218 <xsl:with-param name="hour" select="$hour" />
219 <xsl:with-param name="minute" select="$minute" />
220 <xsl:with-param name="second" select="$second" />
221 <xsl:with-param name="timezone" select="$timezone" />
222 <xsl:with-param name="pattern" select="substring($pattern, string-length($literal-value) + 2)" />
223 </xsl:call-template>
224 </xsl:otherwise>
225 </xsl:choose>
226 </xsl:when>
227 <xsl:when test="not(contains('abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $char))">
228 <xsl:value-of select="$char" />
229 <xsl:call-template name="date:_format-date">
230 <xsl:with-param name="year" select="$year" />
231 <xsl:with-param name="month" select="$month" />
232 <xsl:with-param name="day" select="$day" />
233 <xsl:with-param name="hour" select="$hour" />
234 <xsl:with-param name="minute" select="$minute" />
235 <xsl:with-param name="second" select="$second" />
236 <xsl:with-param name="timezone" select="$timezone" />
237 <xsl:with-param name="pattern" select="substring($pattern, 2)" />
238 </xsl:call-template>
239 </xsl:when>
240 <xsl:when test="not(contains('GyMdhHmsSEDFwWakKz', $char))">
241 <xsl:message>
242 Invalid token in format string: <xsl:value-of select="$char" />
243 </xsl:message>
244 <xsl:call-template name="date:_format-date">
245 <xsl:with-param name="year" select="$year" />
246 <xsl:with-param name="month" select="$month" />
247 <xsl:with-param name="day" select="$day" />
248 <xsl:with-param name="hour" select="$hour" />
249 <xsl:with-param name="minute" select="$minute" />
250 <xsl:with-param name="second" select="$second" />
251 <xsl:with-param name="timezone" select="$timezone" />
252 <xsl:with-param name="pattern" select="substring($pattern, 2)" />
253 </xsl:call-template>
254 </xsl:when>
255 <xsl:otherwise>
256 <xsl:variable name="next-different-char" select="substring(translate($pattern, $char, ''), 1, 1)" />
257 <xsl:variable name="pattern-length">
258 <xsl:choose>
259 <xsl:when test="$next-different-char">
260 <xsl:value-of select="string-length(substring-before($pattern, $next-different-char))" />
261 </xsl:when>
262 <xsl:otherwise>
263 <xsl:value-of select="string-length($pattern)" />
264 </xsl:otherwise>
265 </xsl:choose>
266 </xsl:variable>
267 <xsl:choose>
268 <xsl:when test="$char = 'G'">
269 <xsl:choose>
270 <xsl:when test="string($year) = 'NaN'" />
271 <xsl:when test="$year > 0">AD</xsl:when>
272 <xsl:otherwise>BC</xsl:otherwise>
273 </xsl:choose>
274 </xsl:when>
275 <xsl:when test="$char = 'M'">
276 <xsl:choose>
277 <xsl:when test="string($month) = 'NaN'" />
278 <xsl:when test="$pattern-length >= 3">
279 <xsl:variable name="month-node" select="document('')/*/date:months/date:month[number($month)]" />
280 <xsl:choose>
281 <xsl:when test="$pattern-length >= 4">
282 <xsl:value-of select="$month-node" />
283 </xsl:when>
284 <xsl:otherwise>
285 <xsl:value-of select="$month-node/@abbr" />
286 </xsl:otherwise>
287 </xsl:choose>
288 </xsl:when>
289 <xsl:when test="$pattern-length = 2">
290 <xsl:value-of select="format-number($month, '00')" />
291 </xsl:when>
292 <xsl:otherwise>
293 <xsl:value-of select="$month" />
294 </xsl:otherwise>
295 </xsl:choose>
296 </xsl:when>
297 <xsl:when test="$char = 'E'">
298 <xsl:choose>
299 <xsl:when test="string($year) = 'NaN' or string($month) = 'NaN' or string($day) = 'NaN'" />
300 <xsl:otherwise>
301 <xsl:variable name="month-days" select="sum(document('')/*/date:months/date:month[position() &lt; $month]/@length)" />
302 <xsl:variable name="days" select="$month-days + $day + boolean(((not(boolean($year mod 4)) and $year mod 100) or not(boolean($year mod 400))) and $month > 2)" />
303 <xsl:variable name="y-1" select="$year - 1" />
304 <xsl:variable name="dow"
305 select="(($y-1 + floor($y-1 div 4) -
306 floor($y-1 div 100) + floor($y-1 div 400) +
307 $days)
308 mod 7) + 1" />
309 <xsl:variable name="day-node" select="document('')/*/date:days/date:day[number($dow)]" />
310 <xsl:choose>
311 <xsl:when test="$pattern-length >= 4">
312 <xsl:value-of select="$day-node" />
313 </xsl:when>
314 <xsl:otherwise>
315 <xsl:value-of select="$day-node/@abbr" />
316 </xsl:otherwise>
317 </xsl:choose>
318 </xsl:otherwise>
319 </xsl:choose>
320 </xsl:when>
321 <xsl:when test="$char = 'a'">
322 <xsl:choose>
323 <xsl:when test="string($hour) = 'NaN'" />
324 <xsl:when test="$hour >= 12">PM</xsl:when>
325 <xsl:otherwise>AM</xsl:otherwise>
326 </xsl:choose>
327 </xsl:when>
328 <xsl:when test="$char = 'z'">
329 <xsl:choose>
330 <xsl:when test="$timezone = 'Z'">UTC</xsl:when>
331 <xsl:otherwise>UTC<xsl:value-of select="$timezone" /></xsl:otherwise>
332 </xsl:choose>
333 </xsl:when>
334 <xsl:otherwise>
335 <xsl:variable name="padding">
336 <xsl:choose>
337 <xsl:when test="$pattern-length > 10">
338 <xsl:call-template name="str:padding">
339 <xsl:with-param name="length" select="$pattern-length" />
340 <xsl:with-param name="chars" select="'0'" />
341 </xsl:call-template>
342 </xsl:when>
343 <xsl:otherwise>
344 <xsl:value-of select="substring('0000000000', 1, $pattern-length)" />
345 </xsl:otherwise>
346 </xsl:choose>
347 </xsl:variable>
348 <xsl:choose>
349 <xsl:when test="$char = 'y'">
350 <xsl:choose>
351 <xsl:when test="string($year) = 'NaN'" />
352 <xsl:when test="$pattern-length > 2"><xsl:value-of select="format-number($year, $padding)" /></xsl:when>
353 <xsl:otherwise><xsl:value-of select="format-number(substring($year, string-length($year) - 1), $padding)" /></xsl:otherwise>
354 </xsl:choose>
355 </xsl:when>
356 <xsl:when test="$char = 'd'">
357 <xsl:choose>
358 <xsl:when test="string($day) = 'NaN'" />
359 <xsl:otherwise><xsl:value-of select="format-number($day, $padding)" /></xsl:otherwise>
360 </xsl:choose>
361 </xsl:when>
362 <xsl:when test="$char = 'h'">
363 <xsl:variable name="h" select="$hour mod 12" />
364 <xsl:choose>
365 <xsl:when test="string($hour) = 'NaN'"></xsl:when>
366 <xsl:when test="$h"><xsl:value-of select="format-number($h, $padding)" /></xsl:when>
367 <xsl:otherwise><xsl:value-of select="format-number(12, $padding)" /></xsl:otherwise>
368 </xsl:choose>
369 </xsl:when>
370 <xsl:when test="$char = 'H'">
371 <xsl:choose>
372 <xsl:when test="string($hour) = 'NaN'" />
373 <xsl:otherwise>
374 <xsl:value-of select="format-number($hour, $padding)" />
375 </xsl:otherwise>
376 </xsl:choose>
377 </xsl:when>
378 <xsl:when test="$char = 'k'">
379 <xsl:choose>
380 <xsl:when test="string($hour) = 'NaN'" />
381 <xsl:when test="$hour"><xsl:value-of select="format-number($hour, $padding)" /></xsl:when>
382 <xsl:otherwise><xsl:value-of select="format-number(24, $padding)" /></xsl:otherwise>
383 </xsl:choose>
384 </xsl:when>
385 <xsl:when test="$char = 'K'">
386 <xsl:choose>
387 <xsl:when test="string($hour) = 'NaN'" />
388 <xsl:otherwise><xsl:value-of select="format-number($hour mod 12, $padding)" /></xsl:otherwise>
389 </xsl:choose>
390 </xsl:when>
391 <xsl:when test="$char = 'm'">
392 <xsl:choose>
393 <xsl:when test="string($minute) = 'NaN'" />
394 <xsl:otherwise>
395 <xsl:value-of select="format-number($minute, $padding)" />
396 </xsl:otherwise>
397 </xsl:choose>
398 </xsl:when>
399 <xsl:when test="$char = 's'">
400 <xsl:choose>
401 <xsl:when test="string($second) = 'NaN'" />
402 <xsl:otherwise>
403 <xsl:value-of select="format-number($second, $padding)" />
404 </xsl:otherwise>
405 </xsl:choose>
406 </xsl:when>
407 <xsl:when test="$char = 'S'">
408 <xsl:choose>
409 <xsl:when test="string($second) = 'NaN'" />
410 <xsl:otherwise>
411 <xsl:value-of select="format-number(substring-after($second, '.'), $padding)" />
412 </xsl:otherwise>
413 </xsl:choose>
414 </xsl:when>
415 <xsl:when test="$char = 'F'">
416 <xsl:choose>
417 <xsl:when test="string($day) = 'NaN'" />
418 <xsl:otherwise>
419 <xsl:value-of select="floor($day div 7) + 1" />
420 </xsl:otherwise>
421 </xsl:choose>
422 </xsl:when>
423 <xsl:when test="string($year) = 'NaN' or string($month) = 'NaN' or string($day) = 'NaN'" />
424 <xsl:otherwise>
425 <xsl:variable name="month-days" select="sum(document('')/*/date:months/date:month[position() &lt; $month]/@length)" />
426 <xsl:variable name="days" select="$month-days + $day + boolean(((not($year mod 4) and $year mod 100) or not($year mod 400)) and $month > 2)" />
427 <xsl:choose>
428 <xsl:when test="$char = 'D'">
429 <xsl:value-of select="format-number($days, $padding)" />
430 </xsl:when>
431 <xsl:when test="$char = 'w'">
432 <xsl:call-template name="date:_week-in-year">
433 <xsl:with-param name="days" select="$days" />
434 <xsl:with-param name="year" select="$year" />
435 </xsl:call-template>
436 </xsl:when>
437 <xsl:when test="$char = 'W'">
438 <xsl:variable name="y-1" select="$year - 1" />
439 <xsl:variable name="day-of-week"
440 select="(($y-1 + floor($y-1 div 4) -
441 floor($y-1 div 100) + floor($y-1 div 400) +
442 $days)
443 mod 7) + 1" />
444 <xsl:choose>
445 <xsl:when test="($day - $day-of-week) mod 7">
446 <xsl:value-of select="floor(($day - $day-of-week) div 7) + 2" />
447 </xsl:when>
448 <xsl:otherwise>
449 <xsl:value-of select="floor(($day - $day-of-week) div 7) + 1" />
450 </xsl:otherwise>
451 </xsl:choose>
452 </xsl:when>
453 </xsl:choose>
454 </xsl:otherwise>
455 </xsl:choose>
456 </xsl:otherwise>
457 </xsl:choose>
458 <xsl:call-template name="date:_format-date">
459 <xsl:with-param name="year" select="$year" />
460 <xsl:with-param name="month" select="$month" />
461 <xsl:with-param name="day" select="$day" />
462 <xsl:with-param name="hour" select="$hour" />
463 <xsl:with-param name="minute" select="$minute" />
464 <xsl:with-param name="second" select="$second" />
465 <xsl:with-param name="timezone" select="$timezone" />
466 <xsl:with-param name="pattern" select="substring($pattern, $pattern-length + 1)" />
467 </xsl:call-template>
468 </xsl:otherwise>
469 </xsl:choose>
470 </xsl:template>
472 <xsl:template name="date:_week-in-year">
473 <xsl:param name="days" />
474 <xsl:param name="year" />
475 <xsl:variable name="y-1" select="$year - 1" />
476 <!-- this gives the day of the week, counting from Sunday = 0 -->
477 <xsl:variable name="day-of-week"
478 select="($y-1 + floor($y-1 div 4) -
479 floor($y-1 div 100) + floor($y-1 div 400) +
480 $days)
481 mod 7" />
482 <!-- this gives the day of the week, counting from Monday = 1 -->
483 <xsl:variable name="dow">
484 <xsl:choose>
485 <xsl:when test="$day-of-week"><xsl:value-of select="$day-of-week" /></xsl:when>
486 <xsl:otherwise>7</xsl:otherwise>
487 </xsl:choose>
488 </xsl:variable>
489 <xsl:variable name="start-day" select="($days - $dow + 7) mod 7" />
490 <xsl:variable name="week-number" select="floor(($days - $dow + 7) div 7)" />
491 <xsl:choose>
492 <xsl:when test="$start-day >= 4">
493 <xsl:value-of select="$week-number + 1" />
494 </xsl:when>
495 <xsl:otherwise>
496 <xsl:choose>
497 <xsl:when test="not($week-number)">
498 <xsl:call-template name="date:_week-in-year">
499 <xsl:with-param name="days" select="365 + ((not($y-1 mod 4) and $y-1 mod 100) or not($y-1 mod 400))" />
500 <xsl:with-param name="year" select="$y-1" />
501 </xsl:call-template>
502 </xsl:when>
503 <xsl:otherwise>
504 <xsl:value-of select="$week-number" />
505 </xsl:otherwise>
506 </xsl:choose>
507 </xsl:otherwise>
508 </xsl:choose>
509 </xsl:template>
511 </xsl:stylesheet>