fix: local-time timestamps include offset (#14771) (thanks @0xRaini)
This commit is contained in:
@@ -132,9 +132,8 @@ describe("logs cli", () => {
|
||||
it("formats local time in plain mode when localTime is true", () => {
|
||||
const utcTime = "2025-01-01T12:00:00.000Z";
|
||||
const result = formatLogTimestamp(utcTime, "plain", true);
|
||||
// Should be local time without 'Z' suffix
|
||||
expect(result).not.toContain("Z");
|
||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
|
||||
// Should be local time with explicit timezone offset (not 'Z' suffix).
|
||||
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}[+-]\d{2}:\d{2}$/);
|
||||
// The exact time depends on timezone, but should be different from UTC
|
||||
expect(result).not.toBe(utcTime);
|
||||
});
|
||||
|
||||
@@ -72,11 +72,25 @@ export function formatLogTimestamp(
|
||||
if (Number.isNaN(parsed.getTime())) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const formatLocalIsoWithOffset = (now: Date) => {
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(now.getDate()).padStart(2, "0");
|
||||
const h = String(now.getHours()).padStart(2, "0");
|
||||
const m = String(now.getMinutes()).padStart(2, "0");
|
||||
const s = String(now.getSeconds()).padStart(2, "0");
|
||||
const ms = String(now.getMilliseconds()).padStart(3, "0");
|
||||
const tzOffset = now.getTimezoneOffset();
|
||||
const tzSign = tzOffset <= 0 ? "+" : "-";
|
||||
const tzHours = String(Math.floor(Math.abs(tzOffset) / 60)).padStart(2, "0");
|
||||
const tzMinutes = String(Math.abs(tzOffset) % 60).padStart(2, "0");
|
||||
return `${year}-${month}-${day}T${h}:${m}:${s}.${ms}${tzSign}${tzHours}:${tzMinutes}`;
|
||||
};
|
||||
|
||||
let timeString: string;
|
||||
if (localTime) {
|
||||
const tzoffset = parsed.getTimezoneOffset() * 60000; // offset in milliseconds
|
||||
const localISOTime = new Date(parsed.getTime() - tzoffset).toISOString().slice(0, -1);
|
||||
timeString = localISOTime;
|
||||
timeString = formatLocalIsoWithOffset(parsed);
|
||||
} else {
|
||||
timeString = parsed.toISOString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user