← Back to PRs

#21859: fix(logs): respect TZ env var for timestamp display, fix Windows timezone

by hydro13 open 2026-02-20 13:26 View on GitHub →
docs cli size: S
## Summary `openclaw logs` always displayed UTC timestamps regardless of the `TZ` environment variable. On Windows especially, Node.js does not reliably read `TZ` to adjust `getHours()` / `getTimezoneOffset()` — the methods used by the old implementation. ## Root Cause `formatLocalIsoWithOffset()` in `src/logging/timestamps.ts` manually assembled a timestamp using `getHours()`, `getMinutes()`, `getTimezoneOffset()` etc. These methods read the process timezone from the OS — which on Windows does not pick up the `TZ` environment variable set by the user. ## Fix ### 1. `src/logging/timestamps.ts` — rewrite using `Intl.DateTimeFormat` Replace the old manual approach with `Intl.DateTimeFormat` using an explicit `timeZone` parameter. `Intl` correctly resolves timezone conversions on all platforms including Windows. - Function now accepts an optional `timeZone` argument, falling back to `process.env.TZ`, then `Intl.DateTimeFormat().resolvedOptions().timeZone` - Added `parseGmtOffset()` helper to normalize `"GMT+8"` → `"+08:00"` format ### 2. `src/cli/logs-cli.ts` — auto-enable local time when `TZ` is set ```typescript // Before const localTime = Boolean(opts.localTime); // After const localTime = Boolean(opts.localTime) || Boolean(process.env.TZ); ``` Users who set `TZ=Asia/Shanghai` now see local timestamps automatically without needing `--local-time`. ## Tests 6 tests in `src/logging/timestamps.test.ts`: | Test | Result | |---|---| | UTC → `+00:00` offset | ✅ | | `Asia/Shanghai` → `+08:00`, correct hour conversion | ✅ | | `America/New_York` winter (EST) → `-05:00` | ✅ | | `America/New_York` summer (EDT) → `-04:00` | ✅ | | Output matches ISO 8601 with offset regex | ✅ | | Source does NOT contain `getHours`, `getMinutes`, `getTimezoneOffset` | ✅ | All 43 tests in `src/logging/` pass. Fixes #21606 <!-- greptile_comment --> <h3>Greptile Summary</h3> Replaced manual timezone offset calculation with `Intl.DateTimeFormat` to properly respect the `TZ` environment variable on all platforms, especially Windows. The implementation correctly uses `timeZone` parameter in `Intl.DateTimeFormat` which resolves timezone conversions reliably across platforms. The PR also auto-enables local time display when `TZ` is set in the environment. <h3>Confidence Score: 5/5</h3> - This PR is safe to merge with minimal risk - The implementation is a well-tested refactoring that replaces platform-dependent Date methods with the standard `Intl.DateTimeFormat` API. All 6 new tests pass including timezone conversion validation, DST handling, and verification that legacy methods are no longer used. The change is backward compatible (optional parameter) and addresses a real cross-platform bug on Windows. - No files require special attention <sub>Last reviewed commit: b39bb3a</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs