source: ccr2ccd-xslt/trunk/lib/date.format-date.template.xsl@ 838

Last change on this file since 838 was 748, checked in by Richard Braman, 15 years ago

ainitial file from Ken Miller / first pass at coding problems into Level 3 CCD

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