Skip to content

Commit

Permalink
Merge pull request #254 from github/fix-aggressive-compression-of-hou…
Browse files Browse the repository at this point in the history
…rs-into-a-single-day

fix aggressive compression of hours into a single day
  • Loading branch information
keithamus committed Mar 23, 2023
2 parents a307efa + e17753c commit c000266
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/duration.ts
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
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
14 changes: 11 additions & 3 deletions test/relative-time.js
Expand Up @@ -475,13 +475,14 @@ suite('relative-time', function () {
})

test('micro formats years', async () => {
const now = new Date(Date.now() - 10 * 365 * 24 * 60 * 60 * 1000).toISOString()
const datetime = new Date()
datetime.setFullYear(datetime.getFullYear() - 10)
const time = document.createElement('relative-time')
time.setAttribute('tense', 'past')
time.setAttribute('datetime', now)
time.setAttribute('datetime', datetime)
time.setAttribute('format', 'micro')
await Promise.resolve()
assert.equal(time.shadowRoot.textContent, '11y')
assert.equal(time.shadowRoot.textContent, '10y')
})

test('micro formats future times', async () => {
Expand Down Expand Up @@ -1752,6 +1753,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 c000266

Please sign in to comment.