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" />
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>
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>
38 <xsl:template name=
"date:format-date">
39 <xsl:param name=
"date-time" />
40 <xsl:param name=
"pattern" />
41 <xsl:variable name=
"formatted">
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" />
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" />
60 <xsl:variable name=
"neg" select=
"starts-with($date-time, '-')" />
61 <xsl:variable name=
"no-neg">
63 <xsl:when test=
"$neg or starts-with($date-time, '+')">
64 <xsl:value-of select=
"substring($date-time, 2)" />
67 <xsl:value-of select=
"$date-time" />
71 <xsl:variable name=
"no-neg-length" select=
"string-length($no-neg)" />
72 <xsl:variable name=
"timezone">
74 <xsl:when test=
"substring($no-neg, $no-neg-length) = 'Z'">Z
</xsl:when>
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" />
85 <xsl:if test=
"not(string($timezone)) or
87 (substring($timezone, 2, 2) <= 23 and
88 substring($timezone, 5, 2) <= 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)" />
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 <= 23 and
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" />
113 <xsl:variable name=
"year" select=
"substring($dt, 1, 4) * (($neg * -2) + 1)" />
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" />
123 <xsl:when test=
"substring($dt, 5, 1) = '-'">
124 <xsl:variable name=
"month" select=
"substring($dt, 6, 2)" />
126 <xsl:when test=
"not($month <= 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" />
135 <xsl:when test=
"substring($dt, 8, 1) = '-'">
136 <xsl:variable name=
"day" select=
"substring($dt, 9, 2)" />
137 <xsl:if test=
"$day <= 31">
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" />
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 <= 23 and
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" />
181 <xsl:value-of select=
"$formatted" />
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)" />
195 <xsl:when test=
"not($pattern)" />
196 <xsl:when test='$char =
"'"'
>
198 <xsl:when test='substring($pattern,
2,
1) =
"'"'
>
199 <xsl:text>'</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)" />
212 <xsl:variable name=
"literal-value" select='substring-before(substring($pattern,
2),
"'")'
/>
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)" />
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)" />
240 <xsl:when test=
"not(contains('GyMdhHmsSEDFwWakKz', $char))">
242 Invalid token in format string:
<xsl:value-of select=
"$char" />
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)" />
256 <xsl:variable name=
"next-different-char" select=
"substring(translate($pattern, $char, ''), 1, 1)" />
257 <xsl:variable name=
"pattern-length">
259 <xsl:when test=
"$next-different-char">
260 <xsl:value-of select=
"string-length(substring-before($pattern, $next-different-char))" />
263 <xsl:value-of select=
"string-length($pattern)" />
268 <xsl:when test=
"$char = 'G'">
270 <xsl:when test=
"string($year) = 'NaN'" />
271 <xsl:when test=
"$year > 0">AD
</xsl:when>
272 <xsl:otherwise>BC
</xsl:otherwise>
275 <xsl:when test=
"$char = 'M'">
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)]" />
281 <xsl:when test=
"$pattern-length >= 4">
282 <xsl:value-of select=
"$month-node" />
285 <xsl:value-of select=
"$month-node/@abbr" />
289 <xsl:when test=
"$pattern-length = 2">
290 <xsl:value-of select=
"format-number($month, '00')" />
293 <xsl:value-of select=
"$month" />
297 <xsl:when test=
"$char = 'E'">
299 <xsl:when test=
"string($year) = 'NaN' or string($month) = 'NaN' or string($day) = 'NaN'" />
301 <xsl:variable name=
"month-days" select=
"sum(document('')/*/date:months/date:month[position() < $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) +
309 <xsl:variable name=
"day-node" select=
"document('')/*/date:days/date:day[number($dow)]" />
311 <xsl:when test=
"$pattern-length >= 4">
312 <xsl:value-of select=
"$day-node" />
315 <xsl:value-of select=
"$day-node/@abbr" />
321 <xsl:when test=
"$char = 'a'">
323 <xsl:when test=
"string($hour) = 'NaN'" />
324 <xsl:when test=
"$hour >= 12">PM
</xsl:when>
325 <xsl:otherwise>AM
</xsl:otherwise>
328 <xsl:when test=
"$char = 'z'">
330 <xsl:when test=
"$timezone = 'Z'">UTC
</xsl:when>
331 <xsl:otherwise>UTC
<xsl:value-of select=
"$timezone" /></xsl:otherwise>
335 <xsl:variable name=
"padding">
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'" />
344 <xsl:value-of select=
"substring('0000000000', 1, $pattern-length)" />
349 <xsl:when test=
"$char = 'y'">
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>
356 <xsl:when test=
"$char = 'd'">
358 <xsl:when test=
"string($day) = 'NaN'" />
359 <xsl:otherwise><xsl:value-of select=
"format-number($day, $padding)" /></xsl:otherwise>
362 <xsl:when test=
"$char = 'h'">
363 <xsl:variable name=
"h" select=
"$hour mod 12" />
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>
370 <xsl:when test=
"$char = 'H'">
372 <xsl:when test=
"string($hour) = 'NaN'" />
374 <xsl:value-of select=
"format-number($hour, $padding)" />
378 <xsl:when test=
"$char = 'k'">
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>
385 <xsl:when test=
"$char = 'K'">
387 <xsl:when test=
"string($hour) = 'NaN'" />
388 <xsl:otherwise><xsl:value-of select=
"format-number($hour mod 12, $padding)" /></xsl:otherwise>
391 <xsl:when test=
"$char = 'm'">
393 <xsl:when test=
"string($minute) = 'NaN'" />
395 <xsl:value-of select=
"format-number($minute, $padding)" />
399 <xsl:when test=
"$char = 's'">
401 <xsl:when test=
"string($second) = 'NaN'" />
403 <xsl:value-of select=
"format-number($second, $padding)" />
407 <xsl:when test=
"$char = 'S'">
409 <xsl:when test=
"string($second) = 'NaN'" />
411 <xsl:value-of select=
"format-number(substring-after($second, '.'), $padding)" />
415 <xsl:when test=
"$char = 'F'">
417 <xsl:when test=
"string($day) = 'NaN'" />
419 <xsl:value-of select=
"floor($day div 7) + 1" />
423 <xsl:when test=
"string($year) = 'NaN' or string($month) = 'NaN' or string($day) = 'NaN'" />
425 <xsl:variable name=
"month-days" select=
"sum(document('')/*/date:months/date:month[position() < $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)" />
428 <xsl:when test=
"$char = 'D'">
429 <xsl:value-of select=
"format-number($days, $padding)" />
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" />
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) +
445 <xsl:when test=
"($day - $day-of-week) mod 7">
446 <xsl:value-of select=
"floor(($day - $day-of-week) div 7) + 2" />
449 <xsl:value-of select=
"floor(($day - $day-of-week) div 7) + 1" />
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)" />
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) +
482 <!-- this gives the day of the week, counting from Monday = 1 -->
483 <xsl:variable name=
"dow">
485 <xsl:when test=
"$day-of-week"><xsl:value-of select=
"$day-of-week" /></xsl:when>
486 <xsl:otherwise>7</xsl:otherwise>
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)" />
492 <xsl:when test=
"$start-day >= 4">
493 <xsl:value-of select=
"$week-number + 1" />
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" />
504 <xsl:value-of select=
"$week-number" />