Skip to content

Commit

Permalink
fix: round durations to millisecond precision for ISO string
Browse files Browse the repository at this point in the history
  • Loading branch information
creedasaurus committed Jul 4, 2023
1 parent 1fe1b1d commit c584007
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Duration {

let seconds = this.$d.seconds || 0
if (this.$d.milliseconds) {
seconds += this.$d.milliseconds / 1000
seconds += Math.round(this.$d.milliseconds) / 1000
}

const S = getNumberUnitFormat(seconds, 'S')
Expand Down
7 changes: 7 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ describe('Creating', () => {
it('convert to milliseconds', () => {
expect(+dayjs.duration(100)).toBe(100)
})
it('handles rounding to millisecond precision', () => {
expect(dayjs.duration(2 / 3).toISOString()).toBe('PT0.001S')
})
it('should handle round with millisecond precision when negative', () => {
expect(dayjs.duration(1000.5).toISOString()).toBe('PT1.001S')
expect(dayjs.duration(-1000.5).toISOString()).toBe('-PT1S')
})
})

describe('Parse ISO string', () => {
Expand Down

0 comments on commit c584007

Please sign in to comment.