Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle non-standard HH:mm:ss, that occur with old times. #8

Merged
merged 1 commit into from Jul 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/textParsers.js
Expand Up @@ -39,28 +39,32 @@ var parseDate = function(isoDate) {
//Z (UTC)
//-05
//+06:30
var tZone = /([Z|+\-])(\d{2})?:?(\d{2})?/.exec(isoDate.split(' ')[1]);
var tZone = /([Z|+\-])(\d{2})?:?(\d{2})?:?(\d{2})?/.exec(isoDate.split(' ')[1]);
//minutes to adjust for timezone
var tzAdjust = 0;
var tzSign = 1;
var date;
if(tZone) {
var type = tZone[1];
switch(type) {
case 'Z':
break;
case '-':
tzAdjust = -(((parseInt(tZone[2],10)*60)+(parseInt(tZone[3]||0,10))));
break;
tzSign = -1;
case '+':
tzAdjust = (((parseInt(tZone[2],10)*60)+(parseInt(tZone[3]||0,10))));
tzAdjust = tzSign * (
(parseInt(tZone[2], 10) * 3600) +
(parseInt(tZone[3] || 0, 10) * 60) +
(parseInt(tZone[4] || 0, 10))
);
break;
default:
throw new Error("Unidentifed tZone part " + type);
}

var utcOffset = Date.UTC(year, month, day, hour, min, seconds, mili);

date = new Date(utcOffset - (tzAdjust * 60* 1000));
date = new Date(utcOffset - (tzAdjust * 1000));
}
//no timezone information
else {
Expand Down
100 changes: 72 additions & 28 deletions test/dates.js
@@ -1,41 +1,85 @@
var parse = require('../').getTypeParser(1114, 'text');
var assert = require('./assert')
/*global describe, it*/
var parse = require("../").getTypeParser(1114, "text");
var assert = require("./assert");

//some of these tests might be redundant
//they were ported over from node-postgres
//regardles: more tests is a good thing, right? :+1:
describe('date parser', function() {
it('parses date', function() {
assert.equal(parse("2010-12-11 09:09:04").toString(),new Date("2010-12-11 09:09:04").toString());
describe("date parser", function() {
it("parses date", function() {
assert.equal(
parse("2010-12-11 09:09:04").toString(),
new Date("2010-12-11 09:09:04").toString()
);
});

var testForMs = function(part, expected) {
var dateString = "2010-01-01 01:01:01" + part;
it('testing for correcting parsing of ' + dateString, function() {
it("testing for correcting parsing of " + dateString, function() {
var ms = parse(dateString).getMilliseconds();
assert.equal(ms, expected)
})
}

testForMs('.1', 100);
testForMs('.01', 10);
testForMs('.74', 740);

it("dates without timezones", function() {
var actual = "2010-12-11 09:09:04.1";
var expected = JSON.stringify(new Date(2010,11,11,9,9,4,100))
assert.equal(JSON.stringify(parse(actual)),expected);
});
assert.equal(ms, expected);
});
};

it("with timezones", function() {
var actual = "2011-01-23 22:15:51.28-06";
var expected = "\"2011-01-24T04:15:51.280Z\"";
assert.equal(JSON.stringify(parse(actual)),expected);
});
testForMs(".1", 100);
testForMs(".01", 10);
testForMs(".74", 740);

describe("parses dates with", function () {

it("no timezones", function() {
var actual = "2010-12-11 09:09:04.1";
var expected = JSON.stringify(new Date(2010,11,11,9,9,4,100));
assert.equal(JSON.stringify(parse(actual)),expected);
});

it("huge millisecond value", function() {
var actual = "2011-01-23 22:15:51.280843-06";
var expected = "\"2011-01-24T04:15:51.280Z\"";
assert.equal(JSON.stringify(parse(actual)),expected);
});

it("Zulu time offset", function() {
var actual = "2011-01-23 22:15:51Z";
var expected = "\"2011-01-23T22:15:51.000Z\"";
assert.equal(JSON.stringify(parse(actual)),expected);
});

it("with huge millisecond value", function() {
var actual = "2011-01-23 22:15:51.280843-06";
var expected = "\"2011-01-24T04:15:51.280Z\"";
assert.equal(JSON.stringify(parse(actual)),expected);
it("positive hour offset", function() {
var actual = "2011-01-23 10:15:51+04";
var expected = "\"2011-01-23T06:15:51.000Z\"";
assert.equal(JSON.stringify(parse(actual)),expected);
});

it("negative hour offset", function() {
var actual = "2011-01-23 10:15:51-04";
var expected = "\"2011-01-23T14:15:51.000Z\"";
assert.equal(JSON.stringify(parse(actual)),expected);
});

it("positive HH:mm offset", function() {
var actual = "2011-01-23 10:15:51+06:10";
var expected = "\"2011-01-23T04:05:51.000Z\"";
assert.equal(JSON.stringify(parse(actual)),expected);
});

it("negative HH:mm offset", function() {
var actual = "2011-01-23 10:15:51-06:10";
var expected = "\"2011-01-23T16:25:51.000Z\"";
assert.equal(JSON.stringify(parse(actual)),expected);
});

it("positive HH:mm:ss offset", function() {
var actual = "0005-02-03 10:53:28+01:53:28";
var expected = "\"0005-02-03T09:00:00.000Z\"";
assert.equal(JSON.stringify(parse(actual)),expected);
});

it("negative HH:mm:ss offset", function() {
var actual = "0005-02-03 09:58:45-02:01:15";
var expected = "\"0005-02-03T12:00:00.000Z\"";
assert.equal(JSON.stringify(parse(actual)),expected);
});
});

});