SystemClock: support explicit DateTimeZone in constructor #3

Open
opened 2026-07-26 19:13:23 +02:00 by claudeCode · 0 comments
Member

Problem

SystemClock::now() uses new \DateTimeImmutable() which picks up the PHP/system default timezone. Code that needs to work in a specific timezone (e.g. always UTC in background jobs) must either set the global default or wrap the result — both are error-prone.

Proposed API

$clock = new SystemClock(new \DateTimeZone('UTC'));
$clock = new SystemClock(new \DateTimeZone('Europe/Berlin'));
$clock = new SystemClock(); // existing behavior, system default

When a DateTimeZone is provided, now() returns new \DateTimeImmutable('now', $timezone).

Benefits

  • Services that require a fixed timezone can declare it at construction time
  • No global date_default_timezone_set() side effects
  • Constructor default keeps backwards compatibility

Acceptance criteria

  • __construct(?\DateTimeZone $timezone = null)
  • now() uses provided timezone when set
  • now() falls back to system default when null
  • Test coverage for UTC, named timezone, and default behavior
## Problem `SystemClock::now()` uses `new \DateTimeImmutable()` which picks up the PHP/system default timezone. Code that needs to work in a specific timezone (e.g. always UTC in background jobs) must either set the global default or wrap the result — both are error-prone. ## Proposed API ```php $clock = new SystemClock(new \DateTimeZone('UTC')); $clock = new SystemClock(new \DateTimeZone('Europe/Berlin')); $clock = new SystemClock(); // existing behavior, system default ``` When a `DateTimeZone` is provided, `now()` returns `new \DateTimeImmutable('now', $timezone)`. ## Benefits - Services that require a fixed timezone can declare it at construction time - No global `date_default_timezone_set()` side effects - Constructor default keeps backwards compatibility ## Acceptance criteria - `__construct(?\DateTimeZone $timezone = null)` - `now()` uses provided timezone when set - `now()` falls back to system default when `null` - Test coverage for UTC, named timezone, and default behavior
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: tez/tez-utils-clock#3