Skip to content

Commit dda2ab0

Browse files
committed
fix: keep same-month day durations monotonic
Relative date labels around the month boundary can currently move backward in precision: a 25-day-old timestamp may render as "last month" while a 27-day-old timestamp renders as "27 days ago". In sorted date lists this makes newer items look older than later rows, even when the underlying timestamps are ordered correctly. Keep same-calendar-month day spans in days instead of promoting them through the intermediate four-week representation to one month. Preserve month rounding once the compared dates cross a calendar-month boundary. Add regression coverage for the 25/26-day same-month cases so the labels remain monotonic around the month boundary.
1 parent 97fefd8 commit dda2ab0

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/duration.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,10 @@ export function roundToSingleUnit(duration: Duration, {relativeTo = Date.now()}:
181181
const daysDiff = Math.abs(Math.round((Number(newDate) - Number(relativeTo)) / 86400000)) + monthDateCorrection
182182
const monthsDiff = Math.abs(yearDiff * 12 + monthDiff)
183183
if (daysDiff < 27) {
184-
if (days >= 6) {
185-
weeks += Math.round(days / 7)
184+
const roundedWeeks = Math.round(days / 7)
185+
// Keep same-calendar-month spans as days instead of rounding 4 weeks to a month.
186+
if (days >= 6 && (roundedWeeks < 4 || monthsDiff > 0)) {
187+
weeks += roundedWeeks
186188
days = 0
187189
} else {
188190
days = daysDiff

test/duration.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ suite('duration', function () {
259259
['-P21D', '-P3W', {relativeTo: new Date('2023-07-01T00:00:00')}],
260260
['P24D', 'P3W', {relativeTo: new Date('2023-07-01T00:00:00')}],
261261
['-P24D', '-P3W', {relativeTo: new Date('2023-07-01T00:00:00')}],
262-
['P24DT25H', 'P1M', {relativeTo: new Date('2023-07-01T00:00:00')}],
262+
['P24DT25H', 'P25D', {relativeTo: new Date('2023-07-01T00:00:00')}],
263263
['-P24DT25H', '-P1M', {relativeTo: new Date('2023-07-01T00:00:00')}],
264-
['P25D', 'P1M', {relativeTo: new Date('2023-07-01T00:00:00')}],
264+
['P25D', 'P25D', {relativeTo: new Date('2023-07-01T00:00:00')}],
265265
['-P25D', '-P1M', {relativeTo: new Date('2023-07-01T00:00:00')}],
266266
['P1M1D', 'P1M', {relativeTo: new Date('2023-07-02T00:00:00')}],
267267
['-P1M1D', '-P1M', {relativeTo: new Date('2023-07-02T00:00:00')}],
@@ -365,6 +365,9 @@ suite('duration', function () {
365365
['-P24D', [-3, 'week'], {relativeTo: '2023-01-01T00:00:00Z'}],
366366
['P24DT25H', [1, 'month'], {relativeTo: '2023-01-15T00:00:00Z'}],
367367
['P25D', [1, 'month'], {relativeTo: '2023-01-15T00:00:00Z'}],
368+
['P25D', [25, 'day'], {relativeTo: '2023-07-01T00:00:00Z'}],
369+
['-P25D', [-25, 'day'], {relativeTo: '2026-06-29T00:00:00Z'}],
370+
['-P26D', [-26, 'day'], {relativeTo: '2026-06-29T00:00:00Z'}],
368371
['-P35D', [-1, 'month'], {relativeTo: '2023-02-07T22:22:57Z'}],
369372
['-P45D', [-1, 'month'], {relativeTo: '2023-02-17T22:22:57Z'}],
370373
['-P55D', [-1, 'month'], {relativeTo: '2023-02-27T22:22:57Z'}],

0 commit comments

Comments
 (0)