Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

effective dates: parse chat-export '<time> on <day> <Month>, <year>' frontmatter dates

garrytan0cba0ec9c356
diff --git a/src/core/effective-date.ts b/src/core/effective-date.ts
index d0720629c8..2c138d7989 100644
--- a/src/core/effective-date.ts
+++ b/src/core/effective-date.ts
@@ -109,6 +109,8 @@ const NUMERIC_DATE_RE = /^(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})$/;
const DATETIME_RE = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2})(?::(\d{2})(\.\d+)?)?\s*(Z|[+-]\d{2}:?\d{2})?$/i;
const MONTH_DAY_YEAR_RE = /^(?:[a-z]+,?\s+)?([a-z]{3,9})\.?\s+(\d{1,2})(?:st|nd|rd|th)?,?\s+(\d{4})$/i;
const DAY_MONTH_YEAR_RE = /^(\d{1,2})(?:st|nd|rd|th)?\s+([a-z]{3,9})\.?,?\s+(\d{4})$/i;
+/** Chat-export style "1:56 pm on 8 May, 2023": the clock time is dropped and the date parsed. */
+const TIME_ON_PREFIX_RE = /^\d{1,2}:\d{2}\s*(?:am|pm)?\s+on\s+/i;
/** A UTC calendar date, or null when Y/M/D does not name a real day (2024-02-30). */
function utcCalendarDate(year: number, month: number, day: number, h = 0, m = 0, sec = 0, ms = 0): Date | null {
@@ -178,9 +180,10 @@ export function parseDateLoose(value: unknown, timeZone?: string): Date | null {
const [zh, zm] = [+offset.slice(1, 3), +offset.slice(-2)];
return new Date(wall.getTime() - sign * (zh * 60 + zm) * 60_000);
}
- const mdy = MONTH_DAY_YEAR_RE.exec(trimmed);
+ const dated = trimmed.replace(TIME_ON_PREFIX_RE, '');
+ const mdy = MONTH_DAY_YEAR_RE.exec(dated);
if (mdy && MONTHS[mdy[1].toLowerCase()]) return utcCalendarDate(+mdy[3], MONTHS[mdy[1].toLowerCase()], +mdy[2]);
- const dmy = DAY_MONTH_YEAR_RE.exec(trimmed);
+ const dmy = DAY_MONTH_YEAR_RE.exec(dated);
if (dmy && MONTHS[dmy[2].toLowerCase()]) return utcCalendarDate(+dmy[3], MONTHS[dmy[2].toLowerCase()], +dmy[1]);
const ms = Date.parse(trimmed);
if (!Number.isFinite(ms)) return null;
@@ -212,7 +215,7 @@ function validateInRange(d: Date | null): Date | null {
function validateExplicit(value: unknown, timeZone?: string): Date | null {
const d = parseDateLoose(value, timeZone);
const structured = value instanceof Date || (typeof value === 'string'
- && [NUMERIC_DATE_RE, DATETIME_RE, MONTH_DAY_YEAR_RE, DAY_MONTH_YEAR_RE].some(re => re.test(value.trim())));
+ && [NUMERIC_DATE_RE, DATETIME_RE, MONTH_DAY_YEAR_RE, DAY_MONTH_YEAR_RE].some(re => re.test(value.trim().replace(TIME_ON_PREFIX_RE, ''))));
if (!structured) return validateInRange(d);
if (d === null) return null;
const ms = d.getTime();
diff --git a/test/effective-date.test.ts b/test/effective-date.test.ts
index c0e84d1471..cee0ecf318 100644
--- a/test/effective-date.test.ts
+++ b/test/effective-date.test.ts
@@ -47,6 +47,11 @@ describe('parseDateLoose', () => {
test('invalid Date → null', () => {
expect(parseDateLoose(new Date('not a date'))).toBeNull();
});
+ test('chat-export "<time> on <day> <Month>, <year>" parses to the calendar day', () => {
+ expect(parseDateLoose('1:56 pm on 8 May, 2023')?.toISOString()).toBe('2023-05-08T00:00:00.000Z');
+ expect(parseDateLoose('10:04 am on March 3, 2024')?.toISOString()).toBe('2024-03-03T00:00:00.000Z');
+ expect(computeEffectiveDate({ slug: 'chat/x', frontmatter: { date: '1:56 pm on 8 May, 2023' }, updatedAt: new Date(), createdAt: new Date() }).source).toBe('date');
+ });
test('unparseable string → null', () => {
expect(parseDateLoose('tomorrow')).toBeNull();
expect(parseDateLoose('garbage')).toBeNull();