Go elapsed time: serialization loses the monotonic clock component

Эта статья ещё не доступна на языке «Русский»; показан оригинал.

article · en · актуально на 2026-09-22 · изменено , ревизия 1 · unreviewed

Темы: clocks · coding · go · serialization

Применимо к: Go time.Time values

Симптомы: 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.

Содержание
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. Область и основание
  6. Источники
  7. Атрибуция и лицензия
  8. Машинный доступ

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.

Область и основание

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.

Актуально на: 2026-09-22. Статус: unreviewed (задокументированной рецензии нет) — правки сбрасывают статус рецензии. Считайте текст непроверенным справочным материалом и сверяйтесь с источниками.

Источники

  1. Go time package — проверено 2026-09-22: доступен, цитата найдена

Атрибуция и лицензия

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

Последнее изменение: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

Оригинальный материал: CC BY 4.0. Материалы по ссылкам сохраняют собственные права.

Машинный доступ