Skip to content

Commit

Permalink
Improve fractional second parser
Browse files Browse the repository at this point in the history
ISO 8601 allows decimal fractions of at least one digit.
Since JS Date supports only milliseconds, we accept any
number of digits but we'll keep only up to three.

This way higher precision fractions will be accepted but
we'll only ever store 1-999 milliseconds (and no overflows).

See #48
  • Loading branch information
inukshuk committed Jan 12, 2024
1 parent 68c097a commit 08bd254
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/edtf.ne
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ minutes -> d00_59 {% num %}
seconds -> d00_59 {% num %}

milliseconds -> null
| "." d3 {% data => num(data.slice(1)) %}
| "." d3s {% data => num(data.slice(1)) %}

timezone -> "Z" {% zero %}
| ("-"|"−") offset {% data => -data[1] %}
Expand Down Expand Up @@ -296,6 +296,11 @@ d4 -> d2 d2 {% join %}
d3 -> d2 digit {% join %}
d2 -> digit digit {% join %}

d3s -> digit {% id %}
| d2 {% id %}
| d3 {% id %}
| d3 digits {% pick(0) %}

d5+ -> positive_digit d3 digits {% num %}

d1x -> [1-9X] {% id %}
Expand Down
6 changes: 5 additions & 1 deletion src/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ let ParserRules = [
{"name": "minutes", "symbols": ["d00_59"], "postprocess": num},
{"name": "seconds", "symbols": ["d00_59"], "postprocess": num},
{"name": "milliseconds", "symbols": []},
{"name": "milliseconds", "symbols": [{"literal":"."}, "d3"], "postprocess": data => num(data.slice(1))},
{"name": "milliseconds", "symbols": [{"literal":"."}, "d3s"], "postprocess": data => num(data.slice(1))},
{"name": "timezone", "symbols": [{"literal":"Z"}], "postprocess": zero},
{"name": "timezone$subexpression$1", "symbols": [{"literal":"-"}]},
{"name": "timezone$subexpression$1", "symbols": [{"literal":"−"}]},
Expand Down Expand Up @@ -295,6 +295,10 @@ let ParserRules = [
{"name": "d4", "symbols": ["d2", "d2"], "postprocess": join},
{"name": "d3", "symbols": ["d2", "digit"], "postprocess": join},
{"name": "d2", "symbols": ["digit", "digit"], "postprocess": join},
{"name": "d3s", "symbols": ["digit"], "postprocess": id},
{"name": "d3s", "symbols": ["d2"], "postprocess": id},
{"name": "d3s", "symbols": ["d3"], "postprocess": id},
{"name": "d3s", "symbols": ["d3", "digits"], "postprocess": pick(0)},
{"name": "d5+", "symbols": ["positive_digit", "d3", "digits"], "postprocess": num},
{"name": "d1x", "symbols": [/[1-9X]/], "postprocess": id},
{"name": "dx", "symbols": ["d1x"], "postprocess": id},
Expand Down
11 changes: 11 additions & 0 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ describe('parser', () => {
expect(p('2016-05-02T16:54:59.042'))
.to.produce([2016, 4, 2, 16, 54, 59, 42]).at.level(0))

it('YYYY-MM-DDThh:mm:ss.sss+', () => {
expect(p('2016-05-02T16:54:59.04200000'))
.to.produce([2016, 4, 2, 16, 54, 59, 42]).at.level(0)
expect(p('2016-05-02T16:54:59.4'))
.to.produce([2016, 4, 2, 16, 54, 59, 4]).at.level(0)
expect(p('2016-05-02T16:54:59.42'))
.to.produce([2016, 4, 2, 16, 54, 59, 42]).at.level(0)
expect(p('2016-05-02T16:54:59.042123'))
.to.produce([2016, 4, 2, 16, 54, 59, 42]).at.level(0)
})

it('YYYY-MM-DDThh:mm:ssZ', () => {
expect(p('2016-05-02T16:54:59Z'))
.to.produce([2016, 4, 2, 16, 54, 59]).at.level(0)
Expand Down

0 comments on commit 08bd254

Please sign in to comment.