1 | var vows = require('vows');
|
2 | var assert = require('assert');
|
3 |
|
4 | require('../lib/date-utils.js');
|
5 |
|
6 | function pad(str, length) {
|
7 | str = String(str);
|
8 | while (str.length < length) {
|
9 | str = '0' + str;
|
10 | }
|
11 | return str;
|
12 | }
|
13 |
|
14 | vows.describe('Date Format').addBatch({
|
15 | 'can return month abbreviations as static method': {
|
16 | topic: function () { return new Date(123456789); },
|
17 | 'returns the correct abbreviated month': function (date) {
|
18 | assert.equal(date.getMonthAbbr(), 'Jan');
|
19 | }
|
20 | },
|
21 |
|
22 | 'can return month as static method': {
|
23 | topic: function () { return new Date(123456789); },
|
24 | 'returns the correct month': function (date) {
|
25 | assert.equal(date.getMonthAbbr(), 'Jan');
|
26 | }
|
27 | },
|
28 |
|
29 | 'can return common log formatted string': {
|
30 | topic: function () { return new Date(Date.UTC(2011, 0, 1, 1, 1, 1, 0)); },
|
31 | 'returns the correct clf string': function (date) {
|
32 | var tz = pad(Math.abs(date.getTimezoneOffset() / 0.6), 4);
|
33 | if (date.getTimezoneOffset() > 0) {
|
34 | tz = '-' + tz;
|
35 | }
|
36 |
|
37 | date = new Date(date.valueOf() + date.getTimezoneOffset() * 60000);
|
38 | assert.equal(date.toCLFString(), '01/Jan/2011:01:01:01 ' + tz);
|
39 | }
|
40 | },
|
41 |
|
42 | 'can return correctly formatted toFormat': {
|
43 | topic: function () { var topic = new Date(2011, 2, 1);
|
44 | topic.addHours(13)
|
45 | .addMinutes(11)
|
46 | .addSeconds(41)
|
47 | .addMilliseconds(23);
|
48 | return topic;
|
49 | },
|
50 | 'returns correctly': function (date) {
|
51 | assert.equal(date.toFormat('YYYY'), '2011');
|
52 | assert.equal(date.toFormat('YY'), '11');
|
53 | assert.equal(date.toFormat('M'), '3');
|
54 | assert.equal(date.toFormat('MM'), '03');
|
55 | assert.equal(date.toFormat('MMM'), 'Mar');
|
56 | assert.equal(date.toFormat('MMMM'), 'March');
|
57 | assert.equal(date.toFormat('D'), '1');
|
58 | assert.equal(date.toFormat('DD'), '01');
|
59 | assert.equal(date.toFormat('DDD'), 'Tue');
|
60 | assert.equal(date.toFormat('DDDD'), 'Tuesday');
|
61 | assert.equal(date.toFormat('H'), '1');
|
62 | assert.equal(date.toFormat('HH'), '01');
|
63 | assert.equal(date.toFormat('HH24'), '13');
|
64 | assert.equal(date.toFormat('MI'), '11');
|
65 | assert.equal(date.toFormat('SS'), '41');
|
66 | assert.equal(date.toFormat('LL'), '023');
|
67 | },
|
68 |
|
69 | 'returns complex formats correct': function (date) {
|
70 | assert.equal(date.toFormat('DDD-MMM-YYYY'), 'Tue-Mar-2011');
|
71 | assert.equal(date.toFormat('DD/MM/YY'), '01/03/11');
|
72 | assert.equal(date.toFormat('D:M:YY'), '1:3:11');
|
73 | }
|
74 | },
|
75 |
|
76 | 'can return correctly formatted toUTCFormat': {
|
77 | topic: function () {
|
78 | return topic = new Date(Date.UTC(2011, 2, 1, 13, 11, 41, 23));
|
79 | },
|
80 |
|
81 | 'returns correctly': function (date) {
|
82 | assert.equal(date.toUTCFormat('YYYY'), '2011');
|
83 | assert.equal(date.toUTCFormat('YY'), '11');
|
84 | assert.equal(date.toUTCFormat('M'), '3');
|
85 | assert.equal(date.toUTCFormat('MM'), '03');
|
86 | assert.equal(date.toUTCFormat('MMM'), 'Mar');
|
87 | assert.equal(date.toUTCFormat('MMMM'), 'March');
|
88 | assert.equal(date.toUTCFormat('D'), '1');
|
89 | assert.equal(date.toUTCFormat('DD'), '01');
|
90 | assert.equal(date.toUTCFormat('DDD'), 'Tue');
|
91 | assert.equal(date.toUTCFormat('DDDD'), 'Tuesday');
|
92 | assert.equal(date.toUTCFormat('H'), '1');
|
93 | assert.equal(date.toUTCFormat('HH'), '01');
|
94 | assert.equal(date.toUTCFormat('HH24'), '13');
|
95 | assert.equal(date.toUTCFormat('MI'), '11');
|
96 | assert.equal(date.toUTCFormat('SS'), '41');
|
97 | assert.equal(date.toUTCFormat('LL'), '023');
|
98 | },
|
99 |
|
100 | 'returns complex formats correct': function (date) {
|
101 | assert.equal(date.toUTCFormat('DDD-MMM-YYYY'), 'Tue-Mar-2011');
|
102 | assert.equal(date.toUTCFormat('DD/MM/YY'), '01/03/11');
|
103 | assert.equal(date.toUTCFormat('D:M:YY'), '1:3:11');
|
104 | }
|
105 | },
|
106 |
|
107 | 'can return correct months': {
|
108 | topic: function () { return new Date(2011, 0, 1); },
|
109 | 'returns Jan correcty': function (date) { assert.equal(date.addMonths(0).toFormat('MMM'), 'Jan'); },
|
110 | 'returns Feb correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Feb'); },
|
111 | 'returns Mar correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Mar'); },
|
112 | 'returns Apr correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Apr'); },
|
113 | 'returns May correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'May'); },
|
114 | 'returns Jun correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Jun'); },
|
115 | 'returns Jul correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Jul'); },
|
116 | 'returns Aug correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Aug'); },
|
117 | 'returns Sep correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Sep'); },
|
118 | 'returns Oct correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Oct'); },
|
119 | 'returns Nov correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Nov'); },
|
120 | 'returns Dec correcty': function (date) { assert.equal(date.addMonths(1).toFormat('MMM'), 'Dec'); },
|
121 | },
|
122 |
|
123 | 'can return database formatted string': {
|
124 | topic: function () { return new Date(Date.UTC(2011, 0, 1, 1, 1, 1, 0)); },
|
125 | 'returns the correct database string': function (date) {
|
126 | assert.equal(date.toDBString(), '2011-01-01 01:01:01');
|
127 | }
|
128 | },
|
129 |
|
130 | 'when passing an argument to toYMD': {
|
131 | topic: function () { return new Date(2011, 0, 1, 1, 1, 1, 0).toYMD('-'); },
|
132 | 'the correct string is returned': function (topic) {
|
133 | assert.equal(topic, '2011-01-01');
|
134 | }
|
135 | },
|
136 |
|
137 | 'when passing an empty argument to toYMD': {
|
138 | topic: function () { return new Date(2011, 0, 1, 1, 1, 1, 0).toYMD(''); },
|
139 | 'the correct string is returned': function (topic) {
|
140 | assert.equal(topic, '20110101');
|
141 | }
|
142 | },
|
143 |
|
144 | 'when passing no argument to toYMD': {
|
145 | topic: function () { return new Date(2011, 0, 1, 1, 1, 1, 0).toYMD(); },
|
146 | 'the correct default is chosen and the string is returned': function (topic) {
|
147 | assert.equal(topic, '2011-01-01');
|
148 | }
|
149 | }
|
150 |
|
151 | }).export(module);
|