Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
feat: use RFC3339 to format dates, fixes ipfs/go-ipld-git#16
Browse files Browse the repository at this point in the history
  • Loading branch information
sameer committed Mar 21, 2019
1 parent 4519644 commit 15c3277
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 12 additions & 3 deletions src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -19,16 +20,24 @@ 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
}

return {
name: matched[2],
email: matched[3],
date: matched[4]
date: matched[4] && matched[5] && timestampWithOffsetToISOString(parseInt(matched[4]), matched[5])
}
}

Expand All @@ -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(' ')
Expand Down
10 changes: 5 additions & 5 deletions test/parse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand All @@ -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()
})

Expand All @@ -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()
})

Expand All @@ -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()
})

Expand All @@ -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()
})

Expand Down
6 changes: 3 additions & 3 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
}
Expand Down
4 changes: 2 additions & 2 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand All @@ -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()
})
})
Expand Down

0 comments on commit 15c3277

Please sign in to comment.