From fd85788e2cfbfb7f09dabe34f595d09879d85980 Mon Sep 17 00:00:00 2001
From: Lily Foster
Date: Fri, 31 May 2024 16:50:13 -0400
Subject: [PATCH 01/37] correctly count month durations relative to the end of
longer months
---
src/duration.ts | 14 ++++++++++----
test/duration.ts | 9 +++++++--
test/relative-time.js | 18 ++++++++++++++++++
3 files changed, 35 insertions(+), 6 deletions(-)
diff --git a/src/duration.ts b/src/duration.ts
index a0529e3..77de611 100644
--- a/src/duration.ts
+++ b/src/duration.ts
@@ -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
diff --git a/test/duration.ts b/test/duration.ts
index d90c490..03d2177 100644
--- a/test/duration.ts
+++ b/test/duration.ts
@@ -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],
diff --git a/test/relative-time.js b/test/relative-time.js
index bd3cb32..e139bee 100644
--- a/test/relative-time.js
+++ b/test/relative-time.js
@@ -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)
From ff22489803793a6789c795fbf83210aeb87e4c3a Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Sun, 16 Jun 2024 09:45:02 +0000
Subject: [PATCH 02/37] Bump braces from 3.0.2 to 3.0.3
Bumps [braces](https://github.com/micromatch/braces) from 3.0.2 to 3.0.3.
- [Changelog](https://github.com/micromatch/braces/blob/master/CHANGELOG.md)
- [Commits](https://github.com/micromatch/braces/compare/3.0.2...3.0.3)
---
updated-dependencies:
- dependency-name: braces
dependency-type: indirect
...
Signed-off-by: dependabot[bot]
---
package-lock.json | 28 +++++++++++++++++++++-------
1 file changed, 21 insertions(+), 7 deletions(-)
diff --git a/package-lock.json b/package-lock.json
index 31bf23e..58fbc69 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1703,12 +1703,12 @@
}
},
"node_modules/braces": {
- "version": "3.0.2",
- "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
- "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==",
+ "version": "3.0.3",
+ "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz",
+ "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==",
"dev": true,
"dependencies": {
- "fill-range": "^7.0.1"
+ "fill-range": "^7.1.1"
},
"engines": {
"node": ">=8"
@@ -3289,9 +3289,9 @@
}
},
"node_modules/fill-range": {
- "version": "7.0.1",
- "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
- "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==",
+ "version": "7.1.1",
+ "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz",
+ "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==",
"dev": true,
"dependencies": {
"to-regex-range": "^5.0.1"
@@ -3386,6 +3386,20 @@
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==",
"dev": true
},
+ "node_modules/fsevents": {
+ "version": "2.3.3",
+ "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
+ "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
+ "dev": true,
+ "hasInstallScript": true,
+ "optional": true,
+ "os": [
+ "darwin"
+ ],
+ "engines": {
+ "node": "^8.16.0 || ^10.6.0 || >=11.0.0"
+ }
+ },
"node_modules/function-bind": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
From 5cb4310798c99b4ed635303ed113b2b294f8c1cb Mon Sep 17 00:00:00 2001
From: Hidde de Vries
Date: Wed, 19 Jun 2024 09:35:49 +0200
Subject: [PATCH 03/37] docs: match examples with actual values
---
README.md | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/README.md b/README.md
index 50704e2..4f835ae 100644
--- a/README.md
+++ b/README.md
@@ -199,21 +199,21 @@ Precision can be used to limit the display of an `relative` or `duration` format
| `precision=` | format=duration |
|:-------------:|:-------------------:|
-| seconds | 2y 6m 10d 3h 20m 8s |
-| minutes | 2y 6m 10d 3h 20m |
-| hours | 2y 6m 10d 3h |
-| days | 2y 6m 10d |
-| months | 2y 6m |
-| years | 2y |
+| second | 2y 6m 10d 3h 20m 8s |
+| minute | 2y 6m 10d 3h 20m |
+| hour | 2y 6m 10d 3h |
+| day | 2y 6m 10d |
+| month | 2y 6m |
+| year | 2y |
| `precision=` | format=relative |
|:-------------:|:-------------------:|
-| seconds | 25 seconds |
-| minutes | now |
-| hours | now |
-| days | now |
-| months | now |
-| years | now |
+| second | 25 seconds |
+| minute | now |
+| hour | now |
+| day | now |
+| month | now |
+| year | now |
##### threshold (`string`, default: `P30D`)
From c0d3799e391a3b8c0998f259f66b63e6607b251a Mon Sep 17 00:00:00 2001
From: Keith Cirkel
Date: Fri, 23 Aug 2024 11:53:43 +0100
Subject: [PATCH 04/37] ensure #updating doesnt hang updates
---
examples/index.html | 10 ++++++++++
src/relative-time-element.ts | 2 +-
test/relative-time.js | 9 +++++++++
3 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/examples/index.html b/examples/index.html
index d671479..13d91ba 100644
--- a/examples/index.html
+++ b/examples/index.html
@@ -193,6 +193,13 @@ Localised Dates
+
+ Lazily added datetime via setAttribute
+
+ This will display in one second.
+
+
+