diff --git a/package.json b/package.json index ded2cf6..2496b64 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,8 @@ "multihashes": "~0.4.12", "multihashing-async": "~0.5.1", "smart-buffer": "^4.0.0", - "traverse": "~0.6.6" + "traverse": "~0.6.6", + "strftime": "~0.10.0" }, "devDependencies": { "aegir": "^18.0.2", diff --git a/src/util/util.js b/src/util/util.js index a4b4a71..dc2518c 100644 --- a/src/util/util.js +++ b/src/util/util.js @@ -5,6 +5,7 @@ const multihashes = require('multihashes/src/constants') const multicodecs = require('multicodec/src/base-table') const multihash = require('multihashes') const CID = require('cids') +const strftime = require('strftime') exports = module.exports @@ -19,8 +20,16 @@ exports.find = (buf, byte) => { return -1 } +const timestampWithOffsetToISOString = (timestamp, offset) => strftime.timezone(offset)('%FT%T%:z', new Date(timestamp * 1000)) + +const isoStringToTimestampWithOffset = (isoString) => { + const matched = isoString.match(/([+-]\d{2}:\d{2})/) + const offset = matched === null ? '+0000' : (matched[0].slice(-6, -3) + matched[0].slice(-2)) + return strftime.timezone(offset)('%s %z', new Date(isoString)) +} + exports.parsePersonLine = (line) => { - let matched = line.match(/^(([^<]+)\s)?\s?<([^>]+)>\s?(\d+\s[+\-\d]+)?$/) + let matched = line.match(/^(([^<]+)\s)?\s?<([^>]+)>\s?(?:(\d+)\s([+-]\d+))?$/) if (matched === null) { return null } @@ -28,7 +37,7 @@ exports.parsePersonLine = (line) => { return { name: matched[2], email: matched[3], - date: matched[4] + date: matched[4] && matched[5] && timestampWithOffsetToISOString(parseInt(matched[4]), matched[5]) } } @@ -39,7 +48,7 @@ exports.serializePersonLine = (node) => { } parts.push('<' + node.email + '>') if (node.date) { - parts.push(node.date) + parts.push(isoStringToTimestampWithOffset(node.date)) } return parts.join(' ') diff --git a/test/parse.spec.js b/test/parse.spec.js index aa7c6d0..163fe10 100644 --- a/test/parse.spec.js +++ b/test/parse.spec.js @@ -22,7 +22,7 @@ describe('utils', () => { expect(info).to.exist() expect(info.name).to.equal('Someone') expect(info.email).to.equal('some@one.somewhere') - expect(info.date).to.equal('123456 +0123') + expect(info.date).to.equal('1970-01-02T11:40:36+01:23') done() }) @@ -31,7 +31,7 @@ describe('utils', () => { expect(info).to.exist() expect(info.name).to.equal('So Me One') expect(info.email).to.equal('some@one.somewhere') - expect(info.date).to.equal('123456 +0123') + expect(info.date).to.equal('1970-01-02T11:40:36+01:23') done() }) @@ -40,7 +40,7 @@ describe('utils', () => { expect(info).to.exist() expect(info.name).to.not.exist() expect(info.email).to.equal('some@one.somewhere') - expect(info.date).to.equal('123456 +0123') + expect(info.date).to.equal('1970-01-02T11:40:36+01:23') done() }) @@ -49,7 +49,7 @@ describe('utils', () => { expect(info).to.exist() expect(info.name).to.not.exist() expect(info.email).to.equal('some@one.somewhere') - expect(info.date).to.equal('123456 +0123') + expect(info.date).to.equal('1970-01-02T11:40:36+01:23') done() }) @@ -58,7 +58,7 @@ describe('utils', () => { expect(info).to.exist() expect(info.name).to.equal('Some One & Other One') expect(info.email).to.equal('some@one.somewhere, other@one.elsewhere') - expect(info.date).to.equal('987654 +4321') + expect(info.date).to.equal('1970-01-14T05:41:54+43:21') done() }) diff --git a/test/resolver.spec.js b/test/resolver.spec.js index 66e409d..a076448 100644 --- a/test/resolver.spec.js +++ b/test/resolver.spec.js @@ -30,12 +30,12 @@ describe('IPLD format resolver (local)', () => { author: { name: 'John Doe', email: 'johndoe@example.com', - date: '1497302532 +0200' + date: '2017-06-12T23:22:12+02:00' }, committer: { name: 'John Doe', email: 'johndoe@example.com', - date: '1497302532 +0200' + date: '2017-06-12T23:22:12+02:00' }, encoding: 'ISO-8859-1', message: 'Encoded\n' @@ -49,7 +49,7 @@ describe('IPLD format resolver (local)', () => { tagger: { name: 'John Doe', email: 'johndoe@example.com', - date: '1497302532 +0200' + date: '2017-06-12T23:22:12+02:00' }, message: 'A message\n' } diff --git a/test/util.spec.js b/test/util.spec.js index a222167..759cdcb 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -18,7 +18,7 @@ describe('IPLD format util', () => { tagger: { name: 'John Doe', email: 'johndoe@example.com', - date: '1497302532 +0200' + date: '2017-06-12T23:22:12+02:00' }, message: 'A message\n' } @@ -29,7 +29,7 @@ describe('IPLD format util', () => { expect(Buffer.isBuffer(serialized)).to.equal(true) ipldGit.util.deserialize(serialized, (err, deserialized) => { expect(err).to.not.exist() - expect(tagNode).to.eql(deserialized) + expect(deserialized).to.eql(tagNode) done() }) })