fix: parse SQLite timestamps as UTC, not local time
SQLite datetime('now') returns 'YYYY-MM-DD HH:MM:SS' with no timezone
marker. JS was treating this as local time, so timestamps showed the
correct UTC digits but with the local TZ abbreviation attached (e.g.
'7:15 PM EDT' when the real local time was '3:15 PM EDT').
Add parseUtc() which appends 'Z' before parsing any string that has no
existing timezone marker, ensuring JS always treats them as UTC before
the display-timezone conversion is applied.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -58,16 +58,22 @@ describe('esc', () => {
|
||||
|
||||
// ── fmtDate() ─────────────────────────────────────────────────────────────────
|
||||
|
||||
function fmtDate(d) {
|
||||
function parseUtc(d) {
|
||||
if (typeof d !== 'string') return new Date(d)
|
||||
const hasZone = d.endsWith('Z') || /[+-]\d{2}:\d{2}$/.test(d)
|
||||
return new Date(hasZone ? d : d.replace(' ', 'T') + 'Z')
|
||||
}
|
||||
|
||||
function fmtDate(d, tz = 'UTC') {
|
||||
if (!d) return '—'
|
||||
try {
|
||||
return new Date(d).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' })
|
||||
return parseUtc(d).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric', timeZone: tz })
|
||||
} catch (e) { return d }
|
||||
}
|
||||
|
||||
describe('fmtDate', () => {
|
||||
it('formats a valid ISO date string', () => {
|
||||
const result = fmtDate('2024-03-15T00:00:00')
|
||||
const result = fmtDate('2024-03-15T12:00:00Z')
|
||||
expect(result).toMatch(/Mar/)
|
||||
expect(result).toMatch(/15/)
|
||||
expect(result).toMatch(/2024/)
|
||||
@@ -91,7 +97,7 @@ describe('fmtDate', () => {
|
||||
function fmtDateFull(d, tz = 'UTC') {
|
||||
if (!d) return '—'
|
||||
try {
|
||||
return new Date(d).toLocaleString('en-US', {
|
||||
return parseUtc(d).toLocaleString('en-US', {
|
||||
year: 'numeric', month: 'short', day: 'numeric',
|
||||
hour: '2-digit', minute: '2-digit',
|
||||
timeZone: tz, timeZoneName: 'short',
|
||||
@@ -118,6 +124,12 @@ describe('fmtDateFull', () => {
|
||||
expect(result).toMatch(/EDT/)
|
||||
})
|
||||
|
||||
it('treats SQLite-format timestamps (space, no Z) as UTC', () => {
|
||||
// SQLite datetime('now') → 'YYYY-MM-DD HH:MM:SS', no timezone marker.
|
||||
// Must parse identically to the same moment expressed as ISO UTC.
|
||||
expect(fmtDateFull('2024-03-15 18:30:00', 'UTC')).toBe(fmtDateFull('2024-03-15T18:30:00Z', 'UTC'))
|
||||
})
|
||||
|
||||
it('returns — for null', () => {
|
||||
expect(fmtDateFull(null)).toBe('—')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user