Skip to content

Commit

Permalink
fix aggressive compression of hours into a single day
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Mar 23, 2023
1 parent a307efa commit cacd9e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ export function roundToSingleUnit(duration: Duration, {relativeTo = Date.now()}:
if (minutes >= 55) hours += Math.round(minutes / 60)
if (hours || days || weeks || months || years) minutes = 0

if (hours >= 21) days += Math.round(hours / 24)
if (days && hours >= 12) days += Math.round(hours / 24)
if (!days && hours >= 21) days += Math.round(hours / 24)
if (days || weeks || months || years) hours = 0

const currentYear = relativeTo.getFullYear()
Expand Down
7 changes: 7 additions & 0 deletions test/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ suite('duration', function () {
input: '2021-10-29T14:46:00.000Z',
expected: '-P1Y',
},
{
now: '2023-03-23T12:03:00.000Z',
input: '2023-03-21T16:03:00.000Z',
expected: '-P1DT20H',
},
])
for (const {input, now, precision = 'millisecond', expected} of elapsed) {
test(`${input} is ${expected} elapsed from ${now} (precision ${precision})`, () => {
Expand All @@ -235,6 +240,8 @@ suite('duration', function () {
['PT1H55M', 'PT2H'],
['PT20H', 'PT20H'],
['PT21H', 'P1D'],
['P1DT20H', 'P2D'],
['P1DT18H', 'P2D'],
['P4D', 'P4D', {relativeTo: new Date('2023-07-01T00:00:00')}],
['-P4D', '-P4D', {relativeTo: new Date('2023-07-01T00:00:00')}],
['P6D', 'P1W', {relativeTo: new Date('2023-07-01T00:00:00')}],
Expand Down
7 changes: 7 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,13 @@ suite('relative-time', function () {
tense: 'past',
expected: '2 years, 10 days',
},
{
reference: '2023-03-23T12:03:00.000Z',
datetime: '2023-03-21T16:03:00.000Z',
format: 'relative',
tense: 'past',
expected: '2 days ago',
},
])

for (const {
Expand Down

0 comments on commit cacd9e6

Please sign in to comment.