feat: standardize timestamps to UTC

This commit is contained in:
Peter Steinberger
2026-01-05 23:02:13 +00:00
parent f790f3f3ba
commit ac3dedaa1b
15 changed files with 140 additions and 54 deletions

View File

@@ -19,12 +19,12 @@ describe("formatAgentEnvelope", () => {
process.env.TZ = originalTz;
expect(body).toMatch(
/^\[WebChat user1 mac-mini 10\.0\.0\.5 2025-01-02T03:04\+00:00\{.+\}\] hello$/,
expect(body).toBe(
"[WebChat user1 mac-mini 10.0.0.5 2025-01-02T03:04Z] hello",
);
});
it("formats timestamps in local time (not UTC)", () => {
it("formats timestamps in UTC regardless of local timezone", () => {
const originalTz = process.env.TZ;
process.env.TZ = "America/Los_Angeles";
@@ -37,9 +37,7 @@ describe("formatAgentEnvelope", () => {
process.env.TZ = originalTz;
expect(body).toBe(
"[WebChat 2025-01-01T19:04-08:00{America/Los_Angeles}] hello",
);
expect(body).toBe("[WebChat 2025-01-02T03:04Z] hello");
});
it("handles missing optional fields", () => {

View File

@@ -12,25 +12,15 @@ function formatTimestamp(ts?: number | Date): string | undefined {
const date = ts instanceof Date ? ts : new Date(ts);
if (Number.isNaN(date.getTime())) return undefined;
const yyyy = String(date.getFullYear()).padStart(4, "0");
const mm = String(date.getMonth() + 1).padStart(2, "0");
const dd = String(date.getDate()).padStart(2, "0");
const hh = String(date.getHours()).padStart(2, "0");
const min = String(date.getMinutes()).padStart(2, "0");
const yyyy = String(date.getUTCFullYear()).padStart(4, "0");
const mm = String(date.getUTCMonth() + 1).padStart(2, "0");
const dd = String(date.getUTCDate()).padStart(2, "0");
const hh = String(date.getUTCHours()).padStart(2, "0");
const min = String(date.getUTCMinutes()).padStart(2, "0");
// getTimezoneOffset() is minutes *behind* UTC. Flip sign to get ISO offset.
const offsetMinutes = -date.getTimezoneOffset();
const sign = offsetMinutes >= 0 ? "+" : "-";
const absOffsetMinutes = Math.abs(offsetMinutes);
const offsetH = String(Math.floor(absOffsetMinutes / 60)).padStart(2, "0");
const offsetM = String(absOffsetMinutes % 60).padStart(2, "0");
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
const tzSuffix = tz ? `{${tz}}` : "";
// Compact ISO-like *local* timestamp with minutes precision.
// Example: 2025-01-02T03:04-08:00{America/Los_Angeles}
return `${yyyy}-${mm}-${dd}T${hh}:${min}${sign}${offsetH}:${offsetM}${tzSuffix}`;
// Compact ISO-like UTC timestamp with minutes precision.
// Example: 2025-01-02T03:04Z
return `${yyyy}-${mm}-${dd}T${hh}:${min}Z`;
}
export function formatAgentEnvelope(params: AgentEnvelopeParams): string {