Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: round durations to millisecond precision for ISO string #2367

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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