Skip to content

Commit

Permalink
Merge pull request #253 from zbinlin/fix-round-to-string-unit
Browse files Browse the repository at this point in the history
fix: fixes round to string unit
  • Loading branch information
keithamus authored Mar 7, 2023
2 parents 0ea55ec + 73bd58c commit f0f5e08
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ export function roundToSingleUnit(duration: Duration, {relativeTo = Date.now()}:
const currentDate = relativeTo.getDate()
if (days >= 27 || (years + months && days)) {
relativeTo.setDate(currentDate + days * sign)
days = 0
months += Math.abs(
relativeTo.getFullYear() >= currentYear
? relativeTo.getMonth() - currentMonth
: relativeTo.getMonth() - currentMonth - 12,
)
if (months) {
days = 0
}
currentMonth = relativeTo.getMonth()
}

Expand Down
1 change: 1 addition & 0 deletions test/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ suite('duration', function () {
relativeTo: new Date('2022-01-01T00:00:00Z'),
},
],
['-P27D', '-P1M', {relativeTo: new Date('2023-02-28T00:00:00Z')}],
])
for (const [input, expected, opts] of roundTests) {
test(`roundToSingleUnit(${input}) === ${expected}`, () => {
Expand Down

0 comments on commit f0f5e08

Please sign in to comment.