From 533fb94be6f226e1a0b685219798ead0f3fb29f2 Mon Sep 17 00:00:00 2001 From: HirokiKobayashi-R Date: Thu, 29 Jan 2026 17:46:16 +0900 Subject: [PATCH] fix(logging): use local timezone for console timestamps Console log timestamps now respect the TZ environment variable instead of always using UTC. This only affects pretty-style console output; compact, JSON, and file logs remain in UTC for tool compatibility. Closes #3867 --- src/logging/console.ts | 6 +++--- src/logging/subsystem.ts | 3 ++- src/logging/time.ts | 11 +++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 src/logging/time.ts diff --git a/src/logging/console.ts b/src/logging/console.ts index 0a1218494..cca7af327 100644 --- a/src/logging/console.ts +++ b/src/logging/console.ts @@ -8,6 +8,7 @@ import { type LogLevel, normalizeLogLevel } from "./levels.js"; import { getLogger, type LoggerSettings } from "./logger.js"; import { readLoggingConfig } from "./config.js"; import { loggingState } from "./state.js"; +import { formatLocalHHMMSS } from "./time.js"; export type ConsoleStyle = "pretty" | "compact" | "json"; type ConsoleSettings = { @@ -129,9 +130,8 @@ function isEpipeError(err: unknown): boolean { } function formatConsoleTimestamp(style: ConsoleStyle): string { - const now = new Date().toISOString(); - if (style === "pretty") return now.slice(11, 19); - return now; + if (style === "pretty") return formatLocalHHMMSS(); + return new Date().toISOString(); } function hasTimestampPrefix(value: string): boolean { diff --git a/src/logging/subsystem.ts b/src/logging/subsystem.ts index a156fd8f3..6ed756dc0 100644 --- a/src/logging/subsystem.ts +++ b/src/logging/subsystem.ts @@ -8,6 +8,7 @@ import { isVerbose } from "../globals.js"; import { type LogLevel, levelToMinLevel } from "./levels.js"; import { getChildLogger } from "./logger.js"; import { loggingState } from "./state.js"; +import { formatLocalHHMMSS } from "./time.js"; import { clearActiveProgressLine } from "../terminal/progress-line.js"; type LogObj = { date?: Date } & Record; @@ -152,7 +153,7 @@ function formatConsoleLine(opts: { const displayMessage = stripRedundantSubsystemPrefixForConsole(opts.message, displaySubsystem); const time = (() => { if (opts.style === "pretty") { - return color.gray(new Date().toISOString().slice(11, 19)); + return color.gray(formatLocalHHMMSS()); } if (loggingState.consoleTimestampPrefix) { return color.gray(new Date().toISOString()); diff --git a/src/logging/time.ts b/src/logging/time.ts new file mode 100644 index 000000000..6872692fc --- /dev/null +++ b/src/logging/time.ts @@ -0,0 +1,11 @@ +/** + * Formats the current local time as HH:MM:SS. + * Respects the system TZ environment variable. + */ +export function formatLocalHHMMSS(): string { + const now = new Date(); + const hours = String(now.getHours()).padStart(2, "0"); + const minutes = String(now.getMinutes()).padStart(2, "0"); + const seconds = String(now.getSeconds()).padStart(2, "0"); + return `${hours}:${minutes}:${seconds}`; +}