Skip to content

Commit

Permalink
fix: Fix Duration plugin get seconds (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun authored Apr 17, 2020
1 parent 9790b85 commit 62b092d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ class Duration {
$ms %= MILLISECONDS_A_HOUR
this.$d.minutes = Math.floor($ms / MILLISECONDS_A_MINUTE)
$ms %= MILLISECONDS_A_MINUTE
this.$d.seconds = $ms / MILLISECONDS_A_SECOND
this.$d.seconds = Math.floor($ms / MILLISECONDS_A_SECOND)
$ms %= MILLISECONDS_A_SECOND
this.$d.milliseconds = $ms
}

toISOString() {
Expand Down Expand Up @@ -111,8 +113,10 @@ class Duration {
const pUnit = prettyUnit(unit)
if (pUnit === 'milliseconds') {
base %= 1000
} else {
} else if (pUnit === 'weeks') {
base = Math.floor(base / unitToMS[pUnit])
} else {
base = this.$d[pUnit]
}
return base
}
Expand Down
2 changes: 2 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,15 @@ describe('Seconds', () => {
expect(dayjs.duration(500).seconds()).toBe(0)
expect(dayjs.duration(1500).seconds()).toBe(1)
expect(dayjs.duration(15000).seconds()).toBe(15)
expect(dayjs.duration(61000).seconds()).toBe(1) // 1 minute 1 second
expect(dayjs.duration(500).asSeconds()).toBe(0.5)
expect(dayjs.duration(1500).asSeconds()).toBe(1.5)
expect(dayjs.duration(15000).asSeconds()).toBe(15)
})

describe('Minutes', () => {
expect(dayjs.duration(100000).minutes()).toBe(1)
expect(dayjs.duration(61000).minutes()).toBe(1) // 1 minute 1 second
expect(dayjs.duration(100000).asMinutes().toFixed(2)).toBe('1.67')
})

Expand Down

0 comments on commit 62b092d

Please sign in to comment.