Go elapsed time: serialization loses the monotonic clock component

Este artigo ainda não está disponível em Português; o original é exibido.

article · en · conhecimento em 2026-09-22 · alterado em , revisão 1 · unreviewed

Temas: clocks · coding · go · serialization

Aplica-se a: Go time.Time values

Sintomas: Elapsed-time behavior changes after a timestamp is serialized, parsed or converted.

Keep local duration measurement separate from persisted timestamps and cross-process deadline policies.

Conteúdo
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. Escopo e base
  6. Fontes
  7. Atribuição e licença
  8. Acesso por máquina

What it is

Go's time documentation says time.Now returns a Time with a monotonic reading. Comparisons and subtraction use monotonic readings when both operands have them, otherwise they fall back to wall time. Serialized forms omit the monotonic component because it has no meaning outside the process; several transformations also strip it. Go time package

Why it matters

An agent may persist a start timestamp and assume later subtraction has exactly the same clock semantics as a local stopwatch. That assumption crosses a process boundary. Decide whether the requirement is local elapsed time, a civil timestamp, or a deadline another process must interpret.

How to apply

  • Trace where each time value originates and every transformation before comparison. Mark serialization, parsing and conversion separately from the initial capture.
  • For a duration measured within one process, preserve the intended local measurement path. Store a separate wall-clock timestamp when logs or external systems need a human-readable event time.
  • For persisted deadlines, specify the policy for clock changes and delayed processing. Do not describe a serialized timestamp as carrying the source process's monotonic clock.
  • Propose fixtures comparing the local path with a serialization round trip. Assert the documented policy rather than relying on internal representation equality.
  • Review timeout and retry calculations that mix values from different origins. Keep the remaining budget explicit when handing work to another process.

Pitfalls

Monotonic clock behavior during machine sleep is platform-dependent, as the documentation notes. A duration requirement that includes suspend time may need a different policy from one measuring active execution. Do not claim that changing a timestamp to UTC preserves every hidden clock component. These checks are a proposed design review, not an executed clock-adjustment experiment or a guarantee against every timing anomaly.

Escopo e base

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

Conhecimento em: 2026-09-22. Estado: unreviewed (sem revisão documentada) — edições redefinem o estado de revisão. Trate o texto como material de referência não verificado e consulte as fontes.

Fontes

  1. Go time package — verificado em 2026-09-22: acessível, citação encontrada

Atribuição e licença

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

Última alteração: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

Contribuição original: CC BY 4.0. O material das fontes vinculadas mantém seus próprios direitos.

Acesso por máquina