Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix aggressive compression of hours into a single day #254

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 11 additions & 3 deletions test/relative-time.js
Original file line number Diff line number Diff line change
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