From 07fb3b455c8a0ff18cf324b7ffbd37eb21ac2f43 Mon Sep 17 00:00:00 2001 From: Nikolai Giman Date: Sat, 7 Nov 2026 22:00:02 +0100 Subject: [PATCH] fix: tz --- api/dockerfile | 2 + api/src/App.ts | 2 + .../__tests__/process-timezone.spec.ts | 46 +++++++++++++++++++ api/src/modules/process-timezone.ts | 20 ++++++++ 4 files changed, 70 insertions(+) create mode 100644 api/src/modules/__tests__/process-timezone.spec.ts create mode 100644 api/src/modules/process-timezone.ts diff --git a/api/dockerfile b/api/dockerfile index 9bb5813..741c0c4 100644 --- a/api/dockerfile +++ b/api/dockerfile @@ -4,6 +4,8 @@ RUN apk add --no-cache curl RUN npm install pm2 -g +ENV TZ=UTC + WORKDIR /usr/src/app COPY ./dist . diff --git a/api/src/App.ts b/api/src/App.ts index e216557..e2b0a71 100644 --- a/api/src/App.ts +++ b/api/src/App.ts @@ -10,6 +10,7 @@ import SessionStorage from './tv-modules/auth/SessionStorage'; import { AccountDeletion } from './tv-modules/auth/AccountDeletion'; import { InviteEmailDispatcher } from './tv-modules/collaboration/InviteEmailDispatcher'; import { PublicApiUrl } from './modules/public-url'; +import { ProcessTimezone } from './modules/process-timezone'; import { validateWebhooksEnvOnStartup } from './tv-modules/webhooks/webhooks.utils'; import { validateSsoEnvOnStartup } from './tv-modules/sso/sso.utils'; import { validateImportEnvOnStartup } from './tv-modules/import/import.utils'; @@ -23,6 +24,7 @@ export default class App { public port: number; constructor(port: number) { + ProcessTimezone.enforceOnStartup(); LoginMethods.validateOnStartup(); SessionStorage.validateOnStartup(); AccountDeletion.validateOnStartup(); diff --git a/api/src/modules/__tests__/process-timezone.spec.ts b/api/src/modules/__tests__/process-timezone.spec.ts new file mode 100644 index 0000000..e678c65 --- /dev/null +++ b/api/src/modules/__tests__/process-timezone.spec.ts @@ -0,0 +1,46 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { ProcessTimezone } from '../process-timezone'; + +vi.mock('../logget', () => ({ $logger: { warn: vi.fn() } })); + +describe('ProcessTimezone', () => { + const original = process.env.TZ; + + afterEach(() => { + process.env.TZ = original; + vi.restoreAllMocks(); + }); + + it('switches a process started in another time zone to UTC', () => { + process.env.TZ = 'Asia/Tokyo'; + vi.spyOn(console, 'warn').mockImplementation(() => {}); + expect(new Date('2026-01-01T00:00:00Z').getHours()).toBe(9); + + ProcessTimezone.enforceOnStartup(); + + expect(process.env.TZ).toBe('UTC'); + expect(new Date('2026-01-01T00:00:00Z').getHours()).toBe(0); + expect(new Date(2026, 0, 1).toISOString()).toBe('2026-01-01T00:00:00.000Z'); + }); + + it('warns that a non-UTC TZ is ignored', () => { + process.env.TZ = 'Europe/Berlin'; + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + ProcessTimezone.enforceOnStartup(); + + expect(warn).toHaveBeenCalledWith(expect.stringContaining('TZ="Europe/Berlin" is ignored')); + }); + + it('stays quiet when TZ is already UTC or unset', () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + process.env.TZ = 'UTC'; + ProcessTimezone.enforceOnStartup(); + delete process.env.TZ; + ProcessTimezone.enforceOnStartup(); + + expect(warn).not.toHaveBeenCalled(); + expect(process.env.TZ).toBe('UTC'); + }); +}); diff --git a/api/src/modules/process-timezone.ts b/api/src/modules/process-timezone.ts new file mode 100644 index 0000000..af1095b --- /dev/null +++ b/api/src/modules/process-timezone.ts @@ -0,0 +1,20 @@ +import { $logger } from './logget'; + +export const PROCESS_TIMEZONE = 'UTC'; + +export class ProcessTimezone { + /** + * Columns are TIMESTAMP without time zone, and node-postgres reads and writes + * them in the process time zone — so the process must run in UTC no matter + * what TZ the host or container was given. + */ + static enforceOnStartup(): void { + const configured = process.env.TZ; + if (configured && configured !== PROCESS_TIMEZONE) { + const message = `[timezone] TZ="${configured}" is ignored - the API always runs in ${PROCESS_TIMEZONE}`; + console.warn(message); + $logger.warn(message); + } + process.env.TZ = PROCESS_TIMEZONE; + } +}