Skip to content

Commit 890a17a

Browse files
authored
fix: round durations to millisecond precision for ISO string (#2367)
1 parent 1fe1b1d commit 890a17a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/plugin/duration/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class Duration {
139139

140140
let seconds = this.$d.seconds || 0
141141
if (this.$d.milliseconds) {
142-
seconds += this.$d.milliseconds / 1000
142+
seconds += Math.round(this.$d.milliseconds) / 1000
143143
}
144144

145145
const S = getNumberUnitFormat(seconds, 'S')

test/plugin/duration.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ describe('Creating', () => {
7070
it('convert to milliseconds', () => {
7171
expect(+dayjs.duration(100)).toBe(100)
7272
})
73+
it('handles rounding to millisecond precision', () => {
74+
expect(dayjs.duration(2 / 3).toISOString()).toBe('PT0.001S')
75+
})
76+
it('should handle round with millisecond precision when negative', () => {
77+
expect(dayjs.duration(1000.5).toISOString()).toBe('PT1.001S')
78+
expect(dayjs.duration(-1000.5).toISOString()).toBe('-PT1S')
79+
})
7380
})
7481

7582
describe('Parse ISO string', () => {

0 commit comments

Comments
 (0)