Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: github/relative-time-element
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.4.1
Choose a base ref
...
head repository: github/relative-time-element
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.4.2
Choose a head ref
  • 3 commits
  • 3 files changed
  • 2 contributors

Commits on May 31, 2024

  1. correctly count month durations relative to the end of longer months

    lilyinstarlight committed May 31, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    fd85788 View commit details

Commits on Jun 7, 2024

  1. Merge branch 'main' into fix/relative-time-at-end-of-longer-months

    keithamus authored Jun 7, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    60fb1ef View commit details

Commits on Jun 10, 2024

  1. Merge pull request #285 from lilyinstarlight/fix/relative-time-at-end…

    …-of-longer-months
    
    Correctly count month durations relative to the end of longer months
    keithamus authored Jun 10, 2024
    7

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    tonistiigi Tõnis Tiigi
    Copy the full SHA
    7db58b3 View commit details
Showing with 35 additions and 6 deletions.
  1. +10 −4 src/duration.ts
  2. +7 −2 test/duration.ts
  3. +18 −0 test/relative-time.js
14 changes: 10 additions & 4 deletions src/duration.ts
Original file line number Diff line number Diff line change
@@ -153,16 +153,23 @@ export function roundToSingleUnit(duration: Duration, {relativeTo = Date.now()}:

// Resolve calendar dates
const currentYear = relativeTo.getFullYear()
let currentMonth = relativeTo.getMonth()
const currentMonth = relativeTo.getMonth()
const currentDate = relativeTo.getDate()
if (days >= 27 || years + months + days) {
const newMonthDate = new Date(relativeTo)
newMonthDate.setDate(1)
newMonthDate.setMonth(currentMonth + months * sign + 1)
newMonthDate.setDate(0)
const monthDateCorrection = Math.max(0, currentDate - newMonthDate.getDate())

const newDate = new Date(relativeTo)
newDate.setFullYear(currentYear + years * sign)
newDate.setDate(currentDate - monthDateCorrection)
newDate.setMonth(currentMonth + months * sign)
newDate.setDate(currentDate + days * sign)
newDate.setDate(currentDate - monthDateCorrection + days * sign)
const yearDiff = newDate.getFullYear() - relativeTo.getFullYear()
const monthDiff = newDate.getMonth() - relativeTo.getMonth()
const daysDiff = Math.abs(Math.round((Number(newDate) - Number(relativeTo)) / 86400000))
const daysDiff = Math.abs(Math.round((Number(newDate) - Number(relativeTo)) / 86400000)) + monthDateCorrection
const monthsDiff = Math.abs(yearDiff * 12 + monthDiff)
if (daysDiff < 27) {
if (days >= 6) {
@@ -180,7 +187,6 @@ export function roundToSingleUnit(duration: Duration, {relativeTo = Date.now()}:
years = yearDiff * sign
}
if (months || years) days = 0
currentMonth = relativeTo.getMonth()
}
if (years) months = 0

9 changes: 7 additions & 2 deletions test/duration.ts
Original file line number Diff line number Diff line change
@@ -367,6 +367,9 @@ suite('duration', function () {
['-P55D', [-1, 'month'], {relativeTo: '2023-02-27T22:22:57Z'}],
['-P65D', [-3, 'month'], {relativeTo: '2023-02-28T22:22:57Z'}],
['-P75D', [-3, 'month'], {relativeTo: '2023-03-09T22:22:57Z'}],
['P1M', [1, 'month'], {relativeTo: '2024-05-31T00:00:00Z'}],
['-P1M', [-1, 'month'], {relativeTo: '2024-05-31T00:00:00Z'}],
['-P3M', [-3, 'month'], {relativeTo: '2023-05-30T00:00:00Z'}],
[
'P8M',
[8, 'month'],
@@ -396,6 +399,8 @@ suite('duration', function () {
},
],
['P1M1D', [1, 'month'], {relativeTo: new Date('2022-12-01T00:00:00Z')}],
['P1M1D', [2, 'month'], {relativeTo: new Date('2023-01-31T00:00:00Z')}],
['P1M30D', [2, 'month'], {relativeTo: new Date('2023-01-31T00:00:00Z')}],
[
'P9M20DT25H',
[9, 'month'],
@@ -478,14 +483,14 @@ suite('duration', function () {
],
])
for (const [input, [val, unit], opts] of relativeTests) {
test(`getRelativeTimeUnit(${input}) === [${val}, ${unit}]`, () => {
test(`getRelativeTimeUnit(${input}${opts ? `, ${JSON.stringify(opts)}` : ''}) === [${val}, ${unit}]`, () => {
assert.deepEqual(
getRelativeTimeUnit(Duration.from(input), opts || {relativeTo: new Date('2023-07-01T00:00:00')}),
[val, unit],
)
})
if (opts?.relativeTo) continue
test(`getRelativeTimeUnit(-${input}) === [-${val}, ${unit}]`, () => {
test(`getRelativeTimeUnit(-${input}${opts ? `, ${JSON.stringify(opts)}` : ''}) === [-${val}, ${unit}]`, () => {
assert.deepEqual(
getRelativeTimeUnit(Duration.from(`-${input}`), opts || {relativeTo: new Date('2023-07-01T00:00:00')}),
[-val, unit],
18 changes: 18 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
@@ -482,6 +482,24 @@ suite('relative-time', function () {
assert.equal(time.shadowRoot.textContent, '4 months ago')
})

test('rewrites from last few days of month to smaller last month', async () => {
freezeTime(new Date(2024, 4, 31))
const time = document.createElement('relative-time')
time.setAttribute('tense', 'past')
time.setAttribute('datetime', '2024-04-30T00:00:00Z')
await Promise.resolve()
assert.equal(time.shadowRoot.textContent, 'last month')
})

test('rewrites from last few days of month to smaller previous month', async () => {
freezeTime(new Date(2024, 4, 31))
const time = document.createElement('relative-time')
time.setAttribute('tense', 'past')
time.setAttribute('datetime', '2024-02-29T00:00:00Z')
await Promise.resolve()
assert.equal(time.shadowRoot.textContent, '3 months ago')
})

test('micro formats years', async () => {
const datetime = new Date()
datetime.setFullYear(datetime.getFullYear() - 10)