Skip to content

Commit

Permalink
fix ordinal number, add some tests to validate, general test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jsievert committed Aug 7, 2011
1 parent 6560678 commit b7ce339
Show file tree
Hide file tree
Showing 5 changed files with 768 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
67 changes: 67 additions & 0 deletions test/date-format-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var vows = require('vows');
var assert = require('assert');

require('../lib/date-utils.js');

function pad(str, length) {
str = String(str);
while (str.length < length) {
str = '0' + str;
}
return str;
}

vows.describe('Date Format').addBatch({
'can return month abbreviations as static method': {
topic: function () { return new Date(123456789); },
'returns the correct abbreviated month': function (date) {
assert.equal(date.getMonthAbbr(), 'Jan');
}
},

'can return month as static method': {
topic: function () { return new Date(123456789); },
'returns the correct month': function (date) {
assert.equal(date.getMonthAbbr(), 'Jan');
}
},

'can return common log formatted string': {
topic: function () { return new Date(Date.UTC(2011, 0, 1, 1, 1, 1, 0)); },
'returns the correct clf string': function (date) {
var tz = pad(Math.abs(date.getTimezoneOffset() / 0.6), 4);
if (date.getTimezoneOffset() > 0) {
tz = '-' + tz;
}

date = new Date(date.valueOf() + date.getTimezoneOffset() * 60000);
assert.equal(date.toCLFString(), '01/Jan/2011:01:01:01 ' + tz);
}
},

'can return correctly formatted toFormat': {
topic: function () { var topic = new Date(2011, 0, 1);
topic.addHours(13)
.addMinutes(11)
.addSeconds(41);
return topic;
},
'returns correctly': function (date) {
assert.equal(date.toFormat('YYYY'), '2011');
assert.equal(date.toFormat('YY'), '11');
assert.equal(date.toFormat('MM'), '01');
assert.equal(date.toFormat('DD'), '01');
assert.equal(date.toFormat('HH'), '01');
assert.equal(date.toFormat('HH24'), '13');
assert.equal(date.toFormat('MI'), '11');
assert.equal(date.toFormat('SS'), '41');
}
},

'can return database formatted string': {
topic: function () { return new Date(Date.UTC(2011, 0, 1, 1, 1, 1, 0)); },
'returns the correct database string': function (date) {
assert.equal(date.toDBString(), '2011-01-01 01:01:01');
}
}
}).export(module);
60 changes: 60 additions & 0 deletions test/date-new-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var vows = require('vows');
var assert = require('assert');

require('../lib/date-utils.js');

vows.describe('Date New').addBatch({
'can return a new object from today static method': {
topic: function () { return Date.today(); },
'returns the correct time': function (date) {
var compare = new Date().clearTime();
assert.equal(date.valueOf(), compare.valueOf());
}
},

'clearTime() works': {
topic: function() { return new Date().clearTime(); },
'returns the correct value': function (date) {
var compare = new Date();
compare.setHours(0);
compare.setMinutes(0);
compare.setSeconds(0);
compare.setMilliseconds(0);

assert.equal(date.valueOf(), compare.valueOf());
}
},

'today() works': {
topic: function() {
return Date.today();
},
'returns the correct value': function(date) {
var compare = new Date().clearTime();
assert.equal(date.valueOf(), compare.valueOf());
}
},

'yesterday() works': {
topic: function() {
return Date.yesterday();
},
'returns the correct value': function(date) {
var compare = new Date().clearTime();
compare.setSeconds(compare.getSeconds() - 86400);
assert.equal(date.valueOf(), compare.valueOf());
}
},

'tomorrow() works': {
topic: function() {
return Date.tomorrow();
},
'returns the correct value': function(date) {
var compare = new Date().clearTime();
compare.setSeconds(compare.getSeconds() + 86400);
assert.equal(date.valueOf(), compare.valueOf());
}
}

}).export(module);
122 changes: 122 additions & 0 deletions test/date-parse-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
var vows = require('vows');
var assert = require('assert');

require('../lib/date-utils.js');
//getTimezoneOffset returns number of minutes
//converting to milliseconds
var offset = (new Date().getTimezoneOffset()) * 60 * 1000;

vows.describe('Date Parse').addBatch({
'can instantiate milliseconds': {
topic: function () { return new Date(123456789); },
'returns a valid date object': function (date) {
assert.ok(date);
},
'returns a correct value': function (date) {
assert.equal(date.valueOf(), 123456789);
}
},

'can instantiate string': {
topic: function () { return new Date('Jan 1, 2011 01:01:01 GMT'); },
'returns a valid date object': function (date) {
assert.ok(date);
},
'returns a correct value': function (date) {
assert.equal(date.valueOf(), 1293843661000);
}
},

'can instantiate arguments': {
topic: function () { return new Date(2011, 1, 1, 1, 1, 1, 0); },
'returns a valid date object': function (date) {
assert.ok(date);
}
},

'can parse normal date': {
topic: function () { return Date.parse('Jan 1, 2011 01:01:01 GMT'); },
'returns a correct value': function (milli) {
assert.equal(milli, 1293843661000);
}
},
'can parse ISO-8601': {
topic: function () { return Date.parse('2011-01-01T01:01:01Z'); },
'returns a correct value': function (milli) {
assert.equal(milli, 1293843661000);
}
},
'can parse custom format': {
topic: function () {
return Date.parse('20/6/2011 8:30', 'd/M/y H:m');
},
'returns a correct value': function (milli) {
assert.equal(milli, 1308558600000 + offset);
}
},
'parse custom format with full month name': {
topic: function () {
return Date.parse('June 20, 2011 08:30:00', 'MMM d, y H:m:s');
},
'returns a correct value': function (milli) {
assert.equal(milli, 1308558600000 + offset);
}
},
'parse custom format with abbr month name': {
topic: function () {
return Date.parse('Jun 20, 2011 08:30:00', 'MMM d, y H:m:s');
},
'returns a correct value': function (milli) {
assert.equal(milli, 1308558600000 + offset);
}
},
'parse custom format with 12 hr clock': {
topic: function () {
return Date.parse('June 20, 2011 08:30:00AM', 'MMM d, y h:m:sa');
},
'returns a correct value': function (milli) {
assert.equal(milli, 1308558600000 + offset);
}
},
'parse mysql date format': {
topic: function () {
return Date.parse('2011-06-20 08:30:00', 'y-M-d H:m:s');
},
'returns a correct value': function (milli) {
assert.equal(milli, 1308558600000 + offset);
}
},
'parse us date format w/o time': {
topic: function () {
return Date.parse('6/20/2011', 'M/d/y');
},
'returns a correct value': function (milli) {
assert.equal(milli, 1308528000000 + offset);
}
},
'parse us date format with time': {
topic: function () {
return Date.parse('6/20/2011 00:00:01', 'M/d/y H:m:s');
},
'returns a correct value': function (milli) {
assert.equal(milli, 1308528001000 + offset);
}
},
'parse uk date format w/o time': {
topic: function () {
return Date.parse('20/6/2011', 'd/M/y');
},
'returns a correct value': function (milli) {
assert.equal(milli, 1308528000000 + offset);
}
},
'parse uk date format with time': {
topic: function () {

return Date.parse('20/6/2011 00:00:01', 'd/M/y H:m:s');
},
'returns a correct value': function (milli) {
assert.equal(milli, 1308528001000 + offset);
}
}
}).export(module);
Loading

0 comments on commit b7ce339

Please sign in to comment.