feat(backend): pin server timezone to UTC with startup assertion

Belt-and-suspenders, not a bug fix: storage (timestamptz) and timer math are already tz-independent. Add SERVER_TZ env (default UTC) via getServerTimezone(); db/client.js pins the DB session timezone (reads env directly to avoid an import cycle); server.js pins process.env.TZ and asserts at boot that the DB session matches (logs [tz] or a loud warning). Keeps any future date_trunc/::date reporting deterministic and surfaces a misconfigured server early. Documented in backend/CLAUDE.md + .env.example.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-01 22:27:16 +08:00
parent 495eb98787
commit 529a38ae3f
5 changed files with 58 additions and 2 deletions

View File

@@ -11,13 +11,33 @@ import {
import { initFirebase } from './plugins/firebase.js'
import { restoreActiveTimers } from './services/session-timer.service.js'
import { expireStalePaymentRequests, registerPairingSubscriber } from './services/payment.service.js'
import { getXenditConfig } from './services/config.service.js'
import { getXenditConfig, getServerTimezone } from './services/config.service.js'
import { getDb } from './db/client.js'
const PUBLIC_PORT = process.env.PUBLIC_PORT || 3000
const INTERNAL_PORT = process.env.INTERNAL_PORT || 3001
const INTERNAL_HOST = process.env.INTERNAL_HOST || '127.0.0.1'
const start = async () => {
// Timezone assurance. Storage (timestamptz) and our instant-based timer math
// are timezone-independent, so this is belt-and-suspenders, not a fix for a
// live bug: it pins the Node process timezone for any Date formatting and
// then asserts the DB session timezone matches, so a misconfigured server/DB
// surfaces loudly at boot instead of silently skewing future date_trunc /
// ::date style queries. Defaults to UTC; override via SERVER_TZ.
const serverTz = getServerTimezone()
process.env.TZ = serverTz
const [dbTz] = await getDb()`SHOW timezone`
if (dbTz?.TimeZone !== serverTz) {
console.warn(
`[tz] WARNING: DB session timezone is "${dbTz?.TimeZone}" but SERVER_TZ="${serverTz}". ` +
'timestamptz storage is unaffected, but session-tz-dependent SQL may skew. ' +
'Check the DB server default / connection options.'
)
} else {
console.log(`[tz] process + DB session pinned to ${serverTz}`)
}
// Phase 5: fail fast if XENDIT_ENABLED=true without the required credentials.
// Bad config explodes at startup rather than at the first /payment-requests POST.
const xc = getXenditConfig()