Let each user pick the timezone timestamps are displayed in

Everything stays UTC: ingest still records Unix nanoseconds, ClickHouse
still stores UTC, every API response is still RFC3339 with a Z, and
queries are evaluated exactly as before. This changes only how those
instants are written on screen, so two people in two timezones looking
at one log line see the same instant written two ways -- never two
different lines, and never a different sort order.

Where the preference lives differs by deployment, and the three cases
are genuinely different products rather than one with fallbacks:

  - Local login: server-side per named user (display_timezone on users,
    PUT /auth/timezone), so it follows the person across browsers and
    survives logout. Self-service at the RoleViewer floor, same as the
    password change -- a viewer is the role most likely to be *only*
    reading logs, so gating it higher would make it useless.
  - Public demo: sessionStorage, so every new session starts at UTC. A
    shared account's visitors have nothing to do with each other.
  - Neither: localStorage, since there's no per-user record to write to.

api/cmd/api/main.go now imports time/tzdata. The image is
distroless/static with no /usr/share/zoneinfo, so LoadLocation would
otherwise reject every real zone name and the validation would refuse
every valid input.

Two details worth knowing when reading $lib/time.ts. Sub-second digits
are copied verbatim from the source string rather than round-tripped
through a JS Date, which is millisecond-precision and would silently
drop six digits of a ClickHouse nanosecond timestamp; expanding a result
row shows the localized value and the full-precision UTC original
together. And chart axes format their own labels, because ECharts'
type: 'time' axis renders in the browser's zone with no override --
which today puts a chart's clock out of step with the table beside it.

Timestamps are detected by value, not by column name: query output is
arbitrary, so a column called "timestamp" holding something else must
not be mangled, and `stats max(timestamp) as newest` must still be
formatted.

Verified against real zones including both sides of a DST boundary
(America/New_York at -05:00 in January, -04:00 in July), a half-hour
offset, and date rollover.
This commit is contained in:
2026-08-22 16:15:16 -07:00
parent 04e83f64a9
commit 6ee918d15f
20 changed files with 906 additions and 49 deletions
+69
View File
@@ -25,6 +25,7 @@ type store interface {
DeleteUser(ctx context.Context, id string) error
SetPasswordHash(ctx context.Context, userID, hash string) error
SetRole(ctx context.Context, userID string, role authz.Role) error
SetDisplayTimezone(ctx context.Context, userID, tz string) error
CountUsersWithRole(ctx context.Context, role authz.Role) (int, error)
CreateSession(ctx context.Context, userID, tenantID string, role authz.Role, ttl time.Duration) (string, error)
DeleteSessionByHash(ctx context.Context, tokenHash string) error
@@ -113,6 +114,11 @@ func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /auth/logout", h.handleLogout)
mux.HandleFunc("GET /auth/session", authz.RequireRole(h.authorizer, authz.RoleViewer, h.handleGetSession))
mux.HandleFunc("POST /auth/password", authz.RequireRole(h.authorizer, authz.RoleViewer, h.handleChangeOwnPassword))
// Self-service, same RoleViewer floor as the password change above:
// how a user's own clock is rendered is nobody else's permission to
// grant, and a Viewer -- the role most likely to be *only* reading
// logs -- needs it most.
mux.HandleFunc("PUT /auth/timezone", authz.RequireRole(h.authorizer, authz.RoleViewer, h.handleSetTimezone))
mux.HandleFunc("GET /auth/users", authz.RequireRole(h.authorizer, authz.RoleAdmin, h.handleListUsers))
mux.HandleFunc("POST /auth/users", authz.RequireRole(h.authorizer, authz.RoleAdmin, h.handleCreateUser))
@@ -138,6 +144,12 @@ type sessionResponse struct {
TenantID string `json:"tenant_id"`
Username string `json:"username"`
Role string `json:"role"`
// Timezone is the user's stored display preference (IANA zone name,
// "UTC" by default). Sent on the session so the web UI knows which
// offset to render in from its very first paint, without a second
// round trip -- omitted from the login response, where the store
// lookup that produces it hasn't happened.
Timezone string `json:"timezone,omitempty"`
}
func (h *Handler) handleLogin(w http.ResponseWriter, r *http.Request) {
@@ -217,6 +229,7 @@ func (h *Handler) handleGetSession(w http.ResponseWriter, r *http.Request) {
}
writeJSON(w, http.StatusOK, sessionResponse{
UserID: user.ID, TenantID: identity.TenantID, Username: user.Username, Role: string(user.Role),
Timezone: user.DisplayTimezone,
})
}
@@ -542,6 +555,62 @@ func (h *Handler) handleChangeOwnPassword(w http.ResponseWriter, r *http.Request
w.WriteHeader(http.StatusNoContent)
}
type setTimezoneRequest struct {
Timezone string `json:"timezone"`
}
// maxTimezoneLen bounds the input before it reaches LoadLocation, which
// takes the value as a filesystem-ish lookup key. The longest real IANA
// name is well under this ("America/Argentina/ComodRivadavia", 31).
const maxTimezoneLen = 64
// handleSetTimezone stores the caller's own display-timezone preference
// -- see metadata/migrations/0042_add_user_display_timezone.sql for why
// this is presentation-only and can never affect what data a query
// returns.
//
// Validation is time.LoadLocation against the tzdata embedded in this
// binary (see the time/tzdata import in cmd/api/main.go), not a
// hand-maintained allowlist: the set of valid zone names is the tz
// database's to define, and it changes a few times a year. Rejecting
// unknown names here matters because the value is echoed back to every
// client on the session response -- an unvalidated string would just be
// a stored round trip for whatever someone put in.
func (h *Handler) handleSetTimezone(w http.ResponseWriter, r *http.Request) {
var req setTimezoneRequest
if !decodeJSON(w, r, &req) {
return
}
if req.Timezone == "" {
writeError(w, http.StatusBadRequest, "timezone must not be empty")
return
}
if len(req.Timezone) > maxTimezoneLen {
writeError(w, http.StatusBadRequest, "timezone is not a valid IANA zone name")
return
}
// "Local" is a valid LoadLocation argument but means "whatever zone
// the *server* process is in", which is meaningless as a per-user
// display preference and would render differently depending on which
// host answered. The browser's own zone is the client's business to
// resolve into a real name before sending it.
if req.Timezone == "Local" {
writeError(w, http.StatusBadRequest, "timezone must be a specific IANA zone name, not \"Local\"")
return
}
if _, err := time.LoadLocation(req.Timezone); err != nil {
writeError(w, http.StatusBadRequest, "timezone is not a valid IANA zone name")
return
}
identity, _ := authz.IdentityFromContext(r.Context())
if err := h.store.SetDisplayTimezone(r.Context(), identity.UserID, req.Timezone); err != nil {
h.writeStoreErr(w, err, "setting timezone")
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *Handler) setCookie(w http.ResponseWriter, raw string, ttl time.Duration) {
http.SetCookie(w, &http.Cookie{
Name: sessionCookieName,