Skip to content

Commit c000266

Browse files
authored
Merge pull request #254 from github/fix-aggressive-compression-of-hours-into-a-single-day
fix aggressive compression of hours into a single day
2 parents a307efa + e17753c commit c000266

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/duration.ts

+2-1
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

+7
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

+11-3
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,14 @@ suite('relative-time', function () {
475475
})
476476

477477
test('micro formats years', async () => {
478-
const now = new Date(Date.now() - 10 * 365 * 24 * 60 * 60 * 1000).toISOString()
478+
const datetime = new Date()
479+
datetime.setFullYear(datetime.getFullYear() - 10)
479480
const time = document.createElement('relative-time')
480481
time.setAttribute('tense', 'past')
481-
time.setAttribute('datetime', now)
482+
time.setAttribute('datetime', datetime)
482483
time.setAttribute('format', 'micro')
483484
await Promise.resolve()
484-
assert.equal(time.shadowRoot.textContent, '11y')
485+
assert.equal(time.shadowRoot.textContent, '10y')
485486
})
486487

487488
test('micro formats future times', async () => {
@@ -1752,6 +1753,13 @@ suite('relative-time', function () {
17521753
tense: 'past',
17531754
expected: '2 years, 10 days',
17541755
},
1756+
{
1757+
reference: '2023-03-23T12:03:00.000Z',
1758+
datetime: '2023-03-21T16:03:00.000Z',
1759+
format: 'relative',
1760+
tense: 'past',
1761+
expected: '2 days ago',
1762+
},
17551763
])
17561764

17571765
for (const {

0 commit comments

Comments
 (0)