From 468dfda8b51efaa155a8f47d4a1649d8d7328e9c Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Sat, 13 Jun 2015 16:56:09 -0700 Subject: [PATCH] Move date parser to separate module --- lib/textParsers.js | 79 +--------------------------------------------- package.json | 3 +- test/date.js | 78 --------------------------------------------- 3 files changed, 3 insertions(+), 157 deletions(-) delete mode 100644 test/date.js diff --git a/lib/textParsers.js b/lib/textParsers.js index c23ca77..9435c9f 100644 --- a/lib/textParsers.js +++ b/lib/textParsers.js @@ -1,84 +1,7 @@ var array = require('postgres-array') var ap = require('ap') var arrayParser = require(__dirname + "/arrayParser.js"); - -//parses PostgreSQL server formatted date strings into javascript date objects -var parseDate = function(isoDate) { - //TODO this could do w/ a refactor - var dateMatcher = /(\d{1,})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?/; - - var match = dateMatcher.exec(isoDate); - //could not parse date - if(!match) { - dateMatcher = /^(\d{1,})-(\d{2})-(\d{2})$/; - match = dateMatcher.test(isoDate); - if(!match) { - return null; - } else { - //it is a date in YYYY-MM-DD format - //add time portion to force js to parse as local time - return new Date(isoDate + ' 00:00:00'); - } - } - var isBC = /BC$/.test(isoDate); - var _year = parseInt(match[1], 10); - var isFirstCentury = (_year > 0) && (_year < 100); - var year = (isBC ? "-" : "") + match[1]; - - var month = parseInt(match[2],10)-1; - var day = match[3]; - var hour = parseInt(match[4],10); - var min = parseInt(match[5],10); - var seconds = parseInt(match[6], 10); - - var miliString = match[7]; - var mili = 0; - if(miliString) { - mili = 1000 * parseFloat(miliString); - } - - //match timezones like the following: - //Z (UTC) - //-05 - //+06:30 - 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 '-': - tzSign = -1; - case '+': - 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 * 1000)); - } - //no timezone information - else { - date = new Date(year, month, day, hour, min, seconds, mili); - } - - if (isFirstCentury) { - date.setUTCFullYear(year); - } - - return date; -}; +var parseDate = require('postgres-date'); function allowNull (fn) { return function nullAllowed (value) { diff --git a/package.json b/package.json index a7cb0f1..076ae1a 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "dependencies": { "ap": "~0.2.0", - "postgres-array": "~1.0.0" + "postgres-array": "~1.0.0", + "postgres-date": "^1.0.0" } } diff --git a/test/date.js b/test/date.js deleted file mode 100644 index c4bb4bf..0000000 --- a/test/date.js +++ /dev/null @@ -1,78 +0,0 @@ -'use strict' - -var test = require('tape') -var parse = require('../').getTypeParser(1114, 'text') - -test('date parser', function (t) { - t.equal( - parse('2010-12-11 09:09:04').toString(), - new Date('2010-12-11 09:09:04').toString() - ) - - function ms (string) { - var base = '2010-01-01 01:01:01' - return parse(base + string).getMilliseconds() - } - t.equal(ms('.1'), 100) - t.equal(ms('.01'), 10), - t.equal(ms('.74'), 740) - - function iso (string) { - return parse(string).toISOString() - } - t.equal( - iso('2010-12-11 09:09:04.1'), - new Date(2010,11,11,9,9,4,100).toISOString(), - 'no timezones' - ) - - t.equal( - iso('2011-01-23 22:15:51.280843-06'), - '2011-01-24T04:15:51.280Z', - 'huge ms value' - ) - - t.equal( - iso('2011-01-23 22:15:51Z'), - '2011-01-23T22:15:51.000Z', - 'zulu time offset' - ) - - t.equal( - iso('2011-01-23 22:15:51Z'), - '2011-01-23T22:15:51.000Z', - 'zulu time offset' - ) - - t.equal( - iso('2011-01-23 10:15:51-04'), - '2011-01-23T14:15:51.000Z', - 'negative hour offset' - ) - - t.equal( - iso('2011-01-23 10:15:51+06:10'), - '2011-01-23T04:05:51.000Z', - 'positive HH:mm offset' - ) - - t.equal( - iso('2011-01-23 10:15:51-06:10'), - '2011-01-23T16:25:51.000Z', - 'negative HH:mm offset' - ) - - t.equal( - iso('0005-02-03 10:53:28+01:53:28'), - '0005-02-03T09:00:00.000Z', - 'positive HH:mm:ss offset' - ) - - t.equal( - iso('0005-02-03 09:58:45-02:01:15'), - '0005-02-03T12:00:00.000Z', - 'negative HH:mm:ss offset' - ) - - t.end() -})