Skip to content

Commit cacd9e6

Browse files
committed
fix aggressive compression of hours into a single day
1 parent a307efa commit cacd9e6

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/duration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ export function roundToSingleUnit(duration: Duration, {relativeTo = Date.now()}:
147147
if (minutes >= 55) hours += Math.round(minutes / 60)
148148
if (hours || days || weeks || months || years) minutes = 0
149149

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

153154
const currentYear = relativeTo.getFullYear()

test/duration.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ suite('duration', function () {
215215
input: '2021-10-29T14:46:00.000Z',
216216
expected: '-P1Y',
217217
},
218+
{
219+
now: '2023-03-23T12:03:00.000Z',
220+
input: '2023-03-21T16:03:00.000Z',
221+
expected: '-P1DT20H',
222+
},
218223
])
219224
for (const {input, now, precision = 'millisecond', expected} of elapsed) {
220225
test(`${input} is ${expected} elapsed from ${now} (precision ${precision})`, () => {
@@ -235,6 +240,8 @@ suite('duration', function () {
235240
['PT1H55M', 'PT2H'],
236241
['PT20H', 'PT20H'],
237242
['PT21H', 'P1D'],
243+
['P1DT20H', 'P2D'],
244+
['P1DT18H', 'P2D'],
238245
['P4D', 'P4D', {relativeTo: new Date('2023-07-01T00:00:00')}],
239246
['-P4D', '-P4D', {relativeTo: new Date('2023-07-01T00:00:00')}],
240247
['P6D', 'P1W', {relativeTo: new Date('2023-07-01T00:00:00')}],

test/relative-time.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,13 @@ suite('relative-time', function () {
17521752
tense: 'past',
17531753
expected: '2 years, 10 days',
17541754
},
1755+
{
1756+
reference: '2023-03-23T12:03:00.000Z',
1757+
datetime: '2023-03-21T16:03:00.000Z',
1758+
format: 'relative',
1759+
tense: 'past',
1760+
expected: '2 days ago',
1761+
},
17551762
])
17561763

17571764
for (const {

0 commit comments

Comments
 (0)