UNPKG

38.7 kBJavaScriptView Raw
1var vows = require('vows');
2var assert = require('assert');
3
4require('../lib/date-utils.js');
5
6vows.describe('Date Validate').addBatch({
7 'can deal with hours': {
8 topic: function () { return Date; },
9 'false for less than 0': function (topic) {
10 assert.equal(topic.validateHour(-1), false);
11 },
12 'false for greater than 23': function (topic) {
13 assert.equal(topic.validateHour(24), false);
14 },
15 'true for in range': function (topic) {
16 assert.equal(topic.validateHour(12), true);
17 }
18 },
19
20 'can deal with minutes': {
21 topic: function () { return Date; },
22 'false for less than 0': function (topic) {
23 assert.equal(topic.validateMinute(-1), false);
24 },
25 'false for greater than 59': function (topic) {
26 assert.equal(topic.validateMinute(60), false);
27 },
28 'true for in range': function (topic) {
29 assert.equal(topic.validateMinute(30), true);
30 }
31 },
32
33 'can deal with seconds': {
34 topic: function () { return Date; },
35 'false for less than 0': function (topic) {
36 assert.equal(topic.validateSecond(-1), false);
37 },
38 'false for greater than 59': function (topic) {
39 assert.equal(topic.validateSecond(60), false);
40 },
41 'true for in range': function (topic) {
42 assert.equal(topic.validateSecond(30), true);
43 }
44 },
45
46 'can deal with milliseconds': {
47 topic: function () { return Date; },
48 'false for less than 0': function (topic) {
49 assert.equal(topic.validateMillisecond(-1), false);
50 },
51 'false for greater than 999': function (topic) {
52 assert.equal(topic.validateMillisecond(1000), false);
53 },
54 'true for in range': function (topic) {
55 assert.equal(topic.validateMillisecond(500), true);
56 }
57 },
58
59 'can deal with years': {
60 topic: function () { return Date; },
61 'false for less than 0': function (topic) {
62 assert.equal(topic.validateYear(-1), false);
63 },
64 'false for greater than 9999': function (topic) {
65 assert.equal(topic.validateYear(10000), false);
66 },
67 'true for in range': function (topic) {
68 assert.equal(topic.validateYear(5000), true);
69 }
70 },
71
72 'can deal with days': {
73 topic: function () { return Date; },
74 'false for less than 0': function (topic) {
75 assert.equal(topic.validateDay(-1, 2011, 12), false);
76 },
77 'false for greater than 31': function (topic) {
78 assert.equal(topic.validateDay(32, 2011, 12), false);
79 },
80 'true for in range': function (topic) {
81 assert.equal(topic.validateDay(10, 2011, 11), true);
82 }
83 },
84
85 'static compare works': {
86 topic: function () { return Date.today(); },
87 '-1 for yesterday': function (topic) {
88 assert.equal(Date.compare(Date.yesterday(), topic), -1);
89 },
90 '1 for tomorrow': function (topic) {
91 assert.equal(Date.compare(Date.tomorrow(), topic), 1);
92 },
93 '0 for today': function (topic) {
94 assert.equal(Date.compare(Date.today(), topic), 0);
95 }
96 },
97
98 'static equals works': {
99 topic: function () { return Date.today(); },
100 'equal for today': function (topic) {
101 assert.equal(Date.equals(topic, Date.today()), true);
102 },
103 'false for tomorrow': function (topic) {
104 assert.equal(Date.equals(topic, Date.tomorrow()), false);
105 },
106 'false for yesterday': function (topic) {
107 assert.equal(Date.equals(topic, Date.yesterday()), false);
108 }
109 },
110
111 'static equalsDay works': {
112 topic: function () { return Date.today(); },
113 'true for today': function (topic) {
114 assert.equal(Date.equalsDay(topic, Date.today()), true);
115 },
116 'false for yesterday': function (topic) {
117 assert.equal(Date.equalsDay(topic, Date.yesterday()), false);
118 },
119 },
120
121 'getDayNumberFromName works': {
122 topic: function () { return Date; },
123 'sunday works': function (topic) {
124 assert.equal(topic.getDayNumberFromName('sunday'), 0);
125 },
126 'sun works': function (topic) {
127 assert.equal(topic.getDayNumberFromName('sun'), 0);
128 },
129 'su works': function (topic) {
130 assert.equal(topic.getDayNumberFromName('su'), 0);
131 },
132 'monday works': function (topic) {
133 assert.equal(topic.getDayNumberFromName('monday'), 1);
134 },
135 'mon works': function (topic) {
136 assert.equal(topic.getDayNumberFromName('mon'), 1);
137 },
138 'mo works': function (topic) {
139 assert.equal(topic.getDayNumberFromName('mo'), 1);
140 },
141 'tuesday works': function (topic) {
142 assert.equal(topic.getDayNumberFromName('tuesday'), 2);
143 },
144 'tue works': function (topic) {
145 assert.equal(topic.getDayNumberFromName('tue'), 2);
146 },
147 'tu works': function (topic) {
148 assert.equal(topic.getDayNumberFromName('tu'), 2);
149 },
150 'wednesday works': function (topic) {
151 assert.equal(topic.getDayNumberFromName('wednesday'), 3);
152 },
153 'wed works': function (topic) {
154 assert.equal(topic.getDayNumberFromName('wed'), 3);
155 },
156 'we works': function (topic) {
157 assert.equal(topic.getDayNumberFromName('we'), 3);
158 },
159 'thursday works': function (topic) {
160 assert.equal(topic.getDayNumberFromName('thursday'), 4);
161 },
162 'thu works': function (topic) {
163 assert.equal(topic.getDayNumberFromName('thu'), 4);
164 },
165 'th works': function (topic) {
166 assert.equal(topic.getDayNumberFromName('th'), 4);
167 },
168 'friday works': function (topic) {
169 assert.equal(topic.getDayNumberFromName('friday'), 5);
170 },
171 'fri works': function (topic) {
172 assert.equal(topic.getDayNumberFromName('fri'), 5);
173 },
174 'fr works': function (topic) {
175 assert.equal(topic.getDayNumberFromName('fr'), 5);
176 },
177 'saturday works': function (topic) {
178 assert.equal(topic.getDayNumberFromName('saturday'), 6);
179 },
180 'sat works': function (topic) {
181 assert.equal(topic.getDayNumberFromName('sat'), 6);
182 },
183 'sa works': function (topic) {
184 assert.equal(topic.getDayNumberFromName('sa'), 6);
185 },
186 'everything else does not': function (topic) {
187 assert.equal(topic.getDayNumberFromName('junk'), undefined);
188 }
189 },
190
191 'getMonthNumberFromName works': {
192 topic: function () { return Date; },
193 'january works': function (topic) {
194 assert.equal(topic.getMonthNumberFromName('january'), 0);
195 },
196 'jan works': function (topic) {
197 assert.equal(topic.getMonthNumberFromName('jan'), 0);
198 },
199 'february works': function (topic) {
200 assert.equal(topic.getMonthNumberFromName('february'), 1);
201 },
202 'feb works': function (topic) {
203 assert.equal(topic.getMonthNumberFromName('feb'), 1);
204 },
205 'march works': function (topic) {
206 assert.equal(topic.getMonthNumberFromName('march'), 2);
207 },
208 'mar works': function (topic) {
209 assert.equal(topic.getMonthNumberFromName('mar'), 2);
210 },
211 'april works': function (topic) {
212 assert.equal(topic.getMonthNumberFromName('april'), 3);
213 },
214 'apr works': function (topic) {
215 assert.equal(topic.getMonthNumberFromName('apr'), 3);
216 },
217 'may works': function (topic) {
218 assert.equal(topic.getMonthNumberFromName('may'), 4);
219 },
220 'june works': function (topic) {
221 assert.equal(topic.getMonthNumberFromName('june'), 5);
222 },
223 'jun works': function (topic) {
224 assert.equal(topic.getMonthNumberFromName('jun'), 5);
225 },
226 'july works': function (topic) {
227 assert.equal(topic.getMonthNumberFromName('july'), 6);
228 },
229 'jul works': function (topic) {
230 assert.equal(topic.getMonthNumberFromName('jul'), 6);
231 },
232 'august works': function (topic) {
233 assert.equal(topic.getMonthNumberFromName('august'), 7);
234 },
235 'aug works': function (topic) {
236 assert.equal(topic.getMonthNumberFromName('aug'), 7);
237 },
238 'september works': function (topic) {
239 assert.equal(topic.getMonthNumberFromName('september'), 8);
240 },
241 'sep works': function (topic) {
242 assert.equal(topic.getMonthNumberFromName('sep'), 8);
243 },
244 'october works': function (topic) {
245 assert.equal(topic.getMonthNumberFromName('october'), 9);
246 },
247 'oct works': function (topic) {
248 assert.equal(topic.getMonthNumberFromName('oct'), 9);
249 },
250 'november works': function (topic) {
251 assert.equal(topic.getMonthNumberFromName('november'), 10);
252 },
253 'nov works': function (topic) {
254 assert.equal(topic.getMonthNumberFromName('nov'), 10);
255 },
256 'december works': function (topic) {
257 assert.equal(topic.getMonthNumberFromName('december'), 11);
258 },
259 'dec works': function (topic) {
260 assert.equal(topic.getMonthNumberFromName('dec'), 11);
261 }
262 },
263
264 'getWeekNumber works': {
265 'the first week': {
266 topic: function() { return new Date(2013, 0, 1); },
267 'must be 1': function(topic) {
268 assert.strictEqual(topic.getWeekNumber(), 1);
269 }
270 },
271
272 'week 16': {
273 topic: function() { return new Date(2013, 3, 15); },
274 'must be 16': function(topic) {
275 assert.strictEqual(topic.getWeekNumber(), 16);
276 }
277 }
278 },
279
280 'getFullWeekNumber works': {
281 'the first week': {
282 topic: function() { return new Date(2013, 0, 1); },
283 'must be 1': function(topic) {
284 assert.strictEqual(topic.getFullWeekNumber(), "01");
285 }
286 },
287
288 'week 16': {
289 topic: function() { return new Date(2013, 3, 15); },
290 'must be 16': function(topic) {
291 assert.strictEqual(topic.getFullWeekNumber(), "16");
292 }
293 }
294 },
295
296 'getMonthNameFromNumber works': {
297 topic: function () { return Date; },
298 '0 works': function (topic) {
299 assert.equal(topic.getMonthNameFromNumber(0), 'January');
300 },
301 '1 works': function (topic) {
302 assert.equal(topic.getMonthNameFromNumber(1), 'February');
303 },
304 '2 works': function (topic) {
305 assert.equal(topic.getMonthNameFromNumber(2), 'March');
306 },
307 '3 works': function (topic) {
308 assert.equal(topic.getMonthNameFromNumber(3), 'April');
309 },
310 '4 works': function (topic) {
311 assert.equal(topic.getMonthNameFromNumber(4), 'May');
312 },
313 '5 works': function (topic) {
314 assert.equal(topic.getMonthNameFromNumber(5), 'June');
315 },
316 '6 works': function (topic) {
317 assert.equal(topic.getMonthNameFromNumber(6), 'July');
318 },
319 '7 works': function (topic) {
320 assert.equal(topic.getMonthNameFromNumber(7), 'August');
321 },
322 '8 works': function (topic) {
323 assert.equal(topic.getMonthNameFromNumber(8), 'September');
324 },
325 '9 works': function (topic) {
326 assert.equal(topic.getMonthNameFromNumber(9), 'October');
327 },
328 '10 works': function (topic) {
329 assert.equal(topic.getMonthNameFromNumber(10), 'November');
330 },
331 '11 works': function (topic) {
332 assert.equal(topic.getMonthNameFromNumber(11), 'December');
333 }
334 },
335
336 'getMonthAbbrFromNumber works': {
337 topic: function () { return Date; },
338 '0 works': function (topic) {
339 assert.equal(topic.getMonthAbbrFromNumber(0), 'Jan');
340 },
341 '1 works': function (topic) {
342 assert.equal(topic.getMonthAbbrFromNumber(1), 'Feb');
343 },
344 '2 works': function (topic) {
345 assert.equal(topic.getMonthAbbrFromNumber(2), 'Mar');
346 },
347 '3 works': function (topic) {
348 assert.equal(topic.getMonthAbbrFromNumber(3), 'Apr');
349 },
350 '4 works': function (topic) {
351 assert.equal(topic.getMonthAbbrFromNumber(4), 'May');
352 },
353 '5 works': function (topic) {
354 assert.equal(topic.getMonthAbbrFromNumber(5), 'Jun');
355 },
356 '6 works': function (topic) {
357 assert.equal(topic.getMonthAbbrFromNumber(6), 'Jul');
358 },
359 '7 works': function (topic) {
360 assert.equal(topic.getMonthAbbrFromNumber(7), 'Aug');
361 },
362 '8 works': function (topic) {
363 assert.equal(topic.getMonthAbbrFromNumber(8), 'Sep');
364 },
365 '9 works': function (topic) {
366 assert.equal(topic.getMonthAbbrFromNumber(9), 'Oct');
367 },
368 '10 works': function (topic) {
369 assert.equal(topic.getMonthAbbrFromNumber(10), 'Nov');
370 },
371 '11 works': function (topic) {
372 assert.equal(topic.getMonthAbbrFromNumber(11), 'Dec');
373 }
374 },
375
376 'getLastMonthName': {
377 'when it is January': {
378 topic: function () {
379 return new Date("January 15, 2010 12:00:00");
380 },
381 'returns December': function(instance) {
382 assert.equal(instance.getLastMonthName(), 'December');
383 }
384 },
385 'when it is Feburary': {
386 topic: function () {
387 return new Date("Feburary 15, 2010 12:00:00");
388 },
389 'returns January': function(instance) {
390 assert.equal(instance.getLastMonthName(), 'January');
391 }
392 },
393 'when it is March': {
394 topic: function () {
395 return new Date("March 15, 2010 12:00:00");
396 },
397 'returns February': function(instance) {
398 assert.equal(instance.getLastMonthName(), 'February');
399 }
400 },
401 'when it is April': {
402 topic: function () {
403 return new Date("April 15, 2010 12:00:00");
404 },
405 'returns March': function(instance) {
406 assert.equal(instance.getLastMonthName(), 'March');
407 }
408 },
409 'when it is May': {
410 topic: function () {
411 return new Date("May 15, 2010 12:00:00");
412 },
413 'returns April': function(instance) {
414 assert.equal(instance.getLastMonthName(), 'April');
415 }
416 },
417 'when it is June': {
418 topic: function () {
419 return new Date("June 15, 2010 12:00:00");
420 },
421 'returns May': function(instance) {
422 assert.equal(instance.getLastMonthName(), 'May');
423 }
424 },
425 'when it is July': {
426 topic: function () {
427 return new Date("July 15, 2010 12:00:00");
428 },
429 'returns June': function(instance) {
430 assert.equal(instance.getLastMonthName(), 'June');
431 }
432 },
433 'when it is August': {
434 topic: function () {
435 return new Date("August 15, 2010 12:00:00");
436 },
437 'returns July': function(instance) {
438 assert.equal(instance.getLastMonthName(), 'July');
439 }
440 },
441 'when it is September': {
442 topic: function () {
443 return new Date("September 15, 2010 12:00:00");
444 },
445 'returns August': function(instance) {
446 assert.equal(instance.getLastMonthName(), 'August');
447 }
448 },
449 'when it is October': {
450 topic: function () {
451 return new Date("October 15, 2010 12:00:00");
452 },
453 'returns September': function(instance) {
454 assert.equal(instance.getLastMonthName(), 'September');
455 }
456 },
457 'when it is November': {
458 topic: function () {
459 return new Date("November 15, 2010 12:00:00");
460 },
461 'returns October': function(instance) {
462 assert.equal(instance.getLastMonthName(), 'October');
463 }
464 },
465 'when it is December': {
466 topic: function () {
467 return new Date("December 15, 2010 12:00:00");
468 },
469 'returns November': function(instance) {
470 assert.equal(instance.getLastMonthName(), 'November');
471 }
472 }
473 },
474
475 'can add milliseconds': {
476 'adding positive milliseconds works': function () {
477 var topic = Date.today();
478 topic.addMilliseconds(500);
479 assert.equal(topic.getMilliseconds(), 500);
480 },
481 'adding negative milliseconds works': function () {
482 var topic = Date.today();
483 topic.addMilliseconds(500);
484 assert.equal(topic.getMilliseconds(), 500);
485 topic.addMilliseconds(-250);
486 assert.equal(topic.getMilliseconds(), 250);
487 }
488 },
489
490 'can add seconds': {
491 'adding positive seconds works': function () {
492 var topic = Date.today();
493 topic.addSeconds(50);
494 assert.equal(topic.getSeconds(), 50);
495 },
496 'adding negative seconds works': function () {
497 var topic = Date.today();
498 topic.addSeconds(50);
499 assert.equal(topic.getSeconds(), 50);
500 topic.addSeconds(-25);
501 assert.equal(topic.getSeconds(), 25);
502 }
503 },
504
505 'can add minutes': {
506 'adding positive minutes works': function () {
507 var topic = Date.today();
508 topic.addMinutes(50);
509 assert.equal(topic.getMinutes(), 50);
510 },
511 'adding negative minutes works': function () {
512 var topic = Date.today();
513 topic.addMinutes(50);
514 assert.equal(topic.getMinutes(), 50);
515 topic.addMinutes(-25);
516 assert.equal(topic.getMinutes(), 25);
517 }
518 },
519
520 'can add hours': {
521 'adding positive hours works': function () {
522 var topic = Date.today();
523 topic.addHours(5);
524 assert.equal(topic.getHours(), 5);
525 },
526 'adding negative hours works': function () {
527 var topic = Date.today();
528 topic.addHours(5);
529 assert.equal(topic.getHours(), 5);
530 topic.addHours(-2);
531 assert.equal(topic.getHours(), 3);
532 }
533 },
534
535 'can add days': {
536 'adding positive days works': function () {
537 var topic = new Date(2011, 0, 10);
538 topic.addDays(1);
539 assert.equal(topic.getDate(), 11);
540 },
541 'adding positive days works across boundaries': function () {
542 var topic = new Date(2011, 0, 31);
543 topic.addDays(1);
544 assert.equal(topic.getDate(), 1);
545 assert.equal(topic.getMonth(), 1);
546 },
547 'adding negative days works': function () {
548 var topic = new Date(2011, 0, 10);
549 topic.addDays(1);
550 assert.equal(topic.getDate(), 11);
551 topic.addDays(-2);
552 assert.equal(topic.getDate(), 9);
553 }
554 },
555
556 'can add weeks': {
557 'adding positive weeks works': function () {
558 var topic = new Date(2011, 0, 10);
559 topic.addWeeks(1);
560 assert.equal(topic.getDate(), 17);
561 },
562 'adding negative weeks works': function () {
563 var topic = new Date(2011, 0, 10);
564 topic.addWeeks(1);
565 assert.equal(topic.getDate(), 17);
566 topic.addWeeks(-2);
567 assert.equal(topic.getDate(), 3);
568 }
569 },
570
571 'can add months': {
572 'adding positive months works': function () {
573 var topic = new Date(2011, 1, 10);
574 topic.addMonths(1);
575 assert.equal(topic.getMonth(), 2);
576 },
577 'adding negative months works': function () {
578 var topic = new Date(2011, 1, 10);
579 topic.addMonths(1);
580 assert.equal(topic.getMonth(), 2);
581 topic.addMonths(-2);
582 assert.equal(topic.getMonth(), 0);
583 }
584 },
585
586 'can add years': {
587 'adding positive years works': function () {
588 var topic = new Date(2011, 1, 10);
589 topic.addYears(1);
590 assert.equal(topic.getFullYear(), 2012);
591 },
592 'adding negative years works': function () {
593 var topic = new Date(2011, 1, 10);
594 topic.addYears(1);
595 assert.equal(topic.getFullYear(), 2012);
596 topic.addYears(-2);
597 assert.equal(topic.getFullYear(), 2010);
598 }
599 },
600
601 'cannot remove milliseconds': {
602 'removing is not implemented': function () {
603 var topic = Date.today();
604 assert.throws(function() { topic.removeMilliseconds(500) }, Error);
605 }
606 },
607
608 'can remove seconds': {
609 'removing positive seconds works': function () {
610 var topic = Date.today();
611 topic.removeSeconds(50);
612 assert.equal(topic.getSeconds(), 10);
613 },
614 'removing negative seconds works': function () {
615 var topic = Date.today();
616 topic.removeSeconds(50);
617 assert.equal(topic.getSeconds(), 10);
618 topic.removeSeconds(-25);
619 assert.equal(topic.getSeconds(), 35);
620 }
621 },
622
623 'can remove minutes': {
624 'removing positive minutes works': function () {
625 var topic = Date.today();
626 topic.removeMinutes(50);
627 assert.equal(topic.getMinutes(), 10);
628 },
629 'removing negative minutes works': function () {
630 var topic = Date.today();
631 topic.removeMinutes(50);
632 assert.equal(topic.getMinutes(), 10);
633 topic.removeMinutes(-25);
634 assert.equal(topic.getMinutes(), 35);
635 }
636 },
637
638 'can remove hours': {
639 'removing positive hours works': function () {
640 var topic = Date.today();
641 topic.removeHours(5);
642 assert.equal(topic.getHours(), 19);
643 },
644 'removing negative hours works': function () {
645 var topic = Date.today();
646 topic.removeHours(5);
647 assert.equal(topic.getHours(), 19);
648 topic.removeHours(-2);
649 assert.equal(topic.getHours(), 21);
650 }
651 },
652
653 'can remove days': {
654 'removing positive days works': function () {
655 var topic = new Date(2011, 0, 10);
656 topic.removeDays(1);
657 assert.equal(topic.getDate(), 09);
658 },
659 'removing positive days works across boundaries': function () {
660 var topic = new Date(2011, 0, 01);
661 topic.removeDays(1);
662 assert.equal(topic.getDate(), 31);
663 assert.equal(topic.getMonth(), 11);
664 },
665 'removing negative days works': function () {
666 var topic = new Date(2011, 0, 10);
667 topic.removeDays(1);
668 assert.equal(topic.getDate(), 09);
669 topic.removeDays(-2);
670 assert.equal(topic.getDate(), 11);
671 }
672 },
673
674 'can remove weeks': {
675 'removing positive weeks works': function () {
676 var topic = new Date(2011, 0, 10);
677 topic.removeWeeks(1);
678 assert.equal(topic.getDate(), 03);
679 },
680 'removing negative weeks works': function () {
681 var topic = new Date(2011, 0, 10);
682 topic.removeWeeks(1);
683 assert.equal(topic.getDate(), 03);
684 topic.removeWeeks(-2);
685 assert.equal(topic.getDate(), 17);
686 }
687 },
688
689 'can remove months': {
690 'removing positive months works': function () {
691 var topic = new Date(2011, 1, 10);
692 topic.removeMonths(1);
693 assert.equal(topic.getMonth(), 0);
694 },
695 'removing negative months works': function () {
696 var topic = new Date(2011, 1, 10);
697 topic.removeMonths(1);
698 assert.equal(topic.getMonth(), 0);
699 topic.removeMonths(-2);
700 assert.equal(topic.getMonth(), 2);
701 }
702 },
703
704 'can remove years': {
705 'removing positive years works': function () {
706 var topic = new Date(2011, 1, 10);
707 topic.removeYears(1);
708 assert.equal(topic.getFullYear(), 2010);
709 },
710 'removing negative years works': function () {
711 var topic = new Date(2011, 1, 10);
712 topic.removeYears(1);
713 assert.equal(topic.getFullYear(), 2010);
714 topic.removeYears(-2);
715 assert.equal(topic.getFullYear(), 2012);
716 }
717 },
718 'can add weekdays within a week': {
719 'adding positive weekdays': function () {
720 var topic = new Date(2013, 1, 13); //Wed
721 topic.addWeekdays(2);
722 assert.equal(topic.getDay(), 5);
723 },
724 'adding negative weekdays': function () {
725 var topic = new Date(2013, 1, 13); //Wed
726 topic.addWeekdays(-2);
727 assert.equal(topic.getDay(), 1);
728 }
729 },
730 'can add weekdays across one week': {
731 'adding positive weekdays': function () {
732 var wed = new Date(2013, 1, 13);
733 assert.equal(wed.addWeekdays(3).getDate(), 18);
734 var fri = new Date(2013, 1, 15);
735 assert.equal(fri.addWeekdays(1).getDate(), 18);
736 },
737 'adding negative weekdays': function () {
738 var wed = new Date(2013, 1, 13);
739 assert.equal(wed.addWeekdays(-3).getDate(), 8);
740 var mon = new Date(2013, 1, 11);
741 assert.equal(mon.addWeekdays(-1).getDate(), 8);
742 }
743 },
744 'can add weekdays across multiple weeks': {
745 'adding positive weekdays': function () {
746 var tue = new Date(2013, 3, 16);
747 assert.equal(tue.clone().addWeekdays(14).getDate(), 6);
748 assert.equal(tue.clone().addWeekdays(14).getMonth(), 4);
749 var fri = new Date(2013, 3, 19);
750 assert.equal(fri.clone().addWeekdays(14).getDate(), 9);
751 assert.equal(fri.clone().addWeekdays(14).getMonth(), 4);
752 },
753 'adding negative weekdays': function () {
754 var tue = new Date(2013, 3, 16); //Wed
755 assert.equal(tue.clone().addWeekdays(-17).getDate(), 22);
756 assert.equal(tue.clone().addWeekdays(-17).getMonth(), 2);
757 var mon = new Date(2013, 3, 15); //Wed
758 assert.equal(mon.clone().addWeekdays(-17).getDate(), 21);
759 assert.equal(mon.clone().addWeekdays(-17).getMonth(), 2);
760 }
761 },
762 'can add weekdays to a Saturday': {
763 'adding positive weekdays': function () {
764 var sat = new Date(2013, 1, 16);
765 assert.equal(sat.clone().addWeekdays(1).getDate(), 18);
766 assert.equal(sat.clone().addWeekdays(11).getDate(), 4);
767 assert.equal(sat.clone().addWeekdays(31).getDate(), 1);
768 },
769 'adding negative weekdays': function () {
770 var sat = new Date(2013, 1, 16);
771 assert.equal(sat.clone().addWeekdays(-1).getDate(), 15);
772 assert.equal(sat.clone().addWeekdays(-15).getDate(), 28);
773 assert.equal(sat.clone().addWeekdays(-33).getDate(), 2);
774 }
775 },
776 'can add weekdays to a Sunday': {
777 'adding positive weekdays': function () {
778 var sun = new Date(2013, 1, 17);
779 assert.equal(sun.clone().addWeekdays(1).getDate(), 18);
780 assert.equal(sun.clone().addWeekdays(11).getDate(), 4);
781 assert.equal(sun.clone().addWeekdays(31).getDate(), 1);
782
783 },
784 'adding negative weekdays': function () {
785 var sun = new Date(2013, 1, 17);
786 assert.equal(sun.clone().addWeekdays(-1).getDate(), 15);
787 assert.equal(sun.clone().addWeekdays(-15).getDate(), 28);
788 assert.equal(sun.clone().addWeekdays(-33).getDate(), 2);
789 }
790 },
791
792 'can set time to now': {
793 'setting time to now works': function () {
794 var topic = Date.today();
795 topic.setTimeToNow();
796 var now = new Date();
797
798 // hokey, but should be sufficient
799 assert.equal((now.valueOf() - topic.valueOf() < 100), true);
800 }
801 },
802
803 'can clone time': {
804 topic: function () { return new Date(); },
805 'clone works': function (topic) {
806 var clone = topic.clone();
807 assert.equal(clone.valueOf(), topic.valueOf());
808 }
809 },
810
811 'between works': {
812 'between returns true for valid start and end': function () {
813 var today = Date.today();
814 var yesterday = Date.yesterday();
815 var tomorrow = Date.tomorrow();
816 assert.equal(today.between(yesterday, tomorrow), true);
817 },
818 'between returns false for invalid start and end': function () {
819 var today = Date.today();
820 var yesterday = Date.yesterday();
821 var tomorrow = Date.tomorrow();
822 assert.equal(today.between(tomorrow, yesterday), false);
823 }
824 },
825
826 'compareTo works': {
827 topic: function () { return Date.today(); },
828 '-1 for tomorrow': function (topic) {
829 assert.equal(topic.compareTo(Date.tomorrow()), -1);
830 },
831 '1 for yesterday': function (topic) {
832 assert.equal(topic.compareTo(Date.yesterday()), 1);
833 },
834 '0 for today': function (topic) {
835 assert.equal(topic.compareTo(Date.today()), 0);
836 }
837 },
838
839 'isToday works': {
840 topic: function () { return Date.today(); },
841 'true for today': function (topic) {
842 assert.equal(topic.isToday(), true);
843 },
844 'false if not today': function (topic) {
845 assert.equal(Date.yesterday().isToday(), false);
846 }
847 },
848
849 'equals instance works': {
850 topic: function () { return Date.today(); },
851 'true for equal': function (topic) {
852 assert.equal(topic.equals(Date.today()), true);
853 },
854 'false for not equal': function (topic) {
855 assert.equal(topic.equals(Date.tomorrow()), false);
856 }
857 },
858
859 'equalsDay instance works': {
860 topic: function () { return Date.today(); },
861 'true for today': function (topic) {
862 assert.equal(topic.equalsDay(Date.today()), true);
863 },
864 'false for yesterday': function (topic) {
865 assert.equal(topic.equalsDay(Date.yesterday()), false);
866 }
867 },
868
869 'isBefore works': {
870 topic: function () { return Date.today(); },
871 'true for before': function (topic) {
872 assert.equal(topic.isBefore(Date.tomorrow()), true);
873 },
874 'false for after': function (topic) {
875 assert.equal(topic.isBefore(Date.yesterday()), false);
876 }
877 },
878
879 'isAfter works': {
880 topic: function () { return Date.today(); },
881 'false for before': function (topic) {
882 assert.equal(topic.isAfter(Date.tomorrow()), false);
883 },
884 'true for after': function (topic) {
885 assert.equal(topic.isAfter(Date.yesterday()), true);
886 }
887 },
888
889 'isWeekend works': {
890 'false for weekdays': function (topic) {
891 assert.equal(new Date(2013,2,11).isWeekend(), false);
892 assert.equal(new Date(2013,2,12).isWeekend(), false);
893 assert.equal(new Date(2013,2,13).isWeekend(), false);
894 assert.equal(new Date(2013,2,14).isWeekend(), false);
895 assert.equal(new Date(2013,2,15).isWeekend(), false);
896 },
897 'true for weekend': function (topic) {
898 assert.equal(new Date(2013,2,16).isWeekend(), true);
899 assert.equal(new Date(2013,2,17).isWeekend(), true);
900 }
901 },
902
903 'getDaysBetween works': {
904 topic: function () { return Date.today(); },
905 '1 for tomorrow': function (topic) {
906 assert.equal(topic.getDaysBetween(Date.tomorrow()), 1);
907 },
908 '-1 for yesterday': function (topic) {
909 assert.equal(topic.getDaysBetween(Date.yesterday()), -1);
910 },
911 '0 for today': function (topic) {
912 assert.equal(topic.getDaysBetween(Date.today()), 0);
913 }
914 },
915
916 'getDaysBetween works for beginning of year': {
917 topic: function () { return new Date('Jan 1, 2011 01:01:01 GMT'); },
918 'should return 0 for the same day': function (topic) {
919 var date = new Date('Jan 1, 2011 01:01:01 GMT');
920 assert.equal(topic.getDaysBetween(date), 0);
921 },
922 'should return 1 for tomorrow': function (topic) {
923 var date = new Date('Jan 2, 2011 01:01:01 GMT');
924 assert.equal(topic.getDaysBetween(date), 1);
925 }
926 },
927
928 'getMinutesBetween works': {
929 topic: function () { return new Date('Jan 1, 2011 23:31:01 GMT'); },
930 '10 for 10 minutes': function (topic) {
931 assert.equal(topic.getMinutesBetween(new Date('Jan 1, 2011 23:41:01 GMT')), 10);
932 },
933 '-10 for 10 minutes ago': function (topic) {
934 assert.equal(topic.getMinutesBetween(new Date('Jan 1, 2011 23:21:01 GMT')), -10);
935 },
936 '0 for same minute': function (topic) {
937 assert.equal(topic.getMinutesBetween(new Date('Jan 1, 2011 23:31:01 GMT')), 0);
938 },
939 'for time difference that spans days': function (topic) {
940 assert.equal(topic.getMinutesBetween(new Date('Jan 2, 2011 00:01:01 GMT')), 30);
941 }
942 },
943
944 'getSecondsBetween works': {
945 topic: function () { return new Date('Jan 1, 2011 23:31:01 GMT'); },
946 '10 for 10 seconds': function (topic) {
947 assert.equal(topic.getSecondsBetween(new Date('Jan 1, 2011 23:31:11 GMT')), 10);
948 },
949 '-10 for 10 seconds ago': function (topic) {
950 assert.equal(topic.getSecondsBetween(new Date('Jan 1, 2011 23:30:51 GMT')), -10);
951 },
952 '0 for same second': function (topic) {
953 assert.equal(topic.getSecondsBetween(new Date('Jan 1, 2011 23:31:01 GMT')), 0);
954 }
955 },
956
957 'getMillisecondsBetween works': {
958 topic: function () { return new Date(); },
959 '10 for 10 milliseconds': function (topic) {
960 assert.equal(topic.getMillisecondsBetween(new Date(+topic + 10)), 10);
961 },
962 '-10 for 10 milliseconds ago': function (topic) {
963 assert.equal(topic.getMillisecondsBetween(new Date(+topic - 10)), -10);
964 },
965 '0 for same millisecond': function (topic) {
966 assert.equal(topic.getMillisecondsBetween(new Date(+topic)), 0);
967 }
968 },
969
970 'getHoursBetween works': {
971 topic: function () { return new Date('Jan 1, 2011 23:31:01 GMT'); },
972 '1 for 1 hour': function (topic) {
973 assert.equal(topic.getHoursBetween(new Date('Jan 2, 2011 00:31:01 GMT')), 1);
974 },
975 '-1 for 1 hour ago': function (topic) {
976 assert.equal(topic.getHoursBetween(new Date('Jan 1, 2011 22:31:01 GMT')), -1);
977 },
978 '0 for same hour': function (topic) {
979 assert.equal(topic.getHoursBetween(new Date('Jan 1, 2011 23:31:01 GMT')), 0);
980 }
981 },
982
983 'getOrdinalNumber works': {
984 'returns correct day': function () {
985 var date = new Date('02-01-2011');
986 assert.equal(date.getOrdinalNumber(), 32);
987 }
988 },
989
990 'getOrdinalNumber works for january 1st': {
991 'returns correct day': function () {
992 var date = new Date('01-01-2011');
993 assert.equal(date.getOrdinalNumber(), 1);
994 }
995 },
996
997 'getDaysInMonth works': {
998 'january': function (topic) {
999 assert.equal(Date.getDaysInMonth(2011, 0), 31);
1000 },
1001 'february': function (topic) {
1002 assert.equal(Date.getDaysInMonth(2011, 1), 28);
1003 },
1004 'february leap year': function (topic) {
1005 assert.equal(Date.getDaysInMonth(2008, 1), 29);
1006 },
1007 'march': function (topic) {
1008 assert.equal(Date.getDaysInMonth(2011, 2), 31);
1009 },
1010 'april': function (topic) {
1011 assert.equal(Date.getDaysInMonth(2011, 3), 30);
1012 },
1013 'may': function (topic) {
1014 assert.equal(Date.getDaysInMonth(2011, 4), 31);
1015 },
1016 'june': function (topic) {
1017 assert.equal(Date.getDaysInMonth(2011, 5), 30);
1018 },
1019 'july': function (topic) {
1020 assert.equal(Date.getDaysInMonth(2011, 6), 31);
1021 },
1022 'august': function (topic) {
1023 assert.equal(Date.getDaysInMonth(2011, 7), 31);
1024 },
1025 'september': function (topic) {
1026 assert.equal(Date.getDaysInMonth(2011, 8), 30);
1027 },
1028 'october': function (topic) {
1029 assert.equal(Date.getDaysInMonth(2011, 9), 31);
1030 },
1031 'november': function (topic) {
1032 assert.equal(Date.getDaysInMonth(2011, 10), 30);
1033 },
1034 'december': function (topic) {
1035 assert.equal(Date.getDaysInMonth(2011, 11), 31);
1036 }
1037 },
1038
1039 'getMonthsBetween works': {
1040 topic: function() { return new Date( Date.UTC(2013, 1, 28) ); },
1041 'different months': function( topic ) {
1042 var eDate = new Date( Date.UTC(2013, 2, 30) );
1043 assert.equal( topic.getMonthsBetween( eDate ).toFixed( 5 ), 1.09677 );
1044 },
1045 'different months and years': function( topic ) {
1046 var eDate = new Date( Date.UTC(2014, 3, 4) );
1047 assert.equal( topic.getMonthsBetween( eDate ).toFixed( 5 ), 13.26236 );
1048 },
1049 'same month': function( topic ) {
1050 var sDate = new Date( Date.UTC(2013, 1, 1) );
1051 assert.equal( sDate.getMonthsBetween( topic ).toFixed( 5 ), 1 );
1052 },
1053 'same date': function( topic ) {
1054 var sameDate = new Date( topic.getTime() );
1055 assert.equal( topic.getMonthsBetween( sameDate ).toFixed( 5 ), 0.03571 );
1056 },
1057 'same day and month but different years': function( topic ) {
1058 var differentYear = new Date( Date.UTC(2014, 1, 28) );
1059 assert.equal( topic.getMonthsBetween( differentYear ).toFixed( 5 ), 12.03571 );
1060 }
1061 }
1062
1063}).export(module);