1 | /** |
---|
2 | <doc:module date="2001-06-12"> |
---|
3 | <doc:module>exslt:date-time</doc:module> |
---|
4 | <doc:name>dates</doc:name> |
---|
5 | <doc:version>2.0</doc:version> |
---|
6 | <doc:language>exslt:javascript</doc:language> |
---|
7 | <doc:meta> |
---|
8 | <doc:author email="[email protected]" url="http://www.bayes.co.uk">Chris Bayes</doc:author> |
---|
9 | <doc:summary>Implementation of EXSLT - Dates (http://www.exslt.org/date)</doc:summary> |
---|
10 | <doc:todo>_validDuration is not 100%</doc:todo> |
---|
11 | <doc:todo>All timezone code.</doc:todo> |
---|
12 | </doc:meta> |
---|
13 | </doc:module> |
---|
14 | **/ |
---|
15 | var gsSuppliedFormat = ""; |
---|
16 | var gsLang = "en"; |
---|
17 | var gaLang = new Array(); |
---|
18 | // en |
---|
19 | gaLang["en"] = new Array(); |
---|
20 | gaLang["en"]["months"] = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); |
---|
21 | gaLang["en"]["shortMonths"] = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); |
---|
22 | gaLang["en"]["days"] = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); |
---|
23 | gaLang["en"]["shortDays"] = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); |
---|
24 | |
---|
25 | |
---|
26 | var gaDayNames = new Array(); |
---|
27 | gaDayNames["sunday"] = 0;gaDayNames["monday"] = 1;gaDayNames["tuesday"] = 2;gaDayNames["wednesday"] = 3;gaDayNames["thursday"] = 4;gaDayNames["friday"] = 5;gaDayNames["saturday"] = 6; |
---|
28 | var gaMonthCnt = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
---|
29 | var gaMonthCntL = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); |
---|
30 | function _isLeap(y){return ( ((y%4==0)&(y%100!=0))|(y%400==0) );} |
---|
31 | function _dayOfWeek(y,d){return ( ((y-1)+Math.floor((y-1)/4)-Math.floor((y-1)/100)+Math.floor((y-1)/400)+d)%7);} |
---|
32 | function _dayInYear(y,m,d){return (m==0?d:eval( (_isLeap(y)?gaMonthCntL.slice(0,m).join("+"):gaMonthCnt.slice(0,m).join("+")) )+(d*1) );} |
---|
33 | /** |
---|
34 | <doc:function date="2001-06-12"> |
---|
35 | <doc:name>add</doc:name> |
---|
36 | <doc:version>2.0</doc:version> |
---|
37 | <doc:return type="string" /> |
---|
38 | <doc:args> |
---|
39 | <doc:arg name="date" type="string" default="''" optional="no"></doc:arg> |
---|
40 | <doc:arg name="duration" type="string" default="''" optional="no"></doc:arg> |
---|
41 | </doc:args> |
---|
42 | </doc:function> |
---|
43 | **/ |
---|
44 | function add(){// |
---|
45 | var d1,d2; |
---|
46 | if (((d1 = _validDate(arguments[0], "xs:dateTime")) || |
---|
47 | (d1 = _validDate(arguments[0], "xs:date")) || |
---|
48 | (d1 = _validDate(arguments[0], "xs:gYearMonth")) || |
---|
49 | (d1 = _validDate(arguments[0], "xs:gYear")) |
---|
50 | ) && |
---|
51 | (d2 = _validDuration(arguments[1]))) |
---|
52 | return _formatDate(d2.addDate(d1), gsSuppliedFormat); |
---|
53 | return ''; |
---|
54 | } |
---|
55 | /** |
---|
56 | <doc:function date="2001-06-12"> |
---|
57 | <doc:name>addDuration</doc:name> |
---|
58 | <doc:version>2.0</doc:version> |
---|
59 | <doc:return type="string" /> |
---|
60 | <doc:args> |
---|
61 | <doc:arg name="duration" type="string" default="''" optional="no"></doc:arg> |
---|
62 | <doc:arg name="duration" type="string" default="''" optional="no"></doc:arg> |
---|
63 | </doc:args> |
---|
64 | </doc:function> |
---|
65 | **/ |
---|
66 | function addDuration(duration1, duration2){ |
---|
67 | if ((d1 = _validDuration(duration1)) && (d2 = _validDuration(duration2))){ |
---|
68 | var d1mnth = d1.years *12 + d1.months; |
---|
69 | var d1secs = d1.days*86400 + d1.hours*3600 + d1.minutes*60 + (d1.seconds+'.'+d1.fraction)*1; |
---|
70 | var d2mnth = d2.years *12 + d2.months; |
---|
71 | var d2secs = d2.days*86400 + d2.hours*3600 + d2.minutes*60 + (d2.seconds+'.'+d2.fraction)*1; |
---|
72 | var mnthdiff = (d1.pm+d1mnth)*1 + (d2.pm+d2mnth)*1; |
---|
73 | var secsdiff = (d1.pm+d1secs)*1 + (d2.pm+d2secs)*1; |
---|
74 | if ((mnthdiff < 0 && secsdiff > 0) || (mnthdiff > 0 && secsdiff < 0)) |
---|
75 | return ''; |
---|
76 | var secs = secsdiff % 60; |
---|
77 | var pm = (mnthdiff<0||secsdiff<0?'-':''); |
---|
78 | mnthdiff = Math.abs(mnthdiff); |
---|
79 | secsdiff = Math.abs(secsdiff); |
---|
80 | return _formatDuration(new Duration(pm, 0, mnthdiff, Math.floor(secsdiff/86400), Math.floor((secsdiff%86400)/3600), Math.floor((secsdiff%3600)/60), Math.floor(secs), Math.floor(secs%1))); |
---|
81 | } |
---|
82 | return ''; |
---|
83 | } |
---|
84 | /** |
---|
85 | <doc:function date="2001-06-12"> |
---|
86 | <doc:name>date</doc:name> |
---|
87 | <doc:version>2.0</doc:version> |
---|
88 | <doc:return type="string" /> |
---|
89 | <doc:args> |
---|
90 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
91 | </doc:args> |
---|
92 | </doc:function> |
---|
93 | **/ |
---|
94 | function date(){// |
---|
95 | if (arguments.length > 0){ |
---|
96 | var oDate; |
---|
97 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
98 | (oDate = _validDate(arguments[0], "xs:date")) |
---|
99 | ) |
---|
100 | return _formatDate(oDate, "xs:date"); |
---|
101 | else |
---|
102 | return ''; |
---|
103 | }else |
---|
104 | return _formatDate(new Date(), "xs:date"); |
---|
105 | } |
---|
106 | /** |
---|
107 | <doc:function date="2001-06-12"> |
---|
108 | <doc:name>dateTime</doc:name> |
---|
109 | <doc:version>2.0</doc:version> |
---|
110 | <doc:return type="string" /> |
---|
111 | <doc:args /> |
---|
112 | </doc:function> |
---|
113 | **/ |
---|
114 | function dateTime(){//must be UTC/Z |
---|
115 | if (arguments.length > 0) |
---|
116 | dateTime_invalidArgument.error; |
---|
117 | var oDate = new Date(); |
---|
118 | return _formatDate(oDate, "xs:dateTime"); |
---|
119 | } |
---|
120 | /** |
---|
121 | <doc:function date="2001-06-12"> |
---|
122 | <doc:name>dayAbbreviation</doc:name> |
---|
123 | <doc:version>2.0</doc:version> |
---|
124 | <doc:return type="string" /> |
---|
125 | <doc:args> |
---|
126 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
127 | <doc:arg name="date-format" type="string" default="''" optional="yes"></doc:arg> |
---|
128 | <doc:arg name="context" type="context" default="null" optional="yes"></doc:arg> |
---|
129 | </doc:args> |
---|
130 | </doc:function> |
---|
131 | **/ |
---|
132 | function dayAbbreviation(){ |
---|
133 | if (arguments.length > 0){ |
---|
134 | var oDate; |
---|
135 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
136 | (oDate = _validDate(arguments[0], "xs:date")) |
---|
137 | ) |
---|
138 | return _dayAbbreviation(oDate.getDay(), arguments[1], arguments[2]); |
---|
139 | else |
---|
140 | return ''; |
---|
141 | }else |
---|
142 | return _dayAbbreviation(new Date().getDay(), arguments[1], arguments[2]); |
---|
143 | } |
---|
144 | function _dayAbbreviation(num, dfName, ctx){ |
---|
145 | if (ctx != null){ |
---|
146 | var nodes = ctx.nextNode.selectNodes( |
---|
147 | "//*[local-name()='date-format' and namespace-uri()='http://exslt.org/dates-and-times' and @name='" + dfName +"' ]" + |
---|
148 | "/*[local-name()='days' and namespace-uri()='http://exslt.org/dates-and-times']" + |
---|
149 | "/*[local-name()='day' and namespace-uri()='http://exslt.org/dates-and-times']/@abbr"); |
---|
150 | if (nodes != null) |
---|
151 | return nodes[num].nodeValue; |
---|
152 | else |
---|
153 | return ''; |
---|
154 | }else{ //we could try local |
---|
155 | return gaLang[gsLang]["shortDays"][num*1]; |
---|
156 | } |
---|
157 | } |
---|
158 | /** |
---|
159 | <doc:function date="2001-06-12"> |
---|
160 | <doc:name>dayInMonth</doc:name> |
---|
161 | <doc:version>2.0</doc:version> |
---|
162 | <doc:return type="number" /> |
---|
163 | <doc:args> |
---|
164 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
165 | </doc:args> |
---|
166 | </doc:function> |
---|
167 | **/ |
---|
168 | function dayInMonth(){// |
---|
169 | if (arguments.length > 0){ |
---|
170 | var oDate; |
---|
171 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
172 | (oDate = _validDate(arguments[0], "xs:date")) || |
---|
173 | (oDate = _validDate(arguments[0], "xs:gMonthDay")) || |
---|
174 | (oDate = _validDate(arguments[0], "xs:gDay")) |
---|
175 | ) |
---|
176 | return oDate.getDate(); |
---|
177 | else |
---|
178 | return Number.NaN |
---|
179 | }else |
---|
180 | return new Date().getDate(); |
---|
181 | } |
---|
182 | /** |
---|
183 | <doc:function date="2001-06-12"> |
---|
184 | <doc:name>dayInWeek</doc:name> |
---|
185 | <doc:version>2.0</doc:version> |
---|
186 | <doc:return type="number" /> |
---|
187 | <doc:args> |
---|
188 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
189 | <doc:arg name="date-format" type="string" default="''" optional="yes"></doc:arg> |
---|
190 | <doc:arg name="context" type="context" default="null" optional="yes"></doc:arg> |
---|
191 | </doc:args> |
---|
192 | </doc:function> |
---|
193 | **/ |
---|
194 | function dayInWeek(){// |
---|
195 | if (arguments.length > 0){ |
---|
196 | var oDate; |
---|
197 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
198 | (oDate = _validDate(arguments[0], "xs:date")) |
---|
199 | ) |
---|
200 | return _dayInWeek(oDate, arguments[1], arguments[2]); |
---|
201 | else |
---|
202 | return Number.NaN; |
---|
203 | }else |
---|
204 | return _dayInWeek(new Date(), arguments[1], arguments[2]); |
---|
205 | } |
---|
206 | function _dayInWeek(oDate, dfName, ctx){ |
---|
207 | var fdow = 0; |
---|
208 | if (ctx != null){ |
---|
209 | var nodes = ctx.nextNode.selectSingleNode( |
---|
210 | "//*[local-name()='date-format' and namespace-uri()='http://exslt.org/dates-and-times' and @name='" + dfName +"' ]" + |
---|
211 | "/@first-day-of-week"); |
---|
212 | if (nodes != null) |
---|
213 | fdow = gaDayNames[nodes.nodeValue]; |
---|
214 | } |
---|
215 | return (oDate.getDay()-fdow<0?oDate.getDay()-fdow+7:oDate.getDay()-fdow)+1; |
---|
216 | } |
---|
217 | /** |
---|
218 | <doc:function date="2001-06-12"> |
---|
219 | <doc:name>dayInYear</doc:name> |
---|
220 | <doc:version>2.0</doc:version> |
---|
221 | <doc:return type="number" /> |
---|
222 | <doc:args> |
---|
223 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
224 | </doc:args> |
---|
225 | </doc:function> |
---|
226 | **/ |
---|
227 | function dayInYear(){ |
---|
228 | if (arguments.length > 0){ |
---|
229 | var oDate; |
---|
230 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
231 | (oDate = _validDate(arguments[0], "xs:date")) |
---|
232 | ) |
---|
233 | return _dayInYear(oDate.getFullYear(), oDate.getMonth(), oDate.getDate()); |
---|
234 | else |
---|
235 | return Number.NaN; |
---|
236 | }else{ |
---|
237 | var oDate = new Date() |
---|
238 | return _dayInYear(oDate.getFullYear(), oDate.getMonth(), oDate.getDate()); |
---|
239 | } |
---|
240 | } |
---|
241 | /** |
---|
242 | <doc:function date="2001-06-12"> |
---|
243 | <doc:name>dayName</doc:name> |
---|
244 | <doc:version>2.0</doc:version> |
---|
245 | <doc:return type="string" /> |
---|
246 | <doc:args> |
---|
247 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
248 | <doc:arg name="date-format" type="string" default="''" optional="yes"></doc:arg> |
---|
249 | <doc:arg name="context" type="context" default="null" optional="yes"></doc:arg> |
---|
250 | </doc:args> |
---|
251 | </doc:function> |
---|
252 | **/ |
---|
253 | function dayName(){ |
---|
254 | if (arguments.length > 0){ |
---|
255 | var oDate; |
---|
256 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
257 | (oDate = _validDate(arguments[0], "xs:date")) |
---|
258 | ) |
---|
259 | return _dayName(oDate.getDay(), arguments[1], arguments[2]); |
---|
260 | else |
---|
261 | return ''; |
---|
262 | }else |
---|
263 | return _dayName(new Date().getDay(), arguments[1], arguments[2]); |
---|
264 | } |
---|
265 | function _dayName(num, dfName, ctx){ |
---|
266 | if (ctx != null){ |
---|
267 | var nodes = ctx.nextNode.selectNodes( |
---|
268 | "//*[local-name()='date-format' and namespace-uri()='http://exslt.org/dates-and-times' and @name='" + dfName +"' ]" + |
---|
269 | "/*[local-name()='days' and namespace-uri()='http://exslt.org/dates-and-times']" + |
---|
270 | "/*[local-name()='day' and namespace-uri()='http://exslt.org/dates-and-times']/text()"); |
---|
271 | if (nodes != null) |
---|
272 | return nodes[num].nodeValue; |
---|
273 | else |
---|
274 | return ''; |
---|
275 | }else |
---|
276 | return gaLang[gsLang]["days"][num*1]; |
---|
277 | } |
---|
278 | /** |
---|
279 | <doc:function date="2001-06-12"> |
---|
280 | <doc:name>dayOfWeekInMonth</doc:name> |
---|
281 | <doc:version>2.0</doc:version> |
---|
282 | <doc:return type="number" /> |
---|
283 | <doc:args> |
---|
284 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
285 | <doc:arg name="date-format" type="string" default="''" optional="yes"></doc:arg> |
---|
286 | <doc:arg name="context" type="context" default="null" optional="yes"></doc:arg> |
---|
287 | </doc:args> |
---|
288 | </doc:function> |
---|
289 | **/ |
---|
290 | function dayOfWeekInMonth(){ |
---|
291 | if (arguments.length > 0){ |
---|
292 | var oDate; |
---|
293 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
294 | (oDate = _validDate(arguments[0], "xs:date")) |
---|
295 | ) |
---|
296 | return _dayOfWeekInMonth(oDate, arguments[1], arguments[2]); |
---|
297 | else |
---|
298 | return Number.NaN; |
---|
299 | }else |
---|
300 | return _dayOfWeekInMonth(new Date(), arguments[1], arguments[2]); |
---|
301 | } |
---|
302 | function _dayOfWeekInMonth(oDate, dfName, ctx){ |
---|
303 | var fdow = 0; |
---|
304 | if (ctx != null){ |
---|
305 | var nodes = ctx.nextNode.selectSingleNode( |
---|
306 | "//*[local-name()='date-format' and namespace-uri()='http://exslt.org/dates-and-times' and @name='" + dfName +"' ]" + |
---|
307 | "/@first-day-of-week"); |
---|
308 | if (nodes != null) |
---|
309 | fdow = gaDayNames[nodes.nodeValue]; |
---|
310 | } |
---|
311 | if (fdow > (oDate.getDay()-1)) |
---|
312 | return Math.floor((oDate.getDate())/7)+1; |
---|
313 | else |
---|
314 | return Math.floor((oDate.getDate())/7) + 1; |
---|
315 | } |
---|
316 | /** |
---|
317 | <doc:function date="2001-06-12"> |
---|
318 | <doc:name>difference</doc:name> |
---|
319 | <doc:version>2.0</doc:version> |
---|
320 | <doc:return type="string" /> |
---|
321 | <doc:args> |
---|
322 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
323 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
324 | </doc:args> |
---|
325 | </doc:function> |
---|
326 | **/ |
---|
327 | function difference(){ |
---|
328 | if (((d1 = _validDate(arguments[0], "xs:dateTime")) || |
---|
329 | (d1 = _validDate(arguments[0], "xs:date")) || |
---|
330 | (d1 = _validDate(arguments[0], "xs:gYearMonth")) || |
---|
331 | (d1 = _validDate(arguments[0], "xs:gYear")) |
---|
332 | ) && |
---|
333 | ((d2 = _validDate(arguments[1], "xs:dateTime")) || |
---|
334 | (d2 = _validDate(arguments[1], "xs:date")) || |
---|
335 | (d2 = _validDate(arguments[1], "xs:gYearMonth")) || |
---|
336 | (d2 = _validDate(arguments[1], "xs:gYear")) |
---|
337 | ) |
---|
338 | ){// truncation |
---|
339 | var diffMil = d2.valueOf() - d1.valueOf(); |
---|
340 | return _formatDuration(diffMil); |
---|
341 | } |
---|
342 | return Number.NaN; |
---|
343 | } |
---|
344 | /** |
---|
345 | <doc:function date="2001-06-12"> |
---|
346 | <doc:name>duration</doc:name> |
---|
347 | <doc:version>2.0</doc:version> |
---|
348 | <doc:return type="string" /> |
---|
349 | <doc:args> |
---|
350 | <doc:arg name="seconds" type="number" default="''" optional="yes"></doc:arg> |
---|
351 | </doc:args> |
---|
352 | </doc:function> |
---|
353 | **/ |
---|
354 | function duration(){ |
---|
355 | var oDate; |
---|
356 | if (arguments.length > 0){ |
---|
357 | var nN = new Number(arguments[0]); |
---|
358 | if (isNaN(nN)) |
---|
359 | return ''; |
---|
360 | else |
---|
361 | return _formatDuration(arguments[0] * 1000); |
---|
362 | }else |
---|
363 | return _formatDuration(seconds() * 1000); |
---|
364 | } |
---|
365 | /** |
---|
366 | <doc:function date="2001-06-12"> |
---|
367 | <doc:name>formatDate</doc:name> |
---|
368 | <doc:version>1.0</doc:version> |
---|
369 | <doc:return type="string" /> |
---|
370 | <doc:args> |
---|
371 | <doc:arg name="date" type="string" default="''" optional="no"></doc:arg> |
---|
372 | <doc:arg name="format" type="string" default="''" optional="no"></doc:arg> |
---|
373 | <doc:arg name="date-format" type="string" default="''" optional="no"></doc:arg> |
---|
374 | <doc:arg name="context" type="document" default="null" optional="yes"></doc:arg> |
---|
375 | </doc:args> |
---|
376 | </doc:function> |
---|
377 | **/ |
---|
378 | function formatDate(date, format, dateF, ctx){ |
---|
379 | var oDate; |
---|
380 | var re; |
---|
381 | var retString = ""; |
---|
382 | if ((oDate = _validDate(date, "xs:dateTime")) || |
---|
383 | (oDate = _validDate(date, "xs:date")) || |
---|
384 | (oDate = _validDate(date, "xs:time")) || |
---|
385 | (oDate = _validDate(date, "xs:gYearMonth")) || |
---|
386 | (oDate = _validDate(date, "xs:gYear")) || |
---|
387 | (oDate = _validDate(date, "xs:gMonthDay")) || |
---|
388 | (oDate = _validDate(date, "xs:gMonth")) || |
---|
389 | (oDate = _validDate(date, "xs:gDay")) |
---|
390 | ){ |
---|
391 | for (var i=0; i < format.length;){ |
---|
392 | var s = i; |
---|
393 | switch(format.substr(i, 1)){ |
---|
394 | case "G":// era designator (Text) AD |
---|
395 | while (format.substr(i, 1)=="G"){i++;} |
---|
396 | if (oDate.getFullYear() < 0){ |
---|
397 | retString += "BC"; |
---|
398 | }else{ |
---|
399 | retString += "AD"; |
---|
400 | } |
---|
401 | break; |
---|
402 | case "y":// year (Number) 1996 |
---|
403 | while (format.substr(i, 1)=="y"){i++;} |
---|
404 | if (i-s > 2){ |
---|
405 | retString += oDate.getFullYear(); |
---|
406 | }else{ |
---|
407 | retString += oDate.getFullYear().toString().substring(4-(i-s)); |
---|
408 | } |
---|
409 | break; |
---|
410 | case "M":// month in year (Text & Number) July & 07 |
---|
411 | while (format.substr(i, 1)=="M"){i++;} |
---|
412 | if (i-s <= 2){ |
---|
413 | retString += pad(oDate.getMonth()+1); |
---|
414 | }else{ |
---|
415 | retString += _monthName(oDate.getMonth(), dateF, ctx); |
---|
416 | } |
---|
417 | break; |
---|
418 | case "d":// day in month (Number) 10 |
---|
419 | while (format.substr(i, 1)=="d"){i++;} |
---|
420 | retString += pad(oDate.getDate()); |
---|
421 | break; |
---|
422 | case "h":// hour in am/pm (1~12) (Number) 12 |
---|
423 | while (format.substr(i, 1)=="h"){i++;} |
---|
424 | if (oDate.getHours() > 12){ |
---|
425 | retString += pad(oDate.getHours()-12); |
---|
426 | }else{ |
---|
427 | retString += pad(oDate.getHours()); |
---|
428 | } |
---|
429 | break; |
---|
430 | case "H":// hour in day (0~23) (Number) 0 |
---|
431 | while (format.substr(i, 1)=="H"){i++;} |
---|
432 | retString += oDate.getHours(); |
---|
433 | break; |
---|
434 | case "m":// minute in hour (Number) 30 |
---|
435 | while (format.substr(i, 1)=="m"){i++;} |
---|
436 | retString += pad(oDate.getMinutes()); |
---|
437 | break; |
---|
438 | case "s":// second in minute (Number) 55 |
---|
439 | while (format.substr(i, 1)=="s"){i++;} |
---|
440 | retString += pad(oDate.getSeconds()); |
---|
441 | break; |
---|
442 | case "S":// millisecond (Number) 978 |
---|
443 | while (format.substr(i, 1)=="S"){i++;} |
---|
444 | retString += oDate.getMilliseconds(); |
---|
445 | break; |
---|
446 | case "E":// day in week (Text) Tuesday |
---|
447 | while (format.substr(i, 1)=="E"){i++;} |
---|
448 | retString += dayInWeek(oDate, dateF, ctx); |
---|
449 | break; |
---|
450 | case "D":// day in year (Number) 189 |
---|
451 | while (format.substr(i, 1)=="D"){i++;} |
---|
452 | retString += _dayInYear(oDate.getFullYear(), oDate.getMonths(), oDate.getDate()); |
---|
453 | break; |
---|
454 | case "F":// day of week in month (Number) 2 (2nd Wed in July) |
---|
455 | while (format.substr(i, 1)=="F"){i++;} |
---|
456 | retString += dayOfWeekInMonth(oDate, dateF, ctx); |
---|
457 | break; |
---|
458 | case "w":// week in year (Number) 27 |
---|
459 | while (format.substr(i, 1)=="w"){i++;} |
---|
460 | retString += weekInYear(oDate, dateF, ctx); |
---|
461 | break; |
---|
462 | case "W":// week in month (Number) 2 |
---|
463 | while (format.substr(i, 1)=="W"){i++;} |
---|
464 | retString += weekInMonth(oDate, dateF, ctx); |
---|
465 | break; |
---|
466 | case "a":// am/pm marker (Text) PM |
---|
467 | while (format.substr(i, 1)=="a"){i++;} |
---|
468 | if (oDate.getHours() > 12 ){ |
---|
469 | retString += "PM" |
---|
470 | }else{ |
---|
471 | retString += "AM" |
---|
472 | } |
---|
473 | break; |
---|
474 | case "k":// hour in day (1~24) (Number) 24 |
---|
475 | while (format.substr(i, 1)=="k"){i++;} |
---|
476 | retString += oDate.getHours(); |
---|
477 | break; |
---|
478 | case "K":// hour in am/pm (0~11) (Number) 0 |
---|
479 | while (format.substr(i, 1)=="K"){i++;} |
---|
480 | if (oDate.getHours() > 12){ |
---|
481 | retString += oDate.getHours()-12; |
---|
482 | }else{ |
---|
483 | retString += oDate.getHours(); |
---|
484 | } |
---|
485 | break; |
---|
486 | case "z":// time zone (Text) Pacific Standard Time |
---|
487 | |
---|
488 | |
---|
489 | |
---|
490 | while (format.substr(i, 1)=="z"){i++;} |
---|
491 | var tzo = oDate.getTimezoneOffset(); |
---|
492 | retString += (tzo < 0?"-":"+") + pad(Math.abs(tzo / 60)) + ":" + pad(tzo % 60); |
---|
493 | break; |
---|
494 | case "'":// escape for text (Delimiter) |
---|
495 | if (format.substr(i+1, 1) == "'"){ |
---|
496 | i++; |
---|
497 | while (format.substr(i, 1)=="'"){i++;} |
---|
498 | retString += "'"; |
---|
499 | }else{ |
---|
500 | i++; |
---|
501 | while (format.substr(i, 1)!="'" && i <= format.length){retString += format.substr(i++, 1);} |
---|
502 | if (i >= format.length)return ''; |
---|
503 | i++; |
---|
504 | } |
---|
505 | break; |
---|
506 | default: |
---|
507 | retString += format.substr(i, 1); |
---|
508 | i++; |
---|
509 | break; |
---|
510 | } |
---|
511 | } |
---|
512 | } |
---|
513 | return retString; |
---|
514 | } |
---|
515 | /** |
---|
516 | <doc:function date="2001-06-12"> |
---|
517 | <doc:name>hourInDay</doc:name> |
---|
518 | <doc:version>2.0</doc:version> |
---|
519 | <doc:return type="number" /> |
---|
520 | <doc:args> |
---|
521 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
522 | </doc:args> |
---|
523 | </doc:function> |
---|
524 | **/ |
---|
525 | function hourInDay(){// |
---|
526 | if (arguments.length > 0){ |
---|
527 | var oDate; |
---|
528 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
529 | (oDate = _validDate(arguments[0], "xs:time")) |
---|
530 | ) |
---|
531 | return oDate.getHours(); |
---|
532 | else |
---|
533 | return Number.NaN; |
---|
534 | }else |
---|
535 | return new Date().getHours(); |
---|
536 | } |
---|
537 | /** |
---|
538 | <doc:function date="2001-06-12"> |
---|
539 | <doc:name>leapYear</doc:name> |
---|
540 | <doc:version>2.0</doc:version> |
---|
541 | <doc:return type="boolean" /> |
---|
542 | <doc:args> |
---|
543 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
544 | </doc:args> |
---|
545 | </doc:function> |
---|
546 | **/ |
---|
547 | function leapYear(){ |
---|
548 | if (arguments.length > 0 && arguments[0] != ''){ |
---|
549 | var oDate; |
---|
550 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
551 | (oDate = _validDate(arguments[0], "xs:date")) || |
---|
552 | (oDate = _validDate(arguments[0], "xs:gYearMonth")) || |
---|
553 | (oDate = _validDate(arguments[0], "xs:gYear")) |
---|
554 | ) |
---|
555 | return (_isLeap(oDate.getFullYear())?true:false); |
---|
556 | else |
---|
557 | return Number.NaN; |
---|
558 | }else |
---|
559 | return (_isLeap(new Date().getFullYear())?true:false); |
---|
560 | } |
---|
561 | /** |
---|
562 | <doc:function date="2001-06-12"> |
---|
563 | <doc:name>minuteInHour</doc:name> |
---|
564 | <doc:version>2.0</doc:version> |
---|
565 | <doc:return type="number" /> |
---|
566 | <doc:args> |
---|
567 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
568 | </doc:args> |
---|
569 | </doc:function> |
---|
570 | **/ |
---|
571 | function minuteInHour(){// |
---|
572 | if (arguments.length > 0){ |
---|
573 | var oDate; |
---|
574 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
575 | (oDate = _validDate(arguments[0], "xs:time")) |
---|
576 | ) |
---|
577 | return oDate.getMinutes(); |
---|
578 | else |
---|
579 | return Number.NaN; |
---|
580 | }else |
---|
581 | return new Date().getMinutes(); |
---|
582 | } |
---|
583 | /** |
---|
584 | <doc:function date="2001-06-12"> |
---|
585 | <doc:name>monthAbbreviation</doc:name> |
---|
586 | <doc:version>2.0</doc:version> |
---|
587 | <doc:return type="string" /> |
---|
588 | <doc:args> |
---|
589 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
590 | <doc:arg name="date-format" type="string" default="''" optional="yes"></doc:arg> |
---|
591 | <doc:arg name="context" type="context" default="null" optional="yes"></doc:arg> |
---|
592 | </doc:args> |
---|
593 | </doc:function> |
---|
594 | **/ |
---|
595 | function monthAbbreviation(){// |
---|
596 | if (arguments.length > 0){ |
---|
597 | var oDate; |
---|
598 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
599 | (oDate = _validDate(arguments[0], "xs:date")) || |
---|
600 | (oDate = _validDate(arguments[0], "xs:gYearMonth")) || |
---|
601 | (oDate = _validDate(arguments[0], "xs:gMonthDay")) |
---|
602 | ) |
---|
603 | return _monthAbbreviation(oDate.getMonth(), arguments[1], arguments[2]); |
---|
604 | else |
---|
605 | return ''; |
---|
606 | }else |
---|
607 | return _monthAbbreviation(new Date().getMonth(), arguments[1], arguments[2]); |
---|
608 | } |
---|
609 | function _monthAbbreviation(num, dfName, ctx){// |
---|
610 | if (ctx != null){ |
---|
611 | var nodes = ctx.nextNode.selectNodes( |
---|
612 | "//*[local-name()='date-format' and namespace-uri()='http://exslt.org/dates-and-times' and @name='" + dfName +"' ]" + |
---|
613 | "/*[local-name()='months' and namespace-uri()='http://exslt.org/dates-and-times']" + |
---|
614 | "/*[local-name()='month' and namespace-uri()='http://exslt.org/dates-and-times']/@abbr"); |
---|
615 | if (nodes != null) |
---|
616 | return nodes[num].nodeValue; |
---|
617 | else |
---|
618 | return ''; |
---|
619 | }else{ //we could try local |
---|
620 | return gaLang[gsLang]["shortMonths"][num*1]; |
---|
621 | } |
---|
622 | } |
---|
623 | /** |
---|
624 | <doc:function date="2001-06-12"> |
---|
625 | <doc:name>monthInYear</doc:name> |
---|
626 | <doc:version>2.0</doc:version> |
---|
627 | <doc:return type="number" /> |
---|
628 | <doc:args> |
---|
629 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
630 | </doc:args> |
---|
631 | </doc:function> |
---|
632 | **/ |
---|
633 | function monthInYear(){// |
---|
634 | if (arguments.length > 0){ |
---|
635 | var oDate; |
---|
636 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
637 | (oDate = _validDate(arguments[0], "xs:date")) || |
---|
638 | (oDate = _validDate(arguments[0], "xs:gYearMonth")) || |
---|
639 | (oDate = _validDate(arguments[0], "xs:gMonthDay")) || |
---|
640 | (oDate = _validDate(arguments[0], "xs:gYear")) |
---|
641 | ) |
---|
642 | return oDate.getMonth()+1; |
---|
643 | else |
---|
644 | return Number.NaN; |
---|
645 | }else |
---|
646 | return new Date().getMonth()+1; |
---|
647 | } |
---|
648 | /** |
---|
649 | <doc:function date="2001-06-12"> |
---|
650 | <doc:name>monthName</doc:name> |
---|
651 | <doc:version>2.0</doc:version> |
---|
652 | <doc:return type="string" /> |
---|
653 | <doc:args> |
---|
654 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
655 | <doc:arg name="date-format" type="string" default="''" optional="yes"></doc:arg> |
---|
656 | <doc:arg name="context" type="context" default="null" optional="yes"></doc:arg> |
---|
657 | </doc:args> |
---|
658 | </doc:function> |
---|
659 | **/ |
---|
660 | function monthName(){// |
---|
661 | if (arguments.length > 0){// && arguments[0] != '' |
---|
662 | var oDate; |
---|
663 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
664 | (oDate = _validDate(arguments[0], "xs:date")) || |
---|
665 | (oDate = _validDate(arguments[0], "xs:gYearMonth")) || |
---|
666 | (oDate = _validDate(arguments[0], "xs:gMonthDay")) |
---|
667 | ) |
---|
668 | return _monthName(oDate.getMonth(), arguments[1], arguments[2]); |
---|
669 | else |
---|
670 | return ''; |
---|
671 | }else |
---|
672 | return _monthName(new Date().getMonth(), arguments[1], arguments[2]); |
---|
673 | } |
---|
674 | function _monthName(num, dfName, ctx){// |
---|
675 | if (ctx != null){ |
---|
676 | var nodes = ctx.nextNode.selectNodes( |
---|
677 | "//*[local-name()='date-format' and namespace-uri()='http://exslt.org/dates-and-times' and @name='" + dfName +"' ]" + |
---|
678 | "/*[local-name()='months' and namespace-uri()='http://exslt.org/dates-and-times']" + |
---|
679 | "/*[local-name()='month' and namespace-uri()='http://exslt.org/dates-and-times']/text()"); |
---|
680 | if (nodes != null) |
---|
681 | return nodes[num].nodeValue; |
---|
682 | else |
---|
683 | return ''; |
---|
684 | }else |
---|
685 | return gaLang[gsLang]["months"][num*1]; |
---|
686 | } |
---|
687 | /** |
---|
688 | <doc:function date="2001-06-12"> |
---|
689 | <doc:name>parseDate</doc:name> |
---|
690 | <doc:version>1.0</doc:version> |
---|
691 | <doc:return type="string" /> |
---|
692 | <doc:args> |
---|
693 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
694 | <doc:arg name="input-format" type="string" default="''" optional="no"></doc:arg> |
---|
695 | <doc:arg name="date-format" type="string" default="''" optional="yes"></doc:arg> |
---|
696 | <doc:arg name="context" type="document" default="null" optional="yes"></doc:arg> |
---|
697 | </doc:args> |
---|
698 | </doc:function> |
---|
699 | **/ |
---|
700 | function parseDate(date, inFormat, dfName, ctx){//0001 |
---|
701 | var oDate; |
---|
702 | if (inFormat == ""){ |
---|
703 | if ((oDate = _validDate(date, "xs:dateTime")) || |
---|
704 | (oDate = _validDate(date, "xs:date")) || |
---|
705 | (oDate = _validDate(date, "xs:time")) || |
---|
706 | (oDate = _validDate(date, "xs:gYearMonth")) || |
---|
707 | (oDate = _validDate(date, "xs:gYear")) || |
---|
708 | (oDate = _validDate(date, "xs:gMonthDay")) || |
---|
709 | (oDate = _validDate(date, "xs:gMonth")) || |
---|
710 | (oDate = _validDate(date, "xs:gDay")) |
---|
711 | ){ |
---|
712 | return _formatDate(oDate, "xs:dateTime"); |
---|
713 | } |
---|
714 | }else{ |
---|
715 | if (oDate = _validDate(date, inFormat)) |
---|
716 | return _formatDate(oDate, dfName); |
---|
717 | else |
---|
718 | return ''; |
---|
719 | } |
---|
720 | } |
---|
721 | /** |
---|
722 | <doc:function date="2001-06-12"> |
---|
723 | <doc:name>secondInMinute</doc:name> |
---|
724 | <doc:version>2.0</doc:version> |
---|
725 | <doc:return type="number" /> |
---|
726 | <doc:args> |
---|
727 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
728 | </doc:args> |
---|
729 | </doc:function> |
---|
730 | **/ |
---|
731 | function secondInMinute(){// |
---|
732 | if (arguments.length > 0){ |
---|
733 | var oDate; |
---|
734 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
735 | (oDate = _validDate(arguments[0], "xs:time")) |
---|
736 | ) |
---|
737 | return oDate.getSeconds(); |
---|
738 | else |
---|
739 | return Number.NaN; |
---|
740 | }else |
---|
741 | return new Date().getSeconds(); |
---|
742 | } |
---|
743 | /** |
---|
744 | <doc:function date="2001-06-12"> |
---|
745 | <doc:name>seconds</doc:name> |
---|
746 | <doc:version>2.0</doc:version> |
---|
747 | <doc:return type="number" /> |
---|
748 | <doc:args> |
---|
749 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
750 | </doc:args> |
---|
751 | </doc:function> |
---|
752 | **/ |
---|
753 | function seconds(){// |
---|
754 | if (arguments.length > 0){ |
---|
755 | var oDate; |
---|
756 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
757 | (oDate = _validDate(arguments[0], "xs:date")) || |
---|
758 | (oDate = _validDate(arguments[0], "xs:gYearMonth")) || |
---|
759 | (oDate = _validDate(arguments[0], "xs:gYear")) |
---|
760 | ) |
---|
761 | return oDate.valueOf()/1000; |
---|
762 | else |
---|
763 | if (oDate = _validDuration(arguments[0])) |
---|
764 | return oDate.Seconds(); |
---|
765 | else |
---|
766 | return Number.NaN; |
---|
767 | }else |
---|
768 | return Math.floor(new Date().valueOf()/1000);//current local date/time |
---|
769 | } |
---|
770 | /** |
---|
771 | <doc:function date="2001-06-12"> |
---|
772 | <doc:name>sum</doc:name> |
---|
773 | <doc:version>2.0</doc:version> |
---|
774 | <doc:return type="string" /> |
---|
775 | <doc:args> |
---|
776 | <doc:arg name="durations" type="node-set" default="''" optional="no"></doc:arg> |
---|
777 | </doc:args> |
---|
778 | </doc:function> |
---|
779 | **/ |
---|
780 | function sum(ctx){ |
---|
781 | var sDate = "P0S"; |
---|
782 | if (typeof(ctx) == "object"){ |
---|
783 | if (ctx.length){ |
---|
784 | for (var i=0; i < ctx.length; i++){ |
---|
785 | ctxN = ctx.item(i); |
---|
786 | if (ctxN.nodeType == 1){ |
---|
787 | if (!(sDate = addDuration(sDate, _wander(ctxN)))) |
---|
788 | return ''; |
---|
789 | } |
---|
790 | if (ctxN.nodeType == 2){ |
---|
791 | if (!(sDate = addDuration(sDate, ctxN.nodeValue))) |
---|
792 | return ''; |
---|
793 | } |
---|
794 | } |
---|
795 | }else{ |
---|
796 | if (!(sDate = addDuration(sDate, ctx.nodeValue))) |
---|
797 | return ''; |
---|
798 | } |
---|
799 | }else{ |
---|
800 | return ''; |
---|
801 | } |
---|
802 | return sDate; |
---|
803 | } |
---|
804 | function _wander(ctx){ |
---|
805 | var sDate = "P0S"; |
---|
806 | for (var i=0; i < ctx.childNodes.length; i++){ |
---|
807 | ctxN = ctx.childNodes[i]; |
---|
808 | switch(ctxN.nodeType){ |
---|
809 | case 1: |
---|
810 | if (!(sDate = addDuration(sDate, _wander(ctxN)))) |
---|
811 | return ''; |
---|
812 | break; |
---|
813 | case 3: |
---|
814 | if (!(sDate = addDuration(sDate, ctxN.nodeValue))) |
---|
815 | return ''; |
---|
816 | break; |
---|
817 | default: |
---|
818 | break; |
---|
819 | } |
---|
820 | } |
---|
821 | return sDate; |
---|
822 | } |
---|
823 | /** |
---|
824 | <doc:function date="2001-06-12"> |
---|
825 | <doc:name>time</doc:name> |
---|
826 | <doc:version>2.0</doc:version> |
---|
827 | <doc:return type="string" /> |
---|
828 | <doc:args> |
---|
829 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
830 | </doc:args> |
---|
831 | </doc:function> |
---|
832 | **/ |
---|
833 | function time(){// |
---|
834 | if (arguments.length > 0){ |
---|
835 | var oDate; |
---|
836 | if ((oDate = _validDate(arguments[0], "xs:dateTime"))|| |
---|
837 | (oDate = _validDate(arguments[0], "xs:time")) |
---|
838 | ) |
---|
839 | return _formatDate(oDate, "xs:time"); |
---|
840 | else |
---|
841 | return ''; |
---|
842 | }else |
---|
843 | return _formatDate(new Date(), "xs:time"); |
---|
844 | } |
---|
845 | /** |
---|
846 | <doc:function date="2001-06-12"> |
---|
847 | <doc:name>weekInMonth</doc:name> |
---|
848 | <doc:version>2.0</doc:version> |
---|
849 | <doc:return type="number" /> |
---|
850 | <doc:args> |
---|
851 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
852 | <doc:arg name="date-format" type="string" default="''" optional="yes"></doc:arg> |
---|
853 | <doc:arg name="context" type="context" default="null" optional="yes"></doc:arg> |
---|
854 | </doc:args> |
---|
855 | </doc:function> |
---|
856 | **/ |
---|
857 | function weekInMonth(){ |
---|
858 | if (arguments.length > 0){ |
---|
859 | var oDate; |
---|
860 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
861 | (oDate = _validDate(arguments[0], "xs:date")) |
---|
862 | ) |
---|
863 | return _weekInMonth(oDate, arguments[1], arguments[2]); |
---|
864 | else |
---|
865 | return Number.NaN; |
---|
866 | }else |
---|
867 | return _weekInMonth(new Date(), arguments[1], arguments[2]); |
---|
868 | } |
---|
869 | function _weekInMonth(oDate, dfName, ctx){ |
---|
870 | var fdow = 0; |
---|
871 | if (ctx != null){ |
---|
872 | var nodes = ctx.nextNode.selectSingleNode( |
---|
873 | "//*[local-name()='date-format' and namespace-uri()='http://exslt.org/dates-and-times' and @name='" + dfName +"' ]" + |
---|
874 | "/@first-day-of-week"); |
---|
875 | if (nodes != null) |
---|
876 | fdow = gaDayNames[nodes.nodeValue]; |
---|
877 | } |
---|
878 | var f = new Date(oDate); |
---|
879 | f.setDate(1) |
---|
880 | var fd = f.getDay()-1; |
---|
881 | var dt = oDate.getDate() + fd - fdow; |
---|
882 | return Math.floor((dt)/7)+1; |
---|
883 | } |
---|
884 | /** |
---|
885 | <doc:function date="2001-06-12"> |
---|
886 | <doc:name>weekInYear</doc:name> |
---|
887 | <doc:version>2.0</doc:version> |
---|
888 | <doc:return type="number" /> |
---|
889 | <doc:args> |
---|
890 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
891 | <doc:arg name="date-format" type="string" default="''" optional="yes"></doc:arg> |
---|
892 | <doc:arg name="context" type="context" default="null" optional="yes"></doc:arg> |
---|
893 | </doc:args> |
---|
894 | </doc:function> |
---|
895 | **/ |
---|
896 | function weekInYear(){ |
---|
897 | if (arguments.length > 0){ |
---|
898 | var oDate; |
---|
899 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
900 | (oDate = _validDate(arguments[0], "xs:date")) |
---|
901 | ) |
---|
902 | return _weekInYear(oDate, arguments[1], arguments[2]); |
---|
903 | }else |
---|
904 | return _weekInYear(new Date()); |
---|
905 | return ''; |
---|
906 | } |
---|
907 | function _weekInYear(oDate, dfName, ctx){ |
---|
908 | var fdow = 0; |
---|
909 | if (ctx != null){ |
---|
910 | var nodes = ctx.nextNode.selectSingleNode( |
---|
911 | "//*[local-name()='date-format' and namespace-uri()='http://exslt.org/dates-and-times' and @name='" + dfName +"' ]" + |
---|
912 | "/@first-day-of-week"); |
---|
913 | if (nodes != null) |
---|
914 | fdow = gaDayNames[nodes.nodeValue]; |
---|
915 | return Math.ceil(_dayInYear(oDate.getFullYear(), oDate.getMonth(), oDate.getDate()+(7-fdow)) / 7); |
---|
916 | }else |
---|
917 | return Math.ceil(_dayInYear(oDate.getFullYear(), oDate.getMonth(), oDate.getDate()) / 7); |
---|
918 | } |
---|
919 | /** |
---|
920 | <doc:function date="2001-06-12"> |
---|
921 | <doc:name>year</doc:name> |
---|
922 | <doc:version>2.0</doc:version> |
---|
923 | <doc:return type="number" /> |
---|
924 | <doc:args> |
---|
925 | <doc:arg name="date" type="string" default="''" optional="yes"></doc:arg> |
---|
926 | </doc:args> |
---|
927 | </doc:function> |
---|
928 | **/ |
---|
929 | function year(){// |
---|
930 | if (arguments.length > 0){ |
---|
931 | var oDate; |
---|
932 | if ((oDate = _validDate(arguments[0], "xs:dateTime")) || |
---|
933 | (oDate = _validDate(arguments[0], "xs:date")) || |
---|
934 | (oDate = _validDate(arguments[0], "xs:gYearMonth")) || |
---|
935 | (oDate = _validDate(arguments[0], "xs:gYear")) |
---|
936 | ) |
---|
937 | return oDate.getFullYear(); |
---|
938 | else |
---|
939 | return Number.NaN; |
---|
940 | }else |
---|
941 | return new Date().getFullYear(); |
---|
942 | } |
---|
943 | /** |
---|
944 | Private functions. |
---|
945 | **/ |
---|
946 | function _validDate(date, format){ |
---|
947 | var parts; |
---|
948 | var oDate = new Date(-62135596800000); // Mon Jan 1 00:00:00 UTC 1 |
---|
949 | switch(format){ |
---|
950 | case "xs:dateTime": //(CCYY-MM-DDThh:mm:ss) |
---|
951 | var re = new RegExp("^([0-9]{4})-([0-9]{2})-([0-9]{2})(T([0-9]{2})(:([0-9]{2})(:([0-9]{2}))?)?)?(Z)?([\+\-][0-9]{2}:[0-9]{2})?$"); |
---|
952 | if (parts=re.exec(date)){ |
---|
953 | if (parts[10] == "Z"){ |
---|
954 | oDate.setFullYear(parts[1], parts[2]-1, parts[3]); |
---|
955 | oDate.setHours(parts[5], parts[7], parts[9]); |
---|
956 | oDate.setMinutes(oDate.getMinutes()-oDate.getTimezoneOffset()); |
---|
957 | gsSuppliedFormat = "xs:dateTime"; |
---|
958 | return oDate; |
---|
959 | }else{ |
---|
960 | oDate.setFullYear(parts[1], parts[2]-1, parts[3]); |
---|
961 | oDate.setHours(parts[5], parts[7], parts[9]); |
---|
962 | gsSuppliedFormat = "xs:dateTime"; |
---|
963 | return oDate; |
---|
964 | } |
---|
965 | } |
---|
966 | break; |
---|
967 | case "xs:date": //(CCYY-MM-DD) |
---|
968 | var re = new RegExp("^([0-9]{4})-([0-9]{2})-([0-9]{2})$"); |
---|
969 | if (parts=re.exec(date)){ |
---|
970 | oDate.setFullYear(parts[1], parts[2]-1, parts[3]); |
---|
971 | gsSuppliedFormat = "xs:date"; |
---|
972 | return oDate; |
---|
973 | } |
---|
974 | break; |
---|
975 | case "xs:time": //(hh:mm:ss) |
---|
976 | var re = new RegExp("^([0-9]{2})(:([0-9]{2})(:([0-9]{2}))?)?(Z)?$"); |
---|
977 | if (parts=re.exec(date)){ |
---|
978 | if (parts[9] == "Z"){ |
---|
979 | oDate.setHours(parts[1], parts[3], parts[5]); |
---|
980 | oDate.setMinutes(oDate.getMinutes()-oDate.getTimezoneOffset()); |
---|
981 | gsSuppliedFormat = "xs:time"; |
---|
982 | return oDate; |
---|
983 | }else{ |
---|
984 | oDate.setHours(parts[1], parts[3], parts[5]); |
---|
985 | gsSuppliedFormat = "xs:time"; |
---|
986 | return oDate; |
---|
987 | } |
---|
988 | } |
---|
989 | break; |
---|
990 | case "xs:gYearMonth": //(CCYY-MM) |
---|
991 | var re = new RegExp("^([0-9]{4})-([0-9]{2})$"); |
---|
992 | if (parts=re.exec(date)){ |
---|
993 | oDate.setFullYear(parts[1], parts[2]-1); |
---|
994 | gsSuppliedFormat = "xs:gYearMonth"; |
---|
995 | return oDate; |
---|
996 | } |
---|
997 | break; |
---|
998 | case "xs:gYear": //(CCYY) |
---|
999 | var re = new RegExp("^([0-9]{4})$"); |
---|
1000 | if (parts=re.exec(date)){ |
---|
1001 | oDate.setFullYear(parts[1]); |
---|
1002 | gsSuppliedFormat = "xs:gYear"; |
---|
1003 | return oDate; |
---|
1004 | } |
---|
1005 | break; |
---|
1006 | case "xs:gMonthDay": //(^--MM-DD) |
---|
1007 | var re = new RegExp("--([0-9]{2})-([0-9]{2})"); |
---|
1008 | if (parts=re.exec(date)){ |
---|
1009 | oDate.setMonth(parts[1]-1, parts[2]); |
---|
1010 | gsSuppliedFormat = "xs:gMonthDay"; |
---|
1011 | return oDate; |
---|
1012 | } |
---|
1013 | break; |
---|
1014 | case "xs:gMonth": //(--MM--) |
---|
1015 | var re = new RegExp("^--([0-9]{2})--$"); |
---|
1016 | if (parts=re.exec(date)){ |
---|
1017 | oDate.setMonth(parts[1]-1); |
---|
1018 | gsSuppliedFormat = "xs:gMonth"; |
---|
1019 | return oDate; |
---|
1020 | } |
---|
1021 | break; |
---|
1022 | case "xs:gDay": //(---DD) |
---|
1023 | var re = new RegExp("^---([0-9]{2})$"); |
---|
1024 | if (parts=re.exec(date)){ |
---|
1025 | oDate.setDate(parts[1]); |
---|
1026 | gsSuppliedFormat = "xs:gDay"; |
---|
1027 | return oDate; |
---|
1028 | } |
---|
1029 | break; |
---|
1030 | default: |
---|
1031 | // format string |
---|
1032 | var Z = ""; |
---|
1033 | for (var i=0,j=0; i < format.length & oDate.toString() != "NaN";){ |
---|
1034 | var s = j; |
---|
1035 | switch(format.substr(i, 1)){ |
---|
1036 | case "G":// era designator (Text) AD |
---|
1037 | while (format.substr(i, 1)=="G"){i++;j++;} |
---|
1038 | j++; |
---|
1039 | if (date.substr(s,j-s) == "BC" || date.substr(s,j-s) == "bc"){ |
---|
1040 | oDate.setFullYear(-oDate.getFullYear); |
---|
1041 | } |
---|
1042 | break; |
---|
1043 | case "y":// year (Number) 1996 |
---|
1044 | while (format.substr(i, 1)=="y"){i++;j++;} |
---|
1045 | oDate.setYear(date.substr(s,j-s)); |
---|
1046 | break; |
---|
1047 | case "M":// month in year (Text & Number) July & 07 |
---|
1048 | while (format.substr(i, 1)=="M"){i++;j++;} |
---|
1049 | if (i-s <= 2){ |
---|
1050 | oDate.setMonth(date.substr(s,j-s)-1); |
---|
1051 | }else{ |
---|
1052 | oDate.setMonth(_monthNumber(date.substr(s,j-s))); |
---|
1053 | } |
---|
1054 | break; |
---|
1055 | case "d":// day in month (Number) 10 |
---|
1056 | while (format.substr(i, 1)=="d"){i++;j++;} |
---|
1057 | oDate.setDate(date.substr(s,j-s)); |
---|
1058 | break; |
---|
1059 | case "h":// hour in am/pm (1~12) (Number) 12 |
---|
1060 | while (format.substr(i, 1)=="h"){i++;j++;} |
---|
1061 | oDate.setHours(date.substr(s,j-s)); |
---|
1062 | break; |
---|
1063 | case "H":// hour in day (0~23) (Number) 0 |
---|
1064 | while (format.substr(i, 1)=="H"){i++;j++;} |
---|
1065 | oDate.setHours(date.substr(s,j-s)); |
---|
1066 | break; |
---|
1067 | case "m":// minute in hour (Number) 30 |
---|
1068 | while (format.substr(i, 1)=="m"){i++;j++;} |
---|
1069 | oDate.setMinutes(date.substr(s,j-s)); |
---|
1070 | break; |
---|
1071 | case "s":// second in minute (Number) 55 |
---|
1072 | while (format.substr(i, 1)=="s"){i++;j++;} |
---|
1073 | oDate.setSeconds(date.substr(s,j-s)); |
---|
1074 | break; |
---|
1075 | case "S":// millisecond (Number) 978 |
---|
1076 | while (format.substr(i, 1)=="S"){i++;j++;} |
---|
1077 | oDate.setMilliseconds(date.substr(s,j-s)); |
---|
1078 | break; |
---|
1079 | case "E":// day in week (Text) Tuesday |
---|
1080 | while (format.substr(i, 1)=="E"){i++;j++;} |
---|
1081 | while (format.substr(i+1, 1)!=date.substr(j, 1)){j++;} |
---|
1082 | break; |
---|
1083 | case "D":// day in year (Number) 189 |
---|
1084 | while (format.substr(i, 1)=="D"){i++;j++;} |
---|
1085 | while (format.substr(i+1, 1)!=date.substr(j, 1)){j++;} |
---|
1086 | break; |
---|
1087 | case "F":// day of week in month (Number) 2 (2nd Wed in July) |
---|
1088 | while (format.substr(i, 1)=="F"){i++;j++;} |
---|
1089 | break; |
---|
1090 | case "w":// week in year (Number) 27 |
---|
1091 | while (format.substr(i, 1)=="w"){i++;j++;} |
---|
1092 | while (format.substr(i+1, 1)!=date.substr(j, 1)){j++;} |
---|
1093 | break; |
---|
1094 | case "W":// week in month (Number) 2 |
---|
1095 | while (format.substr(i, 1)=="W"){i++;j++;} |
---|
1096 | while (format.substr(i+1, 1)!=date.substr(j, 1)){j++;} |
---|
1097 | break; |
---|
1098 | case "a":// am/pm marker (Text) PM |
---|
1099 | while (format.substr(i, 1)=="a"){i++;j++;} |
---|
1100 | j++; |
---|
1101 | if (date.substr(s,j-s) == "PM" || date.substr(s,j-s) == "pm") |
---|
1102 | oDate.setHours(oDate.getHours()+12); |
---|
1103 | break; |
---|
1104 | case "k":// hour in day (1~24) (Number) 24 |
---|
1105 | while (format.substr(i, 1)=="k"){i++;j++;} |
---|
1106 | oDate.setHours(date.substr(s,j-s)); |
---|
1107 | break; |
---|
1108 | case "K":// hour in am/pm (0~11) (Number) 0 |
---|
1109 | while (format.substr(i, 1)=="K"){i++;j++;} |
---|
1110 | oDate.setHours(date.substr(s,j-s)); |
---|
1111 | break; |
---|
1112 | case "z":// time zone (Text) Pacific Standard Time |
---|
1113 | while (format.substr(i, 1)=="z"){i++;j++;} |
---|
1114 | while (format.substr(i+1, 1)!=date.substr(j, 1)){j++;} |
---|
1115 | //oDate.setMinutes(oDate.getMinutes() - oDate.getTimezoneOffset()); |
---|
1116 | Z = date.substr(s,j-s); |
---|
1117 | break; |
---|
1118 | case "'":// escape for text (Delimiter) |
---|
1119 | if (format.substr(i+1, 1) == "'"){ |
---|
1120 | i+=2; |
---|
1121 | }else{ |
---|
1122 | i++; |
---|
1123 | while (format.substr(i, 1)!="'"){i++;j++;} |
---|
1124 | i++; |
---|
1125 | } |
---|
1126 | break; |
---|
1127 | default: |
---|
1128 | if (format.substr(i, 1) != date.substr(j, 1)){ |
---|
1129 | return false; |
---|
1130 | } |
---|
1131 | i++;j++; |
---|
1132 | break; |
---|
1133 | } |
---|
1134 | } |
---|
1135 | if (j < date.length) |
---|
1136 | return false; |
---|
1137 | if (oDate.toString() != "NaN"){ |
---|
1138 | if (Z != ""){ |
---|
1139 | var re = new RegExp("([\+\-][0-9]{2})?:([0-9]{2})?"); |
---|
1140 | if (parts=re.exec(Z)){ |
---|
1141 | // check GMT/BST |
---|
1142 | var tzo = parts[1]*60 + parts[2]; |
---|
1143 | //if (tzo != new Date().getTimezoneOffset()){ |
---|
1144 | // oDate.setMinutes(oDate.getMinutes() - (oDate.getTimezoneOffset() - tzo)) |
---|
1145 | //} |
---|
1146 | } |
---|
1147 | } |
---|
1148 | gsSuppliedFormat = "custom"; |
---|
1149 | return oDate; |
---|
1150 | } |
---|
1151 | break; |
---|
1152 | } |
---|
1153 | return false; |
---|
1154 | } |
---|
1155 | function _formatDate(oDate, format){ |
---|
1156 | if (oDate == Number.NaN)return Number.NaN; |
---|
1157 | switch(format){ |
---|
1158 | case "xs:dateTime": //(CCYY-MM-DDThh:mm:ss) |
---|
1159 | var tzo = oDate.getTimezoneOffset(); |
---|
1160 | return oDate.getFullYear() + "-" + pad(oDate.getMonth()+1) + "-" + pad(oDate.getDate()) + "T" + |
---|
1161 | pad(oDate.getHours()) + ":" + pad(oDate.getMinutes()) + ":" + pad(oDate.getSeconds()) + (tzo < 0?"-":"+") + pad(Math.abs(tzo/60)) + ":" + pad(tzo % 60); |
---|
1162 | break; |
---|
1163 | case "xs:date": //(CCYY-MM-DD) |
---|
1164 | return oDate.getFullYear() + "-" + pad(oDate.getMonth()+1) + "-" + pad(oDate.getDate()); |
---|
1165 | break; |
---|
1166 | case "xs:time": //(hh:mm:ss) |
---|
1167 | var tzo = oDate.getTimezoneOffset(); |
---|
1168 | return pad(oDate.getHours()) + ":" + pad(oDate.getMinutes()) + ":" + pad(oDate.getSeconds()) + (tzo < 0?"-":"+") + pad(Math.abs(tzo/60)) + ":" + pad(tzo % 60); |
---|
1169 | break; |
---|
1170 | case "xs:gYearMonth": //(CCYY-MM) |
---|
1171 | return oDate.getFullYear() + "-" + pad(oDate.getMonth()+1); |
---|
1172 | break; |
---|
1173 | case "xs:gYear": //(CCYY) |
---|
1174 | return oDate.getFullYear(); |
---|
1175 | break; |
---|
1176 | case "xs:gMonthDay": //(--MM-DD) |
---|
1177 | return pad(oDate.getMonth() + 1) + "-" + pad(oDate.getDate()); |
---|
1178 | break; |
---|
1179 | case "xs:gMonth": //(--MM--) |
---|
1180 | return pad(oDate.getMonth()+1); |
---|
1181 | break; |
---|
1182 | case "xs:gDay": //(---DD) |
---|
1183 | return pad(oDate.getDate()); |
---|
1184 | break; |
---|
1185 | } |
---|
1186 | return false; |
---|
1187 | } |
---|
1188 | function pad(v){ |
---|
1189 | return (v<10?"0"+v:v); |
---|
1190 | } |
---|
1191 | |
---|
1192 | function _validDuration(d){ |
---|
1193 | var parts; |
---|
1194 | var re = new RegExp("^([\-])?P(([0-9]+)Y)?(([0-9]+)M)?(([0-9]+)D)?((T)?(([0-9]+)H)?(([0-9]+)M)?((([0-9]+)(\.([0-9]+))?)S)?)?$"); |
---|
1195 | if (parts = re.exec(d)){ |
---|
1196 | if ((/T$/.exec(d)) || (parts[9] == "T" && parts[11]+parts[13]+parts[16]+parts[18]== ""))// ~(:+(|) duh!!! |
---|
1197 | return Number.NaN; |
---|
1198 | return new Duration(parts[1], parts[3], parts[5], parts[7], parts[11], parts[13], parts[16], parts[18]); |
---|
1199 | } |
---|
1200 | return Number.NaN; |
---|
1201 | } |
---|
1202 | |
---|
1203 | function Duration(pm, years, months, days, hours, minutes, seconds, fraction){ |
---|
1204 | this.pm = pm; |
---|
1205 | this.years = years*1; |
---|
1206 | this.months = months*1; |
---|
1207 | this.days = days*1; |
---|
1208 | this.hours = hours*1; |
---|
1209 | this.minutes = minutes*1; |
---|
1210 | this.seconds = seconds*1; |
---|
1211 | this.fraction = fraction*1; |
---|
1212 | this.milliseconds = eval('0.'+fraction) * 100; |
---|
1213 | this.mask = ((this.years?1:0)<<6) + ((this.months?1:0)<<5) + ((this.days?1:0)<<4) + ((this.hours?1:0)<<3) + ((this.minutes?1:0)<<2) + ((this.seconds?1:0)<<1) + (this.milliseconds?1:0); |
---|
1214 | this.addDate = function(d){d.setFullYear(eval(this.pm+this.years)+d.getFullYear(),eval(this.pm+this.months)+d.getMonth(),eval(this.pm+this.days)+d.getDate());d.setHours(eval(this.pm+this.hours)+d.getHours(),eval(this.pm+this.minutes)+d.getMinutes(),eval(this.pm+this.seconds)+d.getSeconds());d.setMilliseconds(eval(this.pm+this.milliseconds));return d;} |
---|
1215 | this.Seconds = function(){if (this.years || this.months)return Number.NaN;return this.days*giDayMill + this.hours*giHourMill + this.minutes*giMinMill + this.seconds*giSecMill;} |
---|
1216 | } |
---|
1217 | var giYearMill = 31536000000; |
---|
1218 | var giMonthMill = 2592000000; |
---|
1219 | var giDayMill = 86400000; |
---|
1220 | var giHourMill = 3600000; |
---|
1221 | var giMinMill = 60000; |
---|
1222 | var giSecMill = 1000; |
---|
1223 | function _formatDuration(du){ |
---|
1224 | if (typeof(du) == "object"){ |
---|
1225 | var pm = du.pm; |
---|
1226 | var y = du.years; |
---|
1227 | var m = du.months; |
---|
1228 | var d = du.days; |
---|
1229 | var h = du.hours; |
---|
1230 | var n = du.minutes; |
---|
1231 | var s = du.seconds + du.milliseconds/1000; |
---|
1232 | |
---|
1233 | if (y+m+d+h+n+s == 0) |
---|
1234 | return 'P0S'; |
---|
1235 | return pm + "P" + |
---|
1236 | (y!=0?y + "Y":'') + |
---|
1237 | (m!=0?m + "M":'') + |
---|
1238 | (d!=0?d + "D":'') + |
---|
1239 | (h+n+s!=0?"T":'') + |
---|
1240 | (h!=0?h + "H":'') + |
---|
1241 | (n!=0?n + "M":'') + |
---|
1242 | (s!=0?s + "S":''); |
---|
1243 | }else{ |
---|
1244 | var pm = (du<0?'-':''); |
---|
1245 | du = Math.abs(du); |
---|
1246 | var d = Math.floor((du) / giDayMill); |
---|
1247 | var h = Math.floor((du % giDayMill) / giHourMill); |
---|
1248 | var n = Math.floor((du % giHourMill) / giMinMill); |
---|
1249 | var s = Math.floor((du % giMinMill) / giSecMill) + (du % giSecMill)/1000; |
---|
1250 | return pm + "P" + |
---|
1251 | (d!=0?d + "D":'') + |
---|
1252 | (h+n+s!=0?"T":'') + |
---|
1253 | (h!=0?h + "H":'') + |
---|
1254 | (n!=0?n + "M":'') + |
---|
1255 | (s!=0?s + "S":''); |
---|
1256 | } |
---|
1257 | } |
---|