-
Notifications
You must be signed in to change notification settings - Fork 1
/
datetime.formatters.xsl
430 lines (350 loc) · 14.7 KB
/
datetime.formatters.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
DATE AND TIME
- Author: Nils Hörrmann <http://nilshoerrmann.de> based on a utility by Allen Chang <http://chaoticpattern.com>
- See: <http://symphony-cms.com/download/xslt-utilities/view/20506/>
- Version: 2.1
- Release date: 13th December 2011
# Example usage
<xsl:call-template name="datetime">
<xsl:with-param name="date" select="date" />
</xsl:call-template>
# Required Parameters:
- date: Expects a Symphony date node (as provided by the core date field or the Date and Time extension)
# Optional Parameters:
- time: Takes an time string (12:30), defaults to $date/@time
- weekday: Takes a number (monday = 1, tuesday = 2 ...), defaults to $date/@weekday
- format: Takes a format string with the following options:
Y: Year in 4 digits, e. g. 1981, 1992, 2008
y: Year in 2 digits, e. g. 81, 92, 08
M: Month as a full word, e. g. January, March, September
m: Month in 3 letters, e. g. Jan, Mar, Sep
N: Month in digits without leading zero
n: Month in digits with leading zero
D: Day with suffix and no leading zero, e. g. 1st, 23rd
d: Day in digits with leading zero, e. g. 01, 09, 12, 25
x: Day in digits with no leading zero, e. g. 1, 9, 12, 25
T: Time in 24-hours, e. g. 18:30
h: Time in 24-hours with no leading zero, e. g. 4:25
t: Time in 12-hours, e. g. 6:30pm
W: Weekday as a full word, e. g. Monday, Tuesday
w: Weekday in 3 letters, e. g. Mon, Tue, Wed
rfc2822: RFC 2822 formatted date
iso8601: ISO 8601 formatted date
- lang: Takes a language name, e. g. 'en', 'de'
# Special characters
- A backslash used in date formats escapes the following character.
- An underscore represents a non-breakable space.
# Month names and weekdays
For month names and weekdays a special data source, data.datetime.php, is needed. It is bundled with the Date and Time extension, see: <https://github.com/nilshoerrmann/datetime/blob/master/data-sources/data.datetime.php>
# Change log
## Version 2.1
- Bug fixes
## Version 2.0
- Simplified templates
- Renamed main template from `format-date` to `datetime` for consistency reasons within the kit
- Removed timezone for now (this should be reintroduced as full timezone support)
## Version 1.1
- A few cosmetic updates
## Version 1.0
- Initial release
-->
<!--
Date and time parser
-->
<!-- Parse Symphony date node -->
<xsl:template name="datetime">
<xsl:param name="date" />
<xsl:param name="format" select="'m D, Y'" />
<xsl:param name="lang" select="'en'" />
<!-- Parse date -->
<xsl:variable name="year" select="substring($date, 1, 4)" />
<xsl:variable name="month" select="substring($date, 6, 2)" />
<xsl:variable name="day" select="substring($date, 9, 2)" />
<!-- Format date -->
<xsl:call-template name="datetime-formatter">
<xsl:with-param name="year" select="$year" />
<xsl:with-param name="month" select="$month" />
<xsl:with-param name="day" select="$day" />
<xsl:with-param name="time" select="$date/@time" />
<xsl:with-param name="weekday" select="$date/@weekday" />
<xsl:with-param name="format" select="$format" />
<xsl:with-param name="lang" select="$lang" />
</xsl:call-template>
</xsl:template>
<!-- Parse Twitter date string -->
<xsl:template name="datetime-twitter">
<xsl:param name="date" />
<xsl:param name="format" select="'m D, Y'" />
<xsl:param name="lang" select="'en'" />
<!-- Parse date -->
<xsl:variable name="year" select="substring($date, 27, 4)" />
<xsl:variable name="month">
<xsl:if test="/data/datetime/language[@id = 'en']/months/month[@abbr = substring($date, 5, 3)]/@id < 10">0</xsl:if>
<xsl:value-of select="/data/datetime/language[@id = 'en']/months/month[@abbr = substring($date, 5, 3)]/@id" />
</xsl:variable>
<xsl:variable name="day" select="substring($date, 9, 2)" />
<xsl:variable name="time" select="substring($date, 12, 5)" />
<xsl:variable name="weekday" select="/data/datetime/language[@id = 'en']/weekdays/weekday[@abbr = substring($date, 1, 3)]/@id" />
<!-- Format date -->
<xsl:call-template name="datetime-formatter">
<xsl:with-param name="year" select="$year" />
<xsl:with-param name="month" select="$month" />
<xsl:with-param name="day" select="$day" />
<xsl:with-param name="time" select="$time" />
<xsl:with-param name="weekday" select="$weekday" />
<xsl:with-param name="format" select="$format" />
<xsl:with-param name="lang" select="$lang" />
</xsl:call-template>
</xsl:template>
<!-- Parse ISO 8601 date string -->
<xsl:template name="datetime-iso8601">
<xsl:param name="date" />
<xsl:param name="format" select="'m D, Y'" />
<xsl:param name="lang" select="'en'" />
<!-- Parse date -->
<xsl:variable name="year" select="substring($date, 1, 4)" />
<xsl:variable name="month" select="substring($date, 6, 2)" />
<xsl:variable name="day" select="substring($date, 9, 2)" />
<xsl:variable name="time" select="substring($date, 12, 5)" />
<!-- Format date -->
<xsl:call-template name="datetime-formatter">
<xsl:with-param name="year" select="$year" />
<xsl:with-param name="month" select="$month" />
<xsl:with-param name="day" select="$day" />
<xsl:with-param name="time" select="$time" />
<xsl:with-param name="format" select="$format" />
<xsl:with-param name="lang" select="$lang" />
</xsl:call-template>
</xsl:template>
<!--
Date and time formatter
-->
<xsl:template name="datetime-formatter">
<xsl:param name="year" />
<xsl:param name="month" />
<xsl:param name="day" />
<xsl:param name="time" />
<xsl:param name="weekday" />
<xsl:param name="format" />
<xsl:param name="lang" select="'en'" />
<!-- Get format -->
<xsl:variable name="datetime-format">
<xsl:choose>
<!-- RFC 2822: Thu, 17 Jul 1980 17:59:00 +0100 -->
<xsl:when test="$format = 'rfc2822' ">w, d m Y T:00 -0000</xsl:when>
<!-- ISO 8601: 1980-07-17T17:59:00+01:00 -->
<xsl:when test="$format = 'iso8601' ">Y-n-d\TT:00-00:00</xsl:when>
<!-- Custom date format -->
<xsl:otherwise>
<xsl:value-of select="$format"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Format date -->
<xsl:call-template name="datetime-processor">
<xsl:with-param name="year" select="$year" />
<xsl:with-param name="month" select="$month" />
<xsl:with-param name="day" select="$day" />
<xsl:with-param name="time" select="$time" />
<xsl:with-param name="weekday" select="$weekday" />
<xsl:with-param name="format" select="$datetime-format" />
<xsl:with-param name="lang" select="$lang" />
</xsl:call-template>
</xsl:template>
<!--
Date and time processor
-->
<xsl:template name="datetime-processor">
<xsl:param name="year" />
<xsl:param name="month" />
<xsl:param name="day" />
<xsl:param name="time" />
<xsl:param name="weekday" />
<xsl:param name="format" />
<xsl:param name="lang" />
<xsl:variable name="string" select="substring($format, 1, 1)" />
<xsl:variable name="dictionary" select="/data/datetime/language[@id = $lang] | /data/datetime/language[1]" />
<!-- Process date -->
<xsl:choose>
<!-- Year in 4 digits, e. g. 1981, 1992, 2008 -->
<xsl:when test="$string = 'Y'">
<xsl:value-of select="$year" />
</xsl:when>
<!-- Year in 2 digits, e. g. 81, 92, 08 -->
<xsl:when test="$string = 'y'">
<xsl:value-of select="substring($year, 3)" />
</xsl:when>
<!-- Month as a full word, e. g. January, March, September -->
<xsl:when test="$string = 'M'">
<xsl:value-of select="$dictionary/months/month[@id = number($month)]" />
</xsl:when>
<!-- Month in 3 letters, e. g. Jan, Mar, Sep -->
<xsl:when test="$string = 'm'">
<xsl:value-of select="$dictionary/months/month[@id = number($month)]/@abbr" />
</xsl:when>
<!-- Month in digits without leading zero -->
<xsl:when test="$string = 'N'">
<xsl:value-of select="format-number($month, '#0')"/>
</xsl:when>
<!-- Month in digits with leading zero -->
<xsl:when test="$string = 'n'">
<xsl:value-of select="format-number($month, '00')"/>
</xsl:when>
<!-- Day with suffix and no leading zero, e. g. 1st, 23rd -->
<xsl:when test="$string = 'D'">
<xsl:value-of select="format-number($day, '#0')" />
<sup>
<xsl:choose>
<xsl:when test="(substring($day, 2) = 1) and not(substring($day, 1, 1) = 1)">st</xsl:when>
<xsl:when test="(substring($day, 2) = 2) and not(substring($day, 1, 1) = 1)">nd</xsl:when>
<xsl:when test="(substring($day, 2) = 3) and not(substring($day, 1, 1) = 1)">rd</xsl:when>
<xsl:otherwise>th</xsl:otherwise>
</xsl:choose>
</sup>
</xsl:when>
<!-- Day in digits with leading zero, e. g. 01, 09, 12, 25 -->
<xsl:when test="$string = 'd'">
<xsl:value-of select="format-number($day, '00')" />
</xsl:when>
<!-- Day in digits with no leading zero, e. g. 1, 9, 12, 25 -->
<xsl:when test="$string = 'x'">
<xsl:value-of select="format-number($day, '#0')" />
</xsl:when>
<!-- Time in 24-hours, e. g. 18:30 -->
<xsl:when test="$string = 'T'">
<xsl:value-of select="$time" />
</xsl:when>
<!-- Time in 12-hours, e. g. 6:30pm -->
<xsl:when test="$string = 't'">
<xsl:variable name="hour" select="substring($time, 1, 2)" />
<xsl:variable name="minutes" select="substring($time, 4, 2)" />
<xsl:choose>
<xsl:when test="$hour mod 12 = 0">12</xsl:when>
<xsl:otherwise><xsl:value-of select="($hour mod 12)" /></xsl:otherwise>
</xsl:choose>
<xsl:value-of select="concat(':', $minutes)" />
<xsl:choose>
<xsl:when test="$hour < 12">am</xsl:when>
<xsl:otherwise>pm</xsl:otherwise>
</xsl:choose>
</xsl:when>
<!-- Time in 24-hours with no leading zero, e. g. 4:25 -->
<xsl:when test="$string = 'h'">
<xsl:value-of select="format-number(substring-before($time, ':'), '#0')" />
<xsl:value-of select="substring($time, 3)" />
</xsl:when>
<!-- Weekday as a full word, e. g. Monday, Tuesday -->
<xsl:when test="$string = 'W'">
<xsl:value-of select="$dictionary/weekdays/day[@id = $weekday]" />
</xsl:when>
<!-- Weekday in 3 letters, e. g. Mon, Tue, Wed -->
<xsl:when test="$string = 'w'">
<xsl:value-of select="$dictionary/weekdays/day[@id = $weekday]/@abbr" />
</xsl:when>
<!-- Non-breaking space -->
<xsl:when test="$string = '_'">
<xsl:text> </xsl:text>
</xsl:when>
<!-- Escaped letter -->
<xsl:when test="$string = '\'">
<xsl:value-of select="substring($format, 2, 1)" />
</xsl:when>
<!-- Letter -->
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
<!-- Get offset -->
<xsl:variable name="offset">
<xsl:choose>
<xsl:when test="$string = '\'">3</xsl:when>
<xsl:otherwise>2</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Loop -->
<xsl:if test="$string != ''">
<xsl:call-template name="datetime-processor">
<xsl:with-param name="year" select="$year" />
<xsl:with-param name="month" select="$month" />
<xsl:with-param name="day" select="$day" />
<xsl:with-param name="time" select="$time" />
<xsl:with-param name="weekday" select="$weekday" />
<xsl:with-param name="format" select="substring($format, $offset)" />
<xsl:with-param name="lang" select="$lang" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<!--
COMPARE DATES
# Example usage
<xsl:call-template name="compare-dates">
<xsl:with-param name="first" select="'1980-07-17'" />
<xsl:with-param name="second" select="'1983-07-21'" />
<xsl:with-param name="is" select="'<'" />
</xsl:call-template>
# Required Parameters:
- first: First date, formatted as YYYY-MM-DD
- second: Second date, formatted as YYYY-MM-DD
- is: Operator, defaults to '='
# Optional Parameters:
- first-time: Time of the first date
- second-time: Time of the second date
-->
<xsl:template name="compare-dates">
<xsl:param name="first" />
<xsl:param name="second" />
<xsl:param name="first-time" />
<xsl:param name="second-time" />
<xsl:param name="is" select="'='" />
<xsl:variable name="first-stamp" select="concat(translate($first, '-', ''), translate($first-time, ':', ''))" />
<xsl:variable name="second-stamp" select="concat(translate($second, '-', ''), translate($second-time, ':', ''))" />
<xsl:choose>
<xsl:when test="$is = 'earlier than' or $is = '<'">
<xsl:value-of select="boolean($first-stamp < $second-stamp)" />
</xsl:when>
<xsl:when test="$is = 'equal to or earlier than' or $is = '<='">
<xsl:value-of select="boolean($first-stamp <= $second-stamp)" />
</xsl:when>
<xsl:when test="$is = '=' or $is = 'equal to'">
<xsl:value-of select="boolean($first-stamp = $second-stamp)" />
</xsl:when>
<xsl:when test="$is = 'equal to or later than' or $is = '>='">
<xsl:value-of select="boolean($first-stamp >= $second-stamp)" />
</xsl:when>
<xsl:when test="$is = 'later than' or $is = '>'">
<xsl:value-of select="boolean($first-stamp > $second-stamp)" />
</xsl:when>
</xsl:choose>
</xsl:template>
<!--
DATE IN RANGE
# Example usage
<xsl:call-template name="date-in-range">
<xsl:with-param name="date" select="'1983-07-21'" />
<xsl:with-param name="start" select="'1980-07-17'" />
<xsl:with-param name="end" select="'2012-08-04'" />
</xsl:call-template>
# Required Parameters:
- date: Date to check, formatted as YYYY-MM-DD
- start: Start date of range, formatted as YYYY-MM-DD
- end: End date of range, formatted as YYYY-MM-DD
# Optional Parameters:
- date-time: Time of the date to check
- start-time: Start time of the range
- end-time: End time of the range
-->
<xsl:template name="date-in-range">
<xsl:param name="date" />
<xsl:param name="start" />
<xsl:param name="end" />
<xsl:param name="date-time" />
<xsl:param name="start-time" />
<xsl:param name="end-time" />
<xsl:variable name="date-stamp" select="concat(translate($date, '-', ''), translate($date-time, ':', ''))" />
<xsl:variable name="start-stamp" select="concat(translate($start, '-', ''), translate($start-time, ':', ''))" />
<xsl:variable name="end-stamp" select="concat(translate($end, '-', ''), translate($end-time, ':', ''))" />
<xsl:value-of select="boolean($date-stamp >= $start-stamp and $date-stamp <= $end-stamp)" />
</xsl:template>
</xsl:stylesheet>