Skip to content

Commit c13f34a

Browse files
committed
Merge pull request brianc#432 from brianc/pull/425
Fix for early dates
2 parents b38d60d + a2d0ab2 commit c13f34a

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

lib/types/textParsers.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ var parseDate = function(isoDate) {
1717
return new Date(isoDate);
1818
}
1919
}
20-
var year = match[1];
20+
var isBC = /BC$/.test(isoDate);
21+
var _year = parseInt(match[1], 10);
22+
var isFirstCentury = (_year > 0) && (_year < 100);
23+
var year = (isBC ? "-" : "") + match[1];
24+
2125
var month = parseInt(match[2],10)-1;
2226
var day = match[3];
2327
var hour = parseInt(match[4],10);
@@ -37,6 +41,7 @@ var parseDate = function(isoDate) {
3741
var tZone = /([Z|+\-])(\d{2})?:?(\d{2})?/.exec(isoDate.split(' ')[1]);
3842
//minutes to adjust for timezone
3943
var tzAdjust = 0;
44+
var date;
4045
if(tZone) {
4146
var type = tZone[1];
4247
switch(type) {
@@ -53,13 +58,18 @@ var parseDate = function(isoDate) {
5358
}
5459

5560
var utcOffset = Date.UTC(year, month, day, hour, min, seconds, mili);
56-
return new Date(utcOffset - (tzAdjust * 60* 1000));
61+
62+
date = new Date(utcOffset - (tzAdjust * 60* 1000));
5763
}
5864
//no timezone information
5965
else {
60-
return new Date(year, month, day, hour, min, seconds, mili);
66+
date = new Date(year, month, day, hour, min, seconds, mili);
6167
}
6268

69+
if (isFirstCentury) {
70+
date.setUTCFullYear(year);
71+
}
72+
return date;
6373
};
6474

6575
var parseBool = function(val) {

test/integration/client/type-coercion-tests.js

+20
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ test("timestampz round trip", function() {
150150
client.on('drain', client.end.bind(client));
151151
});
152152

153+
if(!helper.config.binary) {
154+
test('early AD & BC date', function() {
155+
var client = helper.client();
156+
client.on('error', function(err) {
157+
console.log(err);
158+
client.end();
159+
});
160+
161+
client.query('SELECT $1::TIMESTAMPTZ as when', ["0062-03-08 14:32:00"], assert.success(function(res) {
162+
assert.equal(res.rows[0].when.getFullYear(), 62);
163+
}))
164+
165+
client.query('SELECT $1::TIMESTAMPTZ as when', ["0062-03-08 14:32:00 BC"], assert.success(function(res) {
166+
assert.equal(res.rows[0].when.getFullYear(), -62);
167+
}))
168+
169+
client.on('drain', client.end.bind(client));
170+
})
171+
}
172+
153173
helper.pg.connect(helper.config, assert.calls(function(err, client, done) {
154174
assert.isNull(err);
155175
client.query('select null as res;', assert.calls(function(err, res) {

0 commit comments

Comments
 (0)