1 | /**
|
---|
2 | * Version: 1.0 Alpha-1
|
---|
3 | * Build Date: 12-Nov-2007
|
---|
4 | * Copyright (c) 2006-2007, Coolite Inc. (http://www.coolite.com/). All rights reserved.
|
---|
5 | * License: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/.
|
---|
6 | * Website: http://www.datejs.com/ or http://www.coolite.com/datejs/
|
---|
7 | */
|
---|
8 |
|
---|
9 | (function () {
|
---|
10 | Date.Parsing = {
|
---|
11 | Exception: function (s) {
|
---|
12 | this.message = "Parse error at '" + s.substring(0, 10) + " ...'";
|
---|
13 | }
|
---|
14 | };
|
---|
15 |
|
---|
16 | var $P = Date.Parsing;
|
---|
17 | var _ = $P.Operators = {
|
---|
18 | //
|
---|
19 | // Tokenizers
|
---|
20 | //
|
---|
21 | rtoken: function (r) { // regex token
|
---|
22 | return function (s) {
|
---|
23 | var mx = s.match(r);
|
---|
24 | if (mx) {
|
---|
25 | return ([ mx[0], s.substring(mx[0].length) ]);
|
---|
26 | } else {
|
---|
27 | throw new $P.Exception(s);
|
---|
28 | }
|
---|
29 | };
|
---|
30 | },
|
---|
31 | token: function (s) { // whitespace-eating token
|
---|
32 | return function (s) {
|
---|
33 | return _.rtoken(new RegExp("^\s*" + s + "\s*"))(s);
|
---|
34 | // Removed .strip()
|
---|
35 | // return _.rtoken(new RegExp("^\s*" + s + "\s*"))(s).strip();
|
---|
36 | };
|
---|
37 | },
|
---|
38 | stoken: function (s) { // string token
|
---|
39 | return _.rtoken(new RegExp("^" + s));
|
---|
40 | },
|
---|
41 |
|
---|
42 | //
|
---|
43 | // Atomic Operators
|
---|
44 | //
|
---|
45 |
|
---|
46 | until: function (p) {
|
---|
47 | return function (s) {
|
---|
48 | var qx = [], rx = null;
|
---|
49 | while (s.length) {
|
---|
50 | try {
|
---|
51 | rx = p.call(this, s);
|
---|
52 | } catch (e) {
|
---|
53 | qx.push(rx[0]);
|
---|
54 | s = rx[1];
|
---|
55 | continue;
|
---|
56 | }
|
---|
57 | break;
|
---|
58 | }
|
---|
59 | return [ qx, s ];
|
---|
60 | };
|
---|
61 | },
|
---|
62 | many: function (p) {
|
---|
63 | return function (s) {
|
---|
64 | var rx = [], r = null;
|
---|
65 | while (s.length) {
|
---|
66 | try {
|
---|
67 | r = p.call(this, s);
|
---|
68 | } catch (e) {
|
---|
69 | return [ rx, s ];
|
---|
70 | }
|
---|
71 | rx.push(r[0]);
|
---|
72 | s = r[1];
|
---|
73 | }
|
---|
74 | return [ rx, s ];
|
---|
75 | };
|
---|
76 | },
|
---|
77 |
|
---|
78 | // generator operators -- see below
|
---|
79 | optional: function (p) {
|
---|
80 | return function (s) {
|
---|
81 | var r = null;
|
---|
82 | try {
|
---|
83 | r = p.call(this, s);
|
---|
84 | } catch (e) {
|
---|
85 | return [ null, s ];
|
---|
86 | }
|
---|
87 | return [ r[0], r[1] ];
|
---|
88 | };
|
---|
89 | },
|
---|
90 | not: function (p) {
|
---|
91 | return function (s) {
|
---|
92 | try {
|
---|
93 | p.call(this, s);
|
---|
94 | } catch (e) {
|
---|
95 | return [null, s];
|
---|
96 | }
|
---|
97 | throw new $P.Exception(s);
|
---|
98 | };
|
---|
99 | },
|
---|
100 | ignore: function (p) {
|
---|
101 | return p ?
|
---|
102 | function (s) {
|
---|
103 | var r = null;
|
---|
104 | r = p.call(this, s);
|
---|
105 | return [null, r[1]];
|
---|
106 | } : null;
|
---|
107 | },
|
---|
108 | product: function () {
|
---|
109 | var px = arguments[0],
|
---|
110 | qx = Array.prototype.slice.call(arguments, 1), rx = [];
|
---|
111 | for (var i = 0 ; i < px.length ; i++) {
|
---|
112 | rx.push(_.each(px[i], qx));
|
---|
113 | }
|
---|
114 | return rx;
|
---|
115 | },
|
---|
116 | cache: function (rule) {
|
---|
117 | var cache = {}, r = null;
|
---|
118 | return function (s) {
|
---|
119 | try {
|
---|
120 | r = cache[s] = (cache[s] || rule.call(this, s));
|
---|
121 | } catch (e) {
|
---|
122 | r = cache[s] = e;
|
---|
123 | }
|
---|
124 | if (r instanceof $P.Exception) {
|
---|
125 | throw r;
|
---|
126 | } else {
|
---|
127 | return r;
|
---|
128 | }
|
---|
129 | };
|
---|
130 | },
|
---|
131 |
|
---|
132 | // vector operators -- see below
|
---|
133 | any: function () {
|
---|
134 | var px = arguments;
|
---|
135 | return function (s) {
|
---|
136 | var r = null;
|
---|
137 | for (var i = 0; i < px.length; i++) {
|
---|
138 | if (px[i] == null) {
|
---|
139 | continue;
|
---|
140 | }
|
---|
141 | try {
|
---|
142 | r = (px[i].call(this, s));
|
---|
143 | } catch (e) {
|
---|
144 | r = null;
|
---|
145 | }
|
---|
146 | if (r) {
|
---|
147 | return r;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | throw new $P.Exception(s);
|
---|
151 | };
|
---|
152 | },
|
---|
153 | each: function () {
|
---|
154 | var px = arguments;
|
---|
155 | return function (s) {
|
---|
156 | var rx = [], r = null;
|
---|
157 | for (var i = 0; i < px.length ; i++) {
|
---|
158 | if (px[i] == null) {
|
---|
159 | continue;
|
---|
160 | }
|
---|
161 | try {
|
---|
162 | r = (px[i].call(this, s));
|
---|
163 | } catch (e) {
|
---|
164 | throw new $P.Exception(s);
|
---|
165 | }
|
---|
166 | rx.push(r[0]);
|
---|
167 | s = r[1];
|
---|
168 | }
|
---|
169 | return [ rx, s];
|
---|
170 | };
|
---|
171 | },
|
---|
172 | all: function () {
|
---|
173 | var px = arguments, _ = _;
|
---|
174 | return _.each(_.optional(px));
|
---|
175 | },
|
---|
176 |
|
---|
177 | // delimited operators
|
---|
178 | sequence: function (px, d, c) {
|
---|
179 | d = d || _.rtoken(/^\s*/);
|
---|
180 | c = c || null;
|
---|
181 |
|
---|
182 | if (px.length == 1) {
|
---|
183 | return px[0];
|
---|
184 | }
|
---|
185 | return function (s) {
|
---|
186 | var r = null, q = null;
|
---|
187 | var rx = [];
|
---|
188 | for (var i = 0; i < px.length ; i++) {
|
---|
189 | try {
|
---|
190 | r = px[i].call(this, s);
|
---|
191 | } catch (e) {
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | rx.push(r[0]);
|
---|
195 | try {
|
---|
196 | q = d.call(this, r[1]);
|
---|
197 | } catch (ex) {
|
---|
198 | q = null;
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | s = q[1];
|
---|
202 | }
|
---|
203 | if (!r) {
|
---|
204 | throw new $P.Exception(s);
|
---|
205 | }
|
---|
206 | if (q) {
|
---|
207 | throw new $P.Exception(q[1]);
|
---|
208 | }
|
---|
209 | if (c) {
|
---|
210 | try {
|
---|
211 | r = c.call(this, r[1]);
|
---|
212 | } catch (ey) {
|
---|
213 | throw new $P.Exception(r[1]);
|
---|
214 | }
|
---|
215 | }
|
---|
216 | return [ rx, (r?r[1]:s) ];
|
---|
217 | };
|
---|
218 | },
|
---|
219 |
|
---|
220 | //
|
---|
221 | // Composite Operators
|
---|
222 | //
|
---|
223 |
|
---|
224 | between: function (d1, p, d2) {
|
---|
225 | d2 = d2 || d1;
|
---|
226 | var _fn = _.each(_.ignore(d1), p, _.ignore(d2));
|
---|
227 | return function (s) {
|
---|
228 | var rx = _fn.call(this, s);
|
---|
229 | return [[rx[0][0], r[0][2]], rx[1]];
|
---|
230 | };
|
---|
231 | },
|
---|
232 | list: function (p, d, c) {
|
---|
233 | d = d || _.rtoken(/^\s*/);
|
---|
234 | c = c || null;
|
---|
235 | return (p instanceof Array ?
|
---|
236 | _.each(_.product(p.slice(0, -1), _.ignore(d)), p.slice(-1), _.ignore(c)) :
|
---|
237 | _.each(_.many(_.each(p, _.ignore(d))), px, _.ignore(c)));
|
---|
238 | },
|
---|
239 | set: function (px, d, c) {
|
---|
240 | d = d || _.rtoken(/^\s*/);
|
---|
241 | c = c || null;
|
---|
242 | return function (s) {
|
---|
243 | // r is the current match, best the current 'best' match
|
---|
244 | // which means it parsed the most amount of input
|
---|
245 | var r = null, p = null, q = null, rx = null, best = [[], s], last = false;
|
---|
246 |
|
---|
247 | // go through the rules in the given set
|
---|
248 | for (var i = 0; i < px.length ; i++) {
|
---|
249 |
|
---|
250 | // last is a flag indicating whether this must be the last element
|
---|
251 | // if there is only 1 element, then it MUST be the last one
|
---|
252 | q = null;
|
---|
253 | p = null;
|
---|
254 | r = null;
|
---|
255 | last = (px.length == 1);
|
---|
256 |
|
---|
257 | // first, we try simply to match the current pattern
|
---|
258 | // if not, try the next pattern
|
---|
259 | try {
|
---|
260 | r = px[i].call(this, s);
|
---|
261 | } catch (e) {
|
---|
262 | continue;
|
---|
263 | }
|
---|
264 |
|
---|
265 | // since we are matching against a set of elements, the first
|
---|
266 | // thing to do is to add r[0] to matched elements
|
---|
267 | rx = [[r[0]], r[1]];
|
---|
268 |
|
---|
269 | // if we matched and there is still input to parse and
|
---|
270 | // we don't already know this is the last element,
|
---|
271 | // we're going to next check for the delimiter ...
|
---|
272 | // if there's none, or if there's no input left to parse
|
---|
273 | // than this must be the last element after all ...
|
---|
274 | if (r[1].length > 0 && ! last) {
|
---|
275 | try {
|
---|
276 | q = d.call(this, r[1]);
|
---|
277 | } catch (ex) {
|
---|
278 | last = true;
|
---|
279 | }
|
---|
280 | } else {
|
---|
281 | last = true;
|
---|
282 | }
|
---|
283 |
|
---|
284 | // if we parsed the delimiter and now there's no more input,
|
---|
285 | // that means we shouldn't have parsed the delimiter at all
|
---|
286 | // so don't update r and mark this as the last element ...
|
---|
287 | if (!last && q[1].length === 0) {
|
---|
288 | last = true;
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | // so, if this isn't the last element, we're going to see if
|
---|
293 | // we can get any more matches from the remaining (unmatched)
|
---|
294 | // elements ...
|
---|
295 | if (!last) {
|
---|
296 |
|
---|
297 | // build a list of the remaining rules we can match against,
|
---|
298 | // i.e., all but the one we just matched against
|
---|
299 | var qx = [];
|
---|
300 | for (var j = 0; j < px.length ; j++) {
|
---|
301 | if (i != j) {
|
---|
302 | qx.push(px[j]);
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | // now invoke recursively set with the remaining input
|
---|
307 | // note that we don't include the closing delimiter ...
|
---|
308 | // we'll check for that ourselves at the end
|
---|
309 | p = _.set(qx, d).call(this, q[1]);
|
---|
310 |
|
---|
311 | // if we got a non-empty set as a result ...
|
---|
312 | // (otw rx already contains everything we want to match)
|
---|
313 | if (p[0].length > 0) {
|
---|
314 | // update current result, which is stored in rx ...
|
---|
315 | // basically, pick up the remaining text from p[1]
|
---|
316 | // and concat the result from p[0] so that we don't
|
---|
317 | // get endless nesting ...
|
---|
318 | rx[0] = rx[0].concat(p[0]);
|
---|
319 | rx[1] = p[1];
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | // at this point, rx either contains the last matched element
|
---|
324 | // or the entire matched set that starts with this element.
|
---|
325 |
|
---|
326 | // now we just check to see if this variation is better than
|
---|
327 | // our best so far, in terms of how much of the input is parsed
|
---|
328 | if (rx[1].length < best[1].length) {
|
---|
329 | best = rx;
|
---|
330 | }
|
---|
331 |
|
---|
332 | // if we've parsed all the input, then we're finished
|
---|
333 | if (best[1].length === 0) {
|
---|
334 | break;
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | // so now we've either gone through all the patterns trying them
|
---|
339 | // as the initial match; or we found one that parsed the entire
|
---|
340 | // input string ...
|
---|
341 |
|
---|
342 | // if best has no matches, just return empty set ...
|
---|
343 | if (best[0].length === 0) {
|
---|
344 | return best;
|
---|
345 | }
|
---|
346 |
|
---|
347 | // if a closing delimiter is provided, then we have to check it also
|
---|
348 | if (c) {
|
---|
349 | // we try this even if there is no remaining input because the pattern
|
---|
350 | // may well be optional or match empty input ...
|
---|
351 | try {
|
---|
352 | q = c.call(this, best[1]);
|
---|
353 | } catch (ey) {
|
---|
354 | throw new $P.Exception(best[1]);
|
---|
355 | }
|
---|
356 |
|
---|
357 | // it parsed ... be sure to update the best match remaining input
|
---|
358 | best[1] = q[1];
|
---|
359 | }
|
---|
360 |
|
---|
361 | // if we're here, either there was no closing delimiter or we parsed it
|
---|
362 | // so now we have the best match; just return it!
|
---|
363 | return best;
|
---|
364 | };
|
---|
365 | },
|
---|
366 | forward: function (gr, fname) {
|
---|
367 | return function (s) {
|
---|
368 | return gr[fname].call(this, s);
|
---|
369 | };
|
---|
370 | },
|
---|
371 |
|
---|
372 | //
|
---|
373 | // Translation Operators
|
---|
374 | //
|
---|
375 | replace: function (rule, repl) {
|
---|
376 | return function (s) {
|
---|
377 | var r = rule.call(this, s);
|
---|
378 | return [repl, r[1]];
|
---|
379 | };
|
---|
380 | },
|
---|
381 | process: function (rule, fn) {
|
---|
382 | return function (s) {
|
---|
383 | var r = rule.call(this, s);
|
---|
384 | return [fn.call(this, r[0]), r[1]];
|
---|
385 | };
|
---|
386 | },
|
---|
387 | min: function (min, rule) {
|
---|
388 | return function (s) {
|
---|
389 | var rx = rule.call(this, s);
|
---|
390 | if (rx[0].length < min) {
|
---|
391 | throw new $P.Exception(s);
|
---|
392 | }
|
---|
393 | return rx;
|
---|
394 | };
|
---|
395 | }
|
---|
396 | };
|
---|
397 |
|
---|
398 |
|
---|
399 | // Generator Operators And Vector Operators
|
---|
400 |
|
---|
401 | // Generators are operators that have a signature of F(R) => R,
|
---|
402 | // taking a given rule and returning another rule, such as
|
---|
403 | // ignore, which parses a given rule and throws away the result.
|
---|
404 |
|
---|
405 | // Vector operators are those that have a signature of F(R1,R2,...) => R,
|
---|
406 | // take a list of rules and returning a new rule, such as each.
|
---|
407 |
|
---|
408 | // Generator operators are converted (via the following _generator
|
---|
409 | // function) into functions that can also take a list or array of rules
|
---|
410 | // and return an array of new rules as though the function had been
|
---|
411 | // called on each rule in turn (which is what actually happens).
|
---|
412 |
|
---|
413 | // This allows generators to be used with vector operators more easily.
|
---|
414 | // Example:
|
---|
415 | // each(ignore(foo, bar)) instead of each(ignore(foo), ignore(bar))
|
---|
416 |
|
---|
417 | // This also turns generators into vector operators, which allows
|
---|
418 | // constructs like:
|
---|
419 | // not(cache(foo, bar))
|
---|
420 |
|
---|
421 | var _generator = function (op) {
|
---|
422 | return function () {
|
---|
423 | var args = null, rx = [];
|
---|
424 | if (arguments.length > 1) {
|
---|
425 | args = Array.prototype.slice.call(arguments);
|
---|
426 | } else if (arguments[0] instanceof Array) {
|
---|
427 | args = arguments[0];
|
---|
428 | }
|
---|
429 | if (args) {
|
---|
430 | for (var i = 0, px = args.shift() ; i < px.length ; i++) {
|
---|
431 | args.unshift(px[i]);
|
---|
432 | rx.push(op.apply(null, args));
|
---|
433 | args.shift();
|
---|
434 | return rx;
|
---|
435 | }
|
---|
436 | } else {
|
---|
437 | return op.apply(null, arguments);
|
---|
438 | }
|
---|
439 | };
|
---|
440 | };
|
---|
441 |
|
---|
442 | var gx = "optional not ignore cache".split(/\s/);
|
---|
443 |
|
---|
444 | for (var i = 0 ; i < gx.length ; i++) {
|
---|
445 | _[gx[i]] = _generator(_[gx[i]]);
|
---|
446 | }
|
---|
447 |
|
---|
448 | var _vector = function (op) {
|
---|
449 | return function () {
|
---|
450 | if (arguments[0] instanceof Array) {
|
---|
451 | return op.apply(null, arguments[0]);
|
---|
452 | } else {
|
---|
453 | return op.apply(null, arguments);
|
---|
454 | }
|
---|
455 | };
|
---|
456 | };
|
---|
457 |
|
---|
458 | var vx = "each any all".split(/\s/);
|
---|
459 |
|
---|
460 | for (var j = 0 ; j < vx.length ; j++) {
|
---|
461 | _[vx[j]] = _vector(_[vx[j]]);
|
---|
462 | }
|
---|
463 |
|
---|
464 | }());
|
---|
465 |
|
---|
466 | (function () {
|
---|
467 | var flattenAndCompact = function (ax) {
|
---|
468 | var rx = [];
|
---|
469 | for (var i = 0; i < ax.length; i++) {
|
---|
470 | if (ax[i] instanceof Array) {
|
---|
471 | rx = rx.concat(flattenAndCompact(ax[i]));
|
---|
472 | } else {
|
---|
473 | if (ax[i]) {
|
---|
474 | rx.push(ax[i]);
|
---|
475 | }
|
---|
476 | }
|
---|
477 | }
|
---|
478 | return rx;
|
---|
479 | };
|
---|
480 |
|
---|
481 | Date.Grammar = {};
|
---|
482 |
|
---|
483 | Date.Translator = {
|
---|
484 | hour: function (s) {
|
---|
485 | return function () {
|
---|
486 | this.hour = Number(s);
|
---|
487 | };
|
---|
488 | },
|
---|
489 | minute: function (s) {
|
---|
490 | return function () {
|
---|
491 | this.minute = Number(s);
|
---|
492 | };
|
---|
493 | },
|
---|
494 | second: function (s) {
|
---|
495 | return function () {
|
---|
496 | this.second = Number(s);
|
---|
497 | };
|
---|
498 | },
|
---|
499 | meridian: function (s) {
|
---|
500 | return function () {
|
---|
501 | this.meridian = s.slice(0, 1).toLowerCase();
|
---|
502 | };
|
---|
503 | },
|
---|
504 | timezone: function (s) {
|
---|
505 | return function () {
|
---|
506 | var n = s.replace(/[^\d\+\-]/g, "");
|
---|
507 | if (n.length) {
|
---|
508 | this.timezoneOffset = Number(n);
|
---|
509 | } else {
|
---|
510 | this.timezone = s.toLowerCase();
|
---|
511 | }
|
---|
512 | };
|
---|
513 | },
|
---|
514 | day: function (x) {
|
---|
515 | var s = x[0];
|
---|
516 | return function () {
|
---|
517 | this.day = Number(s.match(/\d+/)[0]);
|
---|
518 | };
|
---|
519 | },
|
---|
520 | month: function (s) {
|
---|
521 | return function () {
|
---|
522 | this.month = ((s.length == 3) ? Date.getMonthNumberFromName(s) : (Number(s) - 1));
|
---|
523 | };
|
---|
524 | },
|
---|
525 | year: function (s) {
|
---|
526 | return function () {
|
---|
527 | var n = Number(s);
|
---|
528 | this.year = ((s.length > 2) ? n :
|
---|
529 | (n + (((n + 2000) < Date.CultureInfo.twoDigitYearMax) ? 2000 : 1900)));
|
---|
530 | };
|
---|
531 | },
|
---|
532 | rday: function (s) {
|
---|
533 | return function () {
|
---|
534 | switch (s) {
|
---|
535 | case "yesterday":
|
---|
536 | this.days = -1;
|
---|
537 | break;
|
---|
538 | case "tomorrow":
|
---|
539 | this.days = 1;
|
---|
540 | break;
|
---|
541 | case "today":
|
---|
542 | this.days = 0;
|
---|
543 | break;
|
---|
544 | case "now":
|
---|
545 | this.days = 0;
|
---|
546 | this.now = true;
|
---|
547 | break;
|
---|
548 | }
|
---|
549 | };
|
---|
550 | },
|
---|
551 | finishExact: function (x) {
|
---|
552 | x = (x instanceof Array) ? x : [ x ];
|
---|
553 |
|
---|
554 | var now = new Date();
|
---|
555 |
|
---|
556 | this.year = now.getFullYear();
|
---|
557 | this.month = now.getMonth();
|
---|
558 | this.day = 1;
|
---|
559 |
|
---|
560 | this.hour = 0;
|
---|
561 | this.minute = 0;
|
---|
562 | this.second = 0;
|
---|
563 |
|
---|
564 | for (var i = 0 ; i < x.length ; i++) {
|
---|
565 | if (x[i]) {
|
---|
566 | x[i].call(this);
|
---|
567 | }
|
---|
568 | }
|
---|
569 |
|
---|
570 | this.hour = (this.meridian == "p" && this.hour < 13) ? this.hour + 12 : this.hour;
|
---|
571 |
|
---|
572 | if (this.day > Date.getDaysInMonth(this.year, this.month)) {
|
---|
573 | throw new RangeError(this.day + " is not a valid value for days.");
|
---|
574 | }
|
---|
575 |
|
---|
576 | var r = new Date(this.year, this.month, this.day, this.hour, this.minute, this.second);
|
---|
577 |
|
---|
578 | if (this.timezone) {
|
---|
579 | r.set({ timezone: this.timezone });
|
---|
580 | } else if (this.timezoneOffset) {
|
---|
581 | r.set({ timezoneOffset: this.timezoneOffset });
|
---|
582 | }
|
---|
583 | return r;
|
---|
584 | },
|
---|
585 | finish: function (x) {
|
---|
586 | x = (x instanceof Array) ? flattenAndCompact(x) : [ x ];
|
---|
587 |
|
---|
588 | if (x.length === 0) {
|
---|
589 | return null;
|
---|
590 | }
|
---|
591 |
|
---|
592 | for (var i = 0 ; i < x.length ; i++) {
|
---|
593 | if (typeof x[i] == "function") {
|
---|
594 | x[i].call(this);
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | if (this.now) {
|
---|
599 | return new Date();
|
---|
600 | }
|
---|
601 |
|
---|
602 | var today = Date.today();
|
---|
603 | var method = null;
|
---|
604 |
|
---|
605 | var expression = !!(this.days != null || this.orient || this.operator);
|
---|
606 | if (expression) {
|
---|
607 | var gap, mod, orient;
|
---|
608 | orient = ((this.orient == "past" || this.operator == "subtract") ? -1 : 1);
|
---|
609 |
|
---|
610 | if (this.weekday) {
|
---|
611 | this.unit = "day";
|
---|
612 | gap = (Date.getDayNumberFromName(this.weekday) - today.getDay());
|
---|
613 | mod = 7;
|
---|
614 | this.days = gap ? ((gap + (orient * mod)) % mod) : (orient * mod);
|
---|
615 | }
|
---|
616 | if (this.month) {
|
---|
617 | this.unit = "month";
|
---|
618 | gap = (this.month - today.getMonth());
|
---|
619 | mod = 12;
|
---|
620 | this.months = gap ? ((gap + (orient * mod)) % mod) : (orient * mod);
|
---|
621 | this.month = null;
|
---|
622 | }
|
---|
623 | if (!this.unit) {
|
---|
624 | this.unit = "day";
|
---|
625 | }
|
---|
626 | if (this[this.unit + "s"] == null || this.operator != null) {
|
---|
627 | if (!this.value) {
|
---|
628 | this.value = 1;
|
---|
629 | }
|
---|
630 |
|
---|
631 | if (this.unit == "week") {
|
---|
632 | this.unit = "day";
|
---|
633 | this.value = this.value * 7;
|
---|
634 | }
|
---|
635 |
|
---|
636 | this[this.unit + "s"] = this.value * orient;
|
---|
637 | }
|
---|
638 | return today.add(this);
|
---|
639 | } else {
|
---|
640 | if (this.meridian && this.hour) {
|
---|
641 | this.hour = (this.hour < 13 && this.meridian == "p") ? this.hour + 12 : this.hour;
|
---|
642 | }
|
---|
643 | if (this.weekday && !this.day) {
|
---|
644 | this.day = (today.addDays((Date.getDayNumberFromName(this.weekday) - today.getDay()))).getDate();
|
---|
645 | }
|
---|
646 | if (this.month && !this.day) {
|
---|
647 | this.day = 1;
|
---|
648 | }
|
---|
649 | return today.set(this);
|
---|
650 | }
|
---|
651 | }
|
---|
652 | };
|
---|
653 |
|
---|
654 | var _ = Date.Parsing.Operators, g = Date.Grammar, t = Date.Translator, _fn;
|
---|
655 |
|
---|
656 | g.datePartDelimiter = _.rtoken(/^([\s\-\.\,\/\x27]+)/);
|
---|
657 | g.timePartDelimiter = _.stoken(":");
|
---|
658 | g.whiteSpace = _.rtoken(/^\s*/);
|
---|
659 | g.generalDelimiter = _.rtoken(/^(([\s\,]|at|on)+)/);
|
---|
660 |
|
---|
661 | var _C = {};
|
---|
662 | g.ctoken = function (keys) {
|
---|
663 | var fn = _C[keys];
|
---|
664 | if (! fn) {
|
---|
665 | var c = Date.CultureInfo.regexPatterns;
|
---|
666 | var kx = keys.split(/\s+/), px = [];
|
---|
667 | for (var i = 0; i < kx.length ; i++) {
|
---|
668 | px.push(_.replace(_.rtoken(c[kx[i]]), kx[i]));
|
---|
669 | }
|
---|
670 | fn = _C[keys] = _.any.apply(null, px);
|
---|
671 | }
|
---|
672 | return fn;
|
---|
673 | };
|
---|
674 | g.ctoken2 = function (key) {
|
---|
675 | return _.rtoken(Date.CultureInfo.regexPatterns[key]);
|
---|
676 | };
|
---|
677 |
|
---|
678 | // hour, minute, second, meridian, timezone
|
---|
679 | g.h = _.cache(_.process(_.rtoken(/^(0[0-9]|1[0-2]|[1-9])/), t.hour));
|
---|
680 | g.hh = _.cache(_.process(_.rtoken(/^(0[0-9]|1[0-2])/), t.hour));
|
---|
681 | g.H = _.cache(_.process(_.rtoken(/^([0-1][0-9]|2[0-3]|[0-9])/), t.hour));
|
---|
682 | g.HH = _.cache(_.process(_.rtoken(/^([0-1][0-9]|2[0-3])/), t.hour));
|
---|
683 | g.m = _.cache(_.process(_.rtoken(/^([0-5][0-9]|[0-9])/), t.minute));
|
---|
684 | g.mm = _.cache(_.process(_.rtoken(/^[0-5][0-9]/), t.minute));
|
---|
685 | g.s = _.cache(_.process(_.rtoken(/^([0-5][0-9]|[0-9])/), t.second));
|
---|
686 | g.ss = _.cache(_.process(_.rtoken(/^[0-5][0-9]/), t.second));
|
---|
687 | g.hms = _.cache(_.sequence([g.H, g.mm, g.ss], g.timePartDelimiter));
|
---|
688 |
|
---|
689 | // _.min(1, _.set([ g.H, g.m, g.s ], g._t));
|
---|
690 | g.t = _.cache(_.process(g.ctoken2("shortMeridian"), t.meridian));
|
---|
691 | g.tt = _.cache(_.process(g.ctoken2("longMeridian"), t.meridian));
|
---|
692 | g.z = _.cache(_.process(_.rtoken(/^(\+|\-)?\s*\d\d\d\d?/), t.timezone));
|
---|
693 | g.zz = _.cache(_.process(_.rtoken(/^(\+|\-)\s*\d\d\d\d/), t.timezone));
|
---|
694 | g.zzz = _.cache(_.process(g.ctoken2("timezone"), t.timezone));
|
---|
695 | g.timeSuffix = _.each(_.ignore(g.whiteSpace), _.set([ g.tt, g.zzz ]));
|
---|
696 | g.time = _.each(_.optional(_.ignore(_.stoken("T"))), g.hms, g.timeSuffix);
|
---|
697 |
|
---|
698 | // days, months, years
|
---|
699 | g.d = _.cache(_.process(_.each(_.rtoken(/^([0-2]\d|3[0-1]|\d)/),
|
---|
700 | _.optional(g.ctoken2("ordinalSuffix"))), t.day));
|
---|
701 | g.dd = _.cache(_.process(_.each(_.rtoken(/^([0-2]\d|3[0-1])/),
|
---|
702 | _.optional(g.ctoken2("ordinalSuffix"))), t.day));
|
---|
703 | g.ddd = g.dddd = _.cache(_.process(g.ctoken("sun mon tue wed thu fri sat"),
|
---|
704 | function (s) {
|
---|
705 | return function () {
|
---|
706 | this.weekday = s;
|
---|
707 | };
|
---|
708 | }
|
---|
709 | ));
|
---|
710 | g.M = _.cache(_.process(_.rtoken(/^(1[0-2]|0\d|\d)/), t.month));
|
---|
711 | g.MM = _.cache(_.process(_.rtoken(/^(1[0-2]|0\d)/), t.month));
|
---|
712 | g.MMM = g.MMMM = _.cache(_.process(
|
---|
713 | g.ctoken("jan feb mar apr may jun jul aug sep oct nov dec"), t.month));
|
---|
714 | g.y = _.cache(_.process(_.rtoken(/^(\d\d?)/), t.year));
|
---|
715 | g.yy = _.cache(_.process(_.rtoken(/^(\d\d)/), t.year));
|
---|
716 | g.yyy = _.cache(_.process(_.rtoken(/^(\d\d?\d?\d?)/), t.year));
|
---|
717 | g.yyyy = _.cache(_.process(_.rtoken(/^(\d\d\d\d)/), t.year));
|
---|
718 |
|
---|
719 | // rolling these up into general purpose rules
|
---|
720 | _fn = function () {
|
---|
721 | return _.each(_.any.apply(null, arguments), _.not(g.ctoken2("timeContext")));
|
---|
722 | };
|
---|
723 |
|
---|
724 | g.day = _fn(g.d, g.dd);
|
---|
725 | g.month = _fn(g.M, g.MMM);
|
---|
726 | g.year = _fn(g.yyyy, g.yy);
|
---|
727 |
|
---|
728 | // relative date / time expressions
|
---|
729 | g.orientation = _.process(g.ctoken("past future"),
|
---|
730 | function (s) {
|
---|
731 | return function () {
|
---|
732 | this.orient = s;
|
---|
733 | };
|
---|
734 | }
|
---|
735 | );
|
---|
736 | g.operator = _.process(g.ctoken("add subtract"),
|
---|
737 | function (s) {
|
---|
738 | return function () {
|
---|
739 | this.operator = s;
|
---|
740 | };
|
---|
741 | }
|
---|
742 | );
|
---|
743 | g.rday = _.process(g.ctoken("yesterday tomorrow today now"), t.rday);
|
---|
744 | g.unit = _.process(g.ctoken("minute hour day week month year"),
|
---|
745 | function (s) {
|
---|
746 | return function () {
|
---|
747 | this.unit = s;
|
---|
748 | };
|
---|
749 | }
|
---|
750 | );
|
---|
751 | g.value = _.process(_.rtoken(/^\d\d?(st|nd|rd|th)?/),
|
---|
752 | function (s) {
|
---|
753 | return function () {
|
---|
754 | this.value = s.replace(/\D/g, "");
|
---|
755 | };
|
---|
756 | }
|
---|
757 | );
|
---|
758 | g.expression = _.set([ g.rday, g.operator, g.value, g.unit, g.orientation, g.ddd, g.MMM ]);
|
---|
759 |
|
---|
760 | // pre-loaded rules for different date part order preferences
|
---|
761 | _fn = function () {
|
---|
762 | return _.set(arguments, g.datePartDelimiter);
|
---|
763 | };
|
---|
764 | g.mdy = _fn(g.ddd, g.month, g.day, g.year);
|
---|
765 | g.ymd = _fn(g.ddd, g.year, g.month, g.day);
|
---|
766 | g.dmy = _fn(g.ddd, g.day, g.month, g.year);
|
---|
767 | g.date = function (s) {
|
---|
768 | return ((g[Date.CultureInfo.dateElementOrder] || g.mdy).call(this, s));
|
---|
769 | };
|
---|
770 |
|
---|
771 | // parsing date format specifiers - ex: "h:m:s tt"
|
---|
772 | // this little guy will generate a custom parser based
|
---|
773 | // on the format string, ex: g.format("h:m:s tt")
|
---|
774 | g.format = _.process(_.many(
|
---|
775 | _.any(
|
---|
776 | // translate format specifiers into grammar rules
|
---|
777 | _.process(
|
---|
778 | _.rtoken(/^(dd?d?d?|MM?M?M?|yy?y?y?|hh?|HH?|mm?|ss?|tt?|zz?z?)/),
|
---|
779 | function (fmt) {
|
---|
780 | if (g[fmt]) {
|
---|
781 | return g[fmt];
|
---|
782 | } else {
|
---|
783 | throw Date.Parsing.Exception(fmt);
|
---|
784 | }
|
---|
785 | }
|
---|
786 | ),
|
---|
787 | // translate separator tokens into token rules
|
---|
788 | _.process(
|
---|
789 | _.rtoken(/^[^dMyhHmstz]+/), // all legal separators
|
---|
790 | function (s) {
|
---|
791 | return _.ignore(_.stoken(s));
|
---|
792 | }
|
---|
793 | )
|
---|
794 | )),
|
---|
795 | // construct the parser ...
|
---|
796 | function (rules) {
|
---|
797 | return _.process(_.each.apply(null, rules), t.finishExact);
|
---|
798 | }
|
---|
799 | );
|
---|
800 |
|
---|
801 | var _F = {
|
---|
802 | //"M/d/yyyy": function (s) {
|
---|
803 | // var m = s.match(/^([0-2]\d|3[0-1]|\d)\/(1[0-2]|0\d|\d)\/(\d\d\d\d)/);
|
---|
804 | // if (m!=null) {
|
---|
805 | // var r = [ t.month.call(this,m[1]), t.day.call(this,m[2]), t.year.call(this,m[3]) ];
|
---|
806 | // r = t.finishExact.call(this,r);
|
---|
807 | // return [ r, "" ];
|
---|
808 | // } else {
|
---|
809 | // throw new Date.Parsing.Exception(s);
|
---|
810 | // }
|
---|
811 | //}
|
---|
812 | //"M/d/yyyy": function (s) { return [ new Date(Date._parse(s)), ""]; }
|
---|
813 | };
|
---|
814 | var _get = function (f) {
|
---|
815 | return _F[f] = (_F[f] || g.format(f)[0]);
|
---|
816 | };
|
---|
817 |
|
---|
818 | g.formats = function (fx) {
|
---|
819 | if (fx instanceof Array) {
|
---|
820 | var rx = [];
|
---|
821 | for (var i = 0 ; i < fx.length ; i++) {
|
---|
822 | rx.push(_get(fx[i]));
|
---|
823 | }
|
---|
824 | return _.any.apply(null, rx);
|
---|
825 | } else {
|
---|
826 | return _get(fx);
|
---|
827 | }
|
---|
828 | };
|
---|
829 |
|
---|
830 | // check for these formats first
|
---|
831 | g._formats = g.formats([
|
---|
832 | "yyyy-MM-ddTHH:mm:ss",
|
---|
833 | "ddd, MMM dd, yyyy H:mm:ss tt",
|
---|
834 | "ddd MMM d yyyy HH:mm:ss zzz",
|
---|
835 | "d"
|
---|
836 | ]);
|
---|
837 |
|
---|
838 | // starting rule for general purpose grammar
|
---|
839 | g._start = _.process(_.set([ g.date, g.time, g.expression ],
|
---|
840 | g.generalDelimiter, g.whiteSpace), t.finish);
|
---|
841 |
|
---|
842 | // real starting rule: tries selected formats first,
|
---|
843 | // then general purpose rule
|
---|
844 | g.start = function (s) {
|
---|
845 | try {
|
---|
846 | var r = g._formats.call({}, s);
|
---|
847 | if (r[1].length === 0) {
|
---|
848 | return r;
|
---|
849 | }
|
---|
850 | } catch (e) {}
|
---|
851 | return g._start.call({}, s);
|
---|
852 | };
|
---|
853 |
|
---|
854 | }());
|
---|
855 |
|
---|
856 |
|
---|
857 | Date._parse = Date.parse;
|
---|
858 |
|
---|
859 | /**
|
---|
860 | * Converts the specified string value into its JavaScript Date equivalent using CultureInfo specific format information.
|
---|
861 | *
|
---|
862 | * Example
|
---|
863 | <pre><code>
|
---|
864 | ///////////
|
---|
865 | // Dates //
|
---|
866 | ///////////
|
---|
867 |
|
---|
868 | // 15-Oct-2004
|
---|
869 | var d1 = Date.parse("10/15/2004");
|
---|
870 |
|
---|
871 | // 15-Oct-2004
|
---|
872 | var d1 = Date.parse("15-Oct-2004");
|
---|
873 |
|
---|
874 | // 15-Oct-2004
|
---|
875 | var d1 = Date.parse("2004.10.15");
|
---|
876 |
|
---|
877 | //Fri Oct 15, 2004
|
---|
878 | var d1 = Date.parse("Fri Oct 15, 2004");
|
---|
879 |
|
---|
880 | ///////////
|
---|
881 | // Times //
|
---|
882 | ///////////
|
---|
883 |
|
---|
884 | // Today at 10 PM.
|
---|
885 | var d1 = Date.parse("10 PM");
|
---|
886 |
|
---|
887 | // Today at 10:30 PM.
|
---|
888 | var d1 = Date.parse("10:30 P.M.");
|
---|
889 |
|
---|
890 | // Today at 6 AM.
|
---|
891 | var d1 = Date.parse("06am");
|
---|
892 |
|
---|
893 | /////////////////////
|
---|
894 | // Dates and Times //
|
---|
895 | /////////////////////
|
---|
896 |
|
---|
897 | // 8-July-2004 @ 10:30 PM
|
---|
898 | var d1 = Date.parse("July 8th, 2004, 10:30 PM");
|
---|
899 |
|
---|
900 | // 1-July-2004 @ 10:30 PM
|
---|
901 | var d1 = Date.parse("2004-07-01T22:30:00");
|
---|
902 |
|
---|
903 | ////////////////////
|
---|
904 | // Relative Dates //
|
---|
905 | ////////////////////
|
---|
906 |
|
---|
907 | // Returns today's date. The string "today" is culture specific.
|
---|
908 | var d1 = Date.parse("today");
|
---|
909 |
|
---|
910 | // Returns yesterday's date. The string "yesterday" is culture specific.
|
---|
911 | var d1 = Date.parse("yesterday");
|
---|
912 |
|
---|
913 | // Returns the date of the next thursday.
|
---|
914 | var d1 = Date.parse("Next thursday");
|
---|
915 |
|
---|
916 | // Returns the date of the most previous monday.
|
---|
917 | var d1 = Date.parse("last monday");
|
---|
918 |
|
---|
919 | // Returns today's day + one year.
|
---|
920 | var d1 = Date.parse("next year");
|
---|
921 |
|
---|
922 | ///////////////
|
---|
923 | // Date Math //
|
---|
924 | ///////////////
|
---|
925 |
|
---|
926 | // Today + 2 days
|
---|
927 | var d1 = Date.parse("t+2");
|
---|
928 |
|
---|
929 | // Today + 2 days
|
---|
930 | var d1 = Date.parse("today + 2 days");
|
---|
931 |
|
---|
932 | // Today + 3 months
|
---|
933 | var d1 = Date.parse("t+3m");
|
---|
934 |
|
---|
935 | // Today - 1 year
|
---|
936 | var d1 = Date.parse("today - 1 year");
|
---|
937 |
|
---|
938 | // Today - 1 year
|
---|
939 | var d1 = Date.parse("t-1y");
|
---|
940 |
|
---|
941 |
|
---|
942 | /////////////////////////////
|
---|
943 | // Partial Dates and Times //
|
---|
944 | /////////////////////////////
|
---|
945 |
|
---|
946 | // July 15th of this year.
|
---|
947 | var d1 = Date.parse("July 15");
|
---|
948 |
|
---|
949 | // 15th day of current day and year.
|
---|
950 | var d1 = Date.parse("15");
|
---|
951 |
|
---|
952 | // July 1st of current year at 10pm.
|
---|
953 | var d1 = Date.parse("7/1 10pm");
|
---|
954 | </code></pre>
|
---|
955 | *
|
---|
956 | * @param {String} The string value to convert into a Date object [Required]
|
---|
957 | * @return {Date} A Date object or null if the string cannot be converted into a Date.
|
---|
958 | */
|
---|
959 | Date.parse = function (s) {
|
---|
960 | var r = null;
|
---|
961 | if (!s) {
|
---|
962 | return null;
|
---|
963 | }
|
---|
964 | try {
|
---|
965 | r = Date.Grammar.start.call({}, s);
|
---|
966 | } catch (e) {
|
---|
967 | return null;
|
---|
968 | }
|
---|
969 | return ((r[1].length === 0) ? r[0] : null);
|
---|
970 | };
|
---|
971 |
|
---|
972 | Date.getParseFunction = function (fx) {
|
---|
973 | var fn = Date.Grammar.formats(fx);
|
---|
974 | return function (s) {
|
---|
975 | var r = null;
|
---|
976 | try {
|
---|
977 | r = fn.call({}, s);
|
---|
978 | } catch (e) {
|
---|
979 | return null;
|
---|
980 | }
|
---|
981 | return ((r[1].length === 0) ? r[0] : null);
|
---|
982 | };
|
---|
983 | };
|
---|
984 | /**
|
---|
985 | * Converts the specified string value into its JavaScript Date equivalent using the specified format {String} or formats {Array} and the CultureInfo specific format information.
|
---|
986 | * The format of the string value must match one of the supplied formats exactly.
|
---|
987 | *
|
---|
988 | * Example
|
---|
989 | <pre><code>
|
---|
990 | // 15-Oct-2004
|
---|
991 | var d1 = Date.parseExact("10/15/2004", "M/d/yyyy");
|
---|
992 |
|
---|
993 | // 15-Oct-2004
|
---|
994 | var d1 = Date.parse("15-Oct-2004", "M-ddd-yyyy");
|
---|
995 |
|
---|
996 | // 15-Oct-2004
|
---|
997 | var d1 = Date.parse("2004.10.15", "yyyy.MM.dd");
|
---|
998 |
|
---|
999 | // Multiple formats
|
---|
1000 | var d1 = Date.parseExact("10/15/2004", [ "M/d/yyyy" , "MMMM d, yyyy" ]);
|
---|
1001 | </code></pre>
|
---|
1002 | *
|
---|
1003 | * @param {String} The string value to convert into a Date object [Required].
|
---|
1004 | * @param {Object} The expected format {String} or an array of expected formats {Array} of the date string [Required].
|
---|
1005 | * @return {Date} A Date object or null if the string cannot be converted into a Date.
|
---|
1006 | */
|
---|
1007 | Date.parseExact = function (s, fx) {
|
---|
1008 | return Date.getParseFunction(fx)(s);
|
---|
1009 | }; |
---|