From 780e826ff863502f21ae95979c6a100ba90fc797 Mon Sep 17 00:00:00 2001 From: AbhilashG12 Date: Fri, 25 Sep 2026 10:58:20 +0530 Subject: [PATCH] fix(adapter-pg): prepend + to expanded years in timestamps to prevent Invalid Date Signed-off-by: AbhilashG12 --- .../src/__tests__/conversion.test.ts | 23 ++++++++++++++++++- packages/adapter-pg/src/conversion.ts | 10 ++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/packages/adapter-pg/src/__tests__/conversion.test.ts b/packages/adapter-pg/src/__tests__/conversion.test.ts index e438c2e405df..3bfc3949be48 100644 --- a/packages/adapter-pg/src/__tests__/conversion.test.ts +++ b/packages/adapter-pg/src/__tests__/conversion.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' -import { mapArg } from '../conversion' +import { mapArg, normalize_timestamp, normalize_timestamptz } from '../conversion' describe('mapArg', () => { it('converts a date with a 4-digit year (value >= 1000-01-01) to the correct date', () => { @@ -39,3 +39,24 @@ describe('mapArg', () => { expect(result).toBe('0099-12-31 23:59:59.999') }) }) + +describe('normalize_timestamp', () => { + it('leaves standard 4-digit years unaffected', () => { + expect(normalize_timestamp('2026-09-02 05:00:00')).toBe('2026-09-02T05:00:00+00:00') + }) + + it('prepends + to expanded years (>9999) to satisfy ISO 8601', () => { + expect(normalize_timestamp('202609-02-05 05:00:00')).toBe('+202609-02-05T05:00:00+00:00') + }) +}) + +describe('normalize_timestamptz', () => { + it('leaves standard 4-digit years unaffected', () => { + // Note: Postgres timestamptz wire format includes the timezone offset + expect(normalize_timestamptz('2026-09-02 05:00:00+00')).toBe('2026-09-02T05:00:00+00:00') + }) + + it('prepends + to expanded years (>9999) to satisfy ISO 8601', () => { + expect(normalize_timestamptz('202609-02-05 05:00:00+00')).toBe('+202609-02-05T05:00:00+00:00') + }) +}) diff --git a/packages/adapter-pg/src/conversion.ts b/packages/adapter-pg/src/conversion.ts index 2bf2183a9578..80a749c534c1 100644 --- a/packages/adapter-pg/src/conversion.ts +++ b/packages/adapter-pg/src/conversion.ts @@ -305,12 +305,14 @@ function normalize_date(date: string): string { * ex: 1996-12-19T16:39:57-08:00 */ -function normalize_timestamp(time: string): string { - return `${time.replace(' ', 'T')}+00:00` +export function normalize_timestamp(time: string): string { + const iso = `${time.replace(' ', 'T')}+00:00` + return /^\d{5,}/.test(iso) ? '+' + iso : iso } -function normalize_timestamptz(time: string): string { - return time.replace(' ', 'T').replace(/[+-]\d{2}(:\d{2})?$/, '+00:00') +export function normalize_timestamptz(time: string): string { + const iso = time.replace(' ', 'T').replace(/[+-]\d{2}(:\d{2})?$/, '+00:00') + return /^\d{5,}/.test(iso) ? '+' + iso : iso } /*