Skip to content
Open
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
4 changes: 4 additions & 0 deletions packages/lingo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ change**, even if the API is untouched.

## [Unreleased]

### Fixed

- `humanizeDateRange` preserves seconds on `second`-grain endpoints, second-grain anchors, and sub-minute anchored durations (`9:00:30am to 5:00:45pm`, `30 seconds starting 2026-07-03T09:00:15`), restoring the two-way round-trip guarantee.

## [0.5.0] - 2026-08-23

### Added
Expand Down
61 changes: 33 additions & 28 deletions packages/lingo/src/date/humanize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export function humanizeDateRange(
opts?: HumanizeDateRangeOptions,
): string {
if (range.anchored && range.start && range.end) {
const a = anchoredPhrase(range.start.date, range.end.date, opts)
const a = anchoredPhrase(range.start, range.end, opts)
if (a) {
return a
}
Expand All @@ -338,12 +338,13 @@ export function humanizeDateRange(
// A calendar range must carry its dates, or "July 1 to July 5" renders as
// "12:00 AM to 12:00 AM" and re-parses to today. Clock slots stay bare.
const render = (ep: { date: Date; grain?: DateGrain }): string => {
if (!range.dated) {
return formatClock(ep.date, hour12)
const clock = formatClock(ep.date, hour12, ep.grain === 'second')
if (!(range.dated || range.anchored)) {
return clock
}
const day = formatMonthDayYear(ep.date)
return ep.grain === 'hour' || ep.grain === 'minute' || ep.grain === 'second'
? `${day} ${formatClock(ep.date, hour12)}`
? `${day} ${clock}`
: day
}
const start = range.start ? render(range.start) : undefined
Expand All @@ -360,43 +361,47 @@ export function humanizeDateRange(
throw new Error('humanizeDateRange: range has neither start nor end.')
}

function anchoredPhrase(s: Date, e: Date, opts?: HumanizeDateRangeOptions): string | null {
const m =
(e.getTime() - s.getTime() - (e.getTimezoneOffset() - s.getTimezoneOffset()) * 6e4) / 6e4
if (m <= 0) {
return null
}
const day = !(s.getHours() | s.getMinutes() | s.getSeconds()) && m % 1440 === 0
const n = day ? m / 1440 : m % 60 === 0 ? m / 60 : m
if (!Number.isInteger(n)) {
function anchoredPhrase(
startEp: { date: Date; grain?: DateGrain },
endEp: { date: Date; grain?: DateGrain },
opts?: HumanizeDateRangeOptions,
): string | null {
const s = startEp.date
const e = endEp.date
const sec =
(e.getTime() - s.getTime() - (e.getTimezoneOffset() - s.getTimezoneOffset()) * 6e4) / 1e3
if (sec <= 0 || !Number.isInteger(sec)) {
return null
}
const u = day
? n === 1
? 'day'
: 'days'
: m % 60 === 0
? n === 1
? 'hour'
: 'hours'
: n === 1
? 'minute'
: 'minutes'
const day =
!(s.getHours() | s.getMinutes() | s.getSeconds()) &&
startEp.grain !== 'second' &&
sec % 86_400 === 0
const [n, singular] = day
? [sec / 86_400, 'day']
: sec % 3600 === 0
? [sec / 3600, 'hour']
: sec % 60 === 0
? [sec / 60, 'minute']
: [sec, 'second']
const u = n === 1 ? singular : `${singular}s`
const anchor = day
? formatMonthDayYear(s)
: `${formatMonthDayYear(s)} ${formatClock(s, opts?.hour12 ?? true)}`
: `${formatMonthDayYear(s)} ${formatClock(s, opts?.hour12 ?? true, startEp.grain === 'second')}`
return `${n} ${u} starting ${anchor}`
}

function formatMonthDayYear(d: Date): string {
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
}

function formatClock(d: Date, hour12: boolean): string {
function formatClock(d: Date, hour12: boolean, withSeconds = false): string {
const h = d.getHours()
const mm = String(d.getMinutes()).padStart(2, '0')
const sec =
withSeconds || d.getSeconds() !== 0 ? `:${String(d.getSeconds()).padStart(2, '0')}` : ''
if (!hour12) {
return `${String(h).padStart(2, '0')}:${mm}`
return `${String(h).padStart(2, '0')}:${mm}${sec}`
}
return `${h % 12 || 12}:${mm} ${h < 12 ? 'AM' : 'PM'}`
return `${h % 12 || 12}:${mm}${sec} ${h < 12 ? 'AM' : 'PM'}`
}
33 changes: 32 additions & 1 deletion packages/lingo/src/date/range.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('parseDateRange', () => {
})

describe('humanizeDateRange round-trips', () => {
const clock = (d: Date) => [d.getHours(), d.getMinutes()] as const
const clock = (d: Date) => [d.getHours(), d.getMinutes(), d.getSeconds()] as const

for (const [text, hour12] of [
['2pm to 4pm', true],
Expand All @@ -192,6 +192,11 @@ describe('humanizeDateRange round-trips', () => {
['noon to midnight', false],
['between 9am and 5pm', false],
['13:15 to 14:45', false],
['9:00:30am to 5:00:45pm', true],
['9:00:30am to 5:00:45pm', false],
['9:00:00am to 5:00:00pm', true],
['from 9:00:15am', true],
['until 17:00:45', false],
] as const) {
it(`re-parses "${text}" (hour12=${hour12})`, () => {
const first = parseDateRange(text, { now: NOW })
Expand Down Expand Up @@ -357,6 +362,31 @@ describe('humanizeDateRange round-trips for time-grain anchored ranges', () => {
const phrase = humanizeDateRange(r)
expect(phrase).toBe('3 days starting 2026-03-01')
})

it('preserves seconds on second-grain anchors and sub-minute durations', () => {
for (const [input, expected] of [
['10 minutes starting 2026-07-03T09:00:30', '10 minutes starting 2026-07-03 9:00:30 AM'],
['30 seconds starting 2026-07-03T09:00:15', '30 seconds starting 2026-07-03 9:00:15 AM'],
['1 second starting 2026-07-03T09:00:00', '1 second starting 2026-07-03 9:00:00 AM'],
] as const) {
const r = parseDateRange(input)
expect(r.ok).toBe(true)
if (!r.ok) {
return
}
const phrase = humanizeDateRange(r)
expect(phrase).toBe(expected)
const reparsed = parseDateRange(phrase)
expect(reparsed.ok).toBe(true)
if (!reparsed.ok) {
return
}
expect(reparsed.start?.date).toEqual(r.start?.date)
expect(reparsed.end?.date).toEqual(r.end?.date)
expect(reparsed.start?.grain).toBe('second')
expect(reparsed.end?.grain).toBe('second')
}
})
})

describe('anchored duration range trailing zone (F3)', () => {
Expand Down Expand Up @@ -553,6 +583,7 @@ describe('humanizeDateRange round-trips calendar ranges', () => {
['until august 9', NOW],
['2026-08-01 to 2026-08-05', NOW],
['July 1 3pm to July 2 5pm', NOW],
['2026-07-03T09:00:30 to 2026-07-03T17:00:45', NOW],
['this weekend', SUNDAY],
['next weekend', SUNDAY],
['last weekend', SUNDAY],
Expand Down
8 changes: 0 additions & 8 deletions plans/backlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,6 @@ surfaces mid-task, add it here and keep going — don't act on it.
drift in the autocomplete display string, not in `format`/`humanize`). Slicing
to 13 (`…T15`) fixes it only if the date parser accepts a bare `THH` — verify
that round-trips before changing. Surfaced by the 0.2.0 completions review.
- **`humanizeDateRange` drops seconds** — `formatClock` (`date/humanize.ts`)
never emits a seconds field, so `9:00:30am to 5:00:45pm` humanizes to
`9:00 AM to 5:00 PM` and re-parses a minute off. This predates the D71
calendar-range work (verified against the commit before it) and breaks the
two-way guarantee for second-grain endpoints. Render seconds whenever an
endpoint carries `second` grain; check the anchored-duration phrase at the
same time, since it shares the clock formatter. Surfaced by the plan-032
adversarial review pass.
- **Elliptical range right sides** — `Aug 3–9`, `July 1-5`, `July 1 through 5`
all fail: `parseDateToDateRange` parses each endpoint independently, so a bare
day number on the right has no month to attach to. The right side should
Expand Down