Skip to content

Commit c53d15c

Browse files
committed
initial import of code
1 parent 6042d78 commit c53d15c

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
© 2011 by Jerry Sievert
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

lib/date-utils.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
(function () {
2+
3+
var origParse = Date.parse;
4+
Date.parse = function (date) {
5+
var timestamp = origParse(date), minutesOffset = 0, match;
6+
if (isNaN(timestamp) && (match = /^(\d{4}|[+\-]\d{6})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3,}))?)?(?:(Z)|([+\-])(\d{2})(?::?(\d{2}))?))?/.exec(date))) {
7+
if (match[8] !== 'Z') {
8+
minutesOffset = +match[10] * 60 + (+match[11]);
9+
10+
if (match[9] === '+') {
11+
minutesOffset = 0 - minutesOffset;
12+
}
13+
}
14+
15+
timestamp = Date.UTC(+match[1], +match[2] - 1, +match[3], +match[4], +match[5] + minutesOffset, +match[6], +match[7].substr(0, 3));
16+
}
17+
18+
return timestamp;
19+
};
20+
21+
var months_abbr = [
22+
'Jan',
23+
'Feb',
24+
'Mar',
25+
'Apr',
26+
'May',
27+
'Jun',
28+
'Jul',
29+
'Aug',
30+
'Sep',
31+
'Oct',
32+
'Nov',
33+
'Dec'
34+
];
35+
36+
if (Date.prototype.getMonthAbbr === undefined) {
37+
Date.prototype.getMonthAbbr = function() {
38+
return months_abbr[this.getMonth()];
39+
}
40+
}
41+
42+
var months = [
43+
'January',
44+
'February',
45+
'March',
46+
'April',
47+
'May',
48+
'June',
49+
'July',
50+
'August',
51+
'September',
52+
'October',
53+
'November',
54+
'December'
55+
];
56+
57+
if (Date.prototype.getMonthName === undefined) {
58+
Date.prototype.getMonthName = function() {
59+
return months[this.getMonth()];
60+
}
61+
}
62+
63+
function pad(str, length) {
64+
str = String(str);
65+
while (str.length < length) {
66+
str = '0' + str;
67+
}
68+
return str;
69+
}
70+
71+
if (Date.prototype.toCLFString === undefined) {
72+
Date.prototype.toCLFString = function() {
73+
var tz = pad(Math.abs(this.getTimezoneOffset() / 0.6), 4);
74+
if (this.getTimezoneOffset() > 0) {
75+
tz = '-' + tz;
76+
}
77+
78+
return pad(this.getDate(), 2) + '/' + this.getMonthAbbr() + '/' + this.getFullYear() +
79+
':' + pad(this.getHours(), 2) + ':' + pad(this.getMinutes(), 2) + ':' + pad(this.getSeconds(), 2) + ' ' + tz;
80+
}
81+
}
82+
83+
84+
}());

test/date-format.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 object 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 object 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+
}).run();

test/date-parse.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var vows = require('vows');
2+
var assert = require('assert');
3+
4+
require('../lib/date-utils.js');
5+
6+
vows.describe('Date Parse').addBatch({
7+
'can instantiate milliseconds': {
8+
topic: function () { return new Date(123456789) },
9+
'returns a valid date object': function (date) {
10+
assert.ok(date);
11+
},
12+
'returns a correct value': function (date) {
13+
assert.equal(date.valueOf(), 123456789);
14+
}
15+
},
16+
17+
'can instantiate string': {
18+
topic: function () { return new Date('Jan 1, 2011 01:01:01 GMT') },
19+
'returns a valid date object': function (date) {
20+
assert.ok(date);
21+
},
22+
'returns a correct value': function (date) {
23+
assert.equal(date.valueOf(), 1293843661000);
24+
}
25+
},
26+
27+
'can instantiate arguments': {
28+
topic: function () { return new Date(2011, 1, 1, 1, 1, 1, 0) },
29+
'returns a valid date object': function (date) {
30+
assert.ok(date);
31+
}
32+
},
33+
34+
'can parse normal date': {
35+
topic: function () { return Date.parse('Jan 1, 2011 01:01:01 GMT') },
36+
'returns a correct value': function (milli) {
37+
assert.equal(milli, 1293843661000);
38+
}
39+
},
40+
41+
'can parse ISO-8601': {
42+
topic: function () { return Date.parse('2011-01-01T01:01:01Z') },
43+
'returns a correct value': function (milli) {
44+
assert.equal(milli, 1293843661000);
45+
}
46+
}
47+
48+
}).run();

0 commit comments

Comments
 (0)