Skip to content

Conversation

jasoneggert
Copy link

@jasoneggert jasoneggert commented Sep 12, 2025

Fix AWS Data API timezone handling in Drizzle ORM

Problem

AWS Data API returns timestamps without timezone indicators (e.g., "2024-01-15 10:30:00.123"), causing Drizzle to incorrectly interpret UTC timestamps as local time. This results in wrong relative times like "in 3 hours" instead of "3 hours ago".

Solution

Enhanced mapFromDriverValue in PgTimestamp to detect timezone indicators using regex /[+-]\d{2}:?\d{2}$|Z$/. When timestamptz columns lack timezone info, automatically append 'Z' to force UTC interpretation.

Before:

mapFromDriverValue = (value) => {
    return new Date(this.withTimezone ? value : value + '+0000');
};

After:

mapFromDriverValue = (value) => {
    const hasTimezone = /[+-]\d{2}:?\d{2}$|Z$/.test(value);
    
    if (this.withTimezone && !hasTimezone) {
        return new Date(value + 'Z'); // Force UTC for AWS Data API
    }
    
    return new Date(this.withTimezone ? value : value + '+0000');
};

Impact

  • ✅ Fixes AWS Data API timezone interpretation
  • ✅ Maintains full backward compatibility
  • ✅ Minimal performance impact (single regex test)
  • ✅ No breaking changes

Type of Change

  • Bug fix (AWS Data API compatibility)
  • Enhancement (improved timezone handling)

Just copy and paste this directly into the GitHub PR description field! 🎯

- Add regex-based timezone detection in mapFromDriverValue
- Use explicit UTC 'Z' suffix when no timezone specified for timestamptz
- Fixes AWS Data API incorrectly interpreting UTC timestamps as local time
- Maintains backward compatibility with existing timezone handling
- Addresses issue drizzle-team#1164 and similar AWS Data API timezone problems
- Clean up timestamp.ts.backup and timestamp.ts.bak files
- These were temporary safety backups during development
@jasoneggert jasoneggert marked this pull request as ready for review September 12, 2025 20:23
@jasoneggert jasoneggert changed the base branch from main to beta September 12, 2025 20:26
@jasoneggert jasoneggert changed the base branch from beta to main September 12, 2025 20:28
@jasoneggert jasoneggert changed the base branch from main to beta September 12, 2025 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant