Skip to content

Commit

Permalink
get_time_object: allow inconsistent time precision
Browse files Browse the repository at this point in the history
rather than crashing

addressing maxlath/wikibase-cli#130
  • Loading branch information
maxlath committed Sep 18, 2020
1 parent c5e721d commit 5c27118
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/claim/get_time_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ module.exports = value => {

const getTimeStringBase = (time, precision) => {
if (precision > 10) return time
if (precision === 10) return time + '-00'
if (precision === 10) {
if (time.match(/^-?\d+-\d+$/)) return time + '-00'
else return time
}
// From the year (9) to the billion years (0)
// See https://www.wikidata.org/wiki/Help:Dates#Precision
const yearMatch = time.match(/^(-?\d+)/)
Expand Down
36 changes: 36 additions & 0 deletions test/unit/claim/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,40 @@ describe('claim time', () => {
getTimeObject({ time: '2020', calendar: 'Q1985786' }).calendarmodel.should.equal(julian)
getTimeObject({ time: '2020', calendar: julian }).calendarmodel.should.equal(julian)
})

it('should accept full rich value', () => {
getTimeObject({
time: '2018-04-15T00:00:00.000Z',
timezone: 0,
before: 0,
after: 0,
precision: 10,
calendarmodel: 'http://www.wikidata.org/entity/Q1985727'
}).should.deepEqual({
time: '+2018-04-15T00:00:00Z',
timezone: 0,
before: 0,
after: 0,
precision: 10,
calendarmodel: 'http://www.wikidata.org/entity/Q1985727'
})
})

it('should accept setting month precision for times that specify a day', () => {
getTimeObject({
time: '2018-04-15T00:00:00.000Z',
timezone: 0,
before: 0,
after: 0,
precision: 10,
calendarmodel: 'http://www.wikidata.org/entity/Q1985727'
}).should.deepEqual({
time: '+2018-04-15T00:00:00Z',
timezone: 0,
before: 0,
after: 0,
precision: 10,
calendarmodel: 'http://www.wikidata.org/entity/Q1985727'
})
})
})

0 comments on commit 5c27118

Please sign in to comment.