Skip to content

Commit b7ce339

Browse files
committed
fix ordinal number, add some tests to validate, general test cleanup
1 parent 6560678 commit b7ce339

File tree

5 files changed

+768
-0
lines changed

5 files changed

+768
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

test/date-format-test.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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, 0, 1);
44+
topic.addHours(13)
45+
.addMinutes(11)
46+
.addSeconds(41);
47+
return topic;
48+
},
49+
'returns correctly': function (date) {
50+
assert.equal(date.toFormat('YYYY'), '2011');
51+
assert.equal(date.toFormat('YY'), '11');
52+
assert.equal(date.toFormat('MM'), '01');
53+
assert.equal(date.toFormat('DD'), '01');
54+
assert.equal(date.toFormat('HH'), '01');
55+
assert.equal(date.toFormat('HH24'), '13');
56+
assert.equal(date.toFormat('MI'), '11');
57+
assert.equal(date.toFormat('SS'), '41');
58+
}
59+
},
60+
61+
'can return database formatted string': {
62+
topic: function () { return new Date(Date.UTC(2011, 0, 1, 1, 1, 1, 0)); },
63+
'returns the correct database string': function (date) {
64+
assert.equal(date.toDBString(), '2011-01-01 01:01:01');
65+
}
66+
}
67+
}).export(module);

test/date-new-test.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var vows = require('vows');
2+
var assert = require('assert');
3+
4+
require('../lib/date-utils.js');
5+
6+
vows.describe('Date New').addBatch({
7+
'can return a new object from today static method': {
8+
topic: function () { return Date.today(); },
9+
'returns the correct time': function (date) {
10+
var compare = new Date().clearTime();
11+
assert.equal(date.valueOf(), compare.valueOf());
12+
}
13+
},
14+
15+
'clearTime() works': {
16+
topic: function() { return new Date().clearTime(); },
17+
'returns the correct value': function (date) {
18+
var compare = new Date();
19+
compare.setHours(0);
20+
compare.setMinutes(0);
21+
compare.setSeconds(0);
22+
compare.setMilliseconds(0);
23+
24+
assert.equal(date.valueOf(), compare.valueOf());
25+
}
26+
},
27+
28+
'today() works': {
29+
topic: function() {
30+
return Date.today();
31+
},
32+
'returns the correct value': function(date) {
33+
var compare = new Date().clearTime();
34+
assert.equal(date.valueOf(), compare.valueOf());
35+
}
36+
},
37+
38+
'yesterday() works': {
39+
topic: function() {
40+
return Date.yesterday();
41+
},
42+
'returns the correct value': function(date) {
43+
var compare = new Date().clearTime();
44+
compare.setSeconds(compare.getSeconds() - 86400);
45+
assert.equal(date.valueOf(), compare.valueOf());
46+
}
47+
},
48+
49+
'tomorrow() works': {
50+
topic: function() {
51+
return Date.tomorrow();
52+
},
53+
'returns the correct value': function(date) {
54+
var compare = new Date().clearTime();
55+
compare.setSeconds(compare.getSeconds() + 86400);
56+
assert.equal(date.valueOf(), compare.valueOf());
57+
}
58+
}
59+
60+
}).export(module);

test/date-parse-test.js

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
var vows = require('vows');
2+
var assert = require('assert');
3+
4+
require('../lib/date-utils.js');
5+
//getTimezoneOffset returns number of minutes
6+
//converting to milliseconds
7+
var offset = (new Date().getTimezoneOffset()) * 60 * 1000;
8+
9+
vows.describe('Date Parse').addBatch({
10+
'can instantiate milliseconds': {
11+
topic: function () { return new Date(123456789); },
12+
'returns a valid date object': function (date) {
13+
assert.ok(date);
14+
},
15+
'returns a correct value': function (date) {
16+
assert.equal(date.valueOf(), 123456789);
17+
}
18+
},
19+
20+
'can instantiate string': {
21+
topic: function () { return new Date('Jan 1, 2011 01:01:01 GMT'); },
22+
'returns a valid date object': function (date) {
23+
assert.ok(date);
24+
},
25+
'returns a correct value': function (date) {
26+
assert.equal(date.valueOf(), 1293843661000);
27+
}
28+
},
29+
30+
'can instantiate arguments': {
31+
topic: function () { return new Date(2011, 1, 1, 1, 1, 1, 0); },
32+
'returns a valid date object': function (date) {
33+
assert.ok(date);
34+
}
35+
},
36+
37+
'can parse normal date': {
38+
topic: function () { return Date.parse('Jan 1, 2011 01:01:01 GMT'); },
39+
'returns a correct value': function (milli) {
40+
assert.equal(milli, 1293843661000);
41+
}
42+
},
43+
'can parse ISO-8601': {
44+
topic: function () { return Date.parse('2011-01-01T01:01:01Z'); },
45+
'returns a correct value': function (milli) {
46+
assert.equal(milli, 1293843661000);
47+
}
48+
},
49+
'can parse custom format': {
50+
topic: function () {
51+
return Date.parse('20/6/2011 8:30', 'd/M/y H:m');
52+
},
53+
'returns a correct value': function (milli) {
54+
assert.equal(milli, 1308558600000 + offset);
55+
}
56+
},
57+
'parse custom format with full month name': {
58+
topic: function () {
59+
return Date.parse('June 20, 2011 08:30:00', 'MMM d, y H:m:s');
60+
},
61+
'returns a correct value': function (milli) {
62+
assert.equal(milli, 1308558600000 + offset);
63+
}
64+
},
65+
'parse custom format with abbr month name': {
66+
topic: function () {
67+
return Date.parse('Jun 20, 2011 08:30:00', 'MMM d, y H:m:s');
68+
},
69+
'returns a correct value': function (milli) {
70+
assert.equal(milli, 1308558600000 + offset);
71+
}
72+
},
73+
'parse custom format with 12 hr clock': {
74+
topic: function () {
75+
return Date.parse('June 20, 2011 08:30:00AM', 'MMM d, y h:m:sa');
76+
},
77+
'returns a correct value': function (milli) {
78+
assert.equal(milli, 1308558600000 + offset);
79+
}
80+
},
81+
'parse mysql date format': {
82+
topic: function () {
83+
return Date.parse('2011-06-20 08:30:00', 'y-M-d H:m:s');
84+
},
85+
'returns a correct value': function (milli) {
86+
assert.equal(milli, 1308558600000 + offset);
87+
}
88+
},
89+
'parse us date format w/o time': {
90+
topic: function () {
91+
return Date.parse('6/20/2011', 'M/d/y');
92+
},
93+
'returns a correct value': function (milli) {
94+
assert.equal(milli, 1308528000000 + offset);
95+
}
96+
},
97+
'parse us date format with time': {
98+
topic: function () {
99+
return Date.parse('6/20/2011 00:00:01', 'M/d/y H:m:s');
100+
},
101+
'returns a correct value': function (milli) {
102+
assert.equal(milli, 1308528001000 + offset);
103+
}
104+
},
105+
'parse uk date format w/o time': {
106+
topic: function () {
107+
return Date.parse('20/6/2011', 'd/M/y');
108+
},
109+
'returns a correct value': function (milli) {
110+
assert.equal(milli, 1308528000000 + offset);
111+
}
112+
},
113+
'parse uk date format with time': {
114+
topic: function () {
115+
116+
return Date.parse('20/6/2011 00:00:01', 'd/M/y H:m:s');
117+
},
118+
'returns a correct value': function (milli) {
119+
assert.equal(milli, 1308528001000 + offset);
120+
}
121+
}
122+
}).export(module);

0 commit comments

Comments
 (0)