From 8e5b52076ce59aec1b6119358ab02da92ae5931f Mon Sep 17 00:00:00 2001 From: Daniel Kindl Date: Wed, 25 Nov 2026 17:01:56 +0100 Subject: [PATCH 1/2] docs(coding): add the time and dates rule Closes #366 --- CHANGELOG.md | 1 + coding/index.md | 1 + coding/time.md | 42 ++++++++++++++++++++++++++++++++++++++++++ test/time.test.mjs | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 coding/time.md create mode 100644 test/time.test.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 13bf9cc..a943f35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ change that a consumer must act on has a "Consumer action:" line. - `setup-quality-gates` copies `check-title`, `check-dashes` and `check-writing` to a consumer, with `markdown-files.mjs` and `pr-event.mjs` that they import (#304). Step 4 gives the title steps of `validate` as the CI example. A test pins the list and runs each script alone. +- `coding/time.md` (#366). Store an instant in UTC as RFC 3339 text. Store a future civil time with its IANA time zone ID. Domain code gets a clock, a duration uses a monotonic clock, and calendar arithmetic uses the platform library. - `scripts/check-maps.mjs --diff` (#317). A change to `docs/maps/` fails when it also changes a file outside `docs/maps/`, `docs/adr/` and `CONTEXT.md`, or when it deletes a map. The `validate` workflow runs it. diff --git a/coding/index.md b/coding/index.md index d549bf6..b817fe5 100644 --- a/coding/index.md +++ b/coding/index.md @@ -14,6 +14,7 @@ Coding standards and practices. - [security.md](security.md): Security basics. - [testing.md](testing.md): A pointer to [tdd/](../tdd/index.md), which has the testing rules. - [dependencies.md](dependencies.md): No unjustified dependencies. +- [time.md](time.md): Time and dates in production code. - [debugging.md](debugging.md): Reproduce the problem and fix the cause. ## Application diff --git a/coding/time.md b/coding/time.md new file mode 100644 index 0000000..d880a39 --- /dev/null +++ b/coding/time.md @@ -0,0 +1,42 @@ +# Time and dates + +Keep an instant in UTC. Keep the time zone of a civil time. Get the time from a clock that tests +can control. + +Apply this page to production code. For time in tests, see +[tdd/deterministic-tests.md](../tdd/deterministic-tests.md). + +## Rules + +- Store and send an instant in UTC, for example a log time or the time of a payment. Use RFC 3339 + text with an offset, for example `2026-11-25T14:30:00Z`. Reason: one instant has one value in + each system, and the text sorts in time order. +- Convert an instant to local time only to show it to a person. Format it for the user's locale. + See [content and localization](../ui/content-and-localization.md). +- Store a future civil time with its IANA time zone ID, for example `Europe/Prague`, not only with + an offset. Examples are a meeting or the time a shop opens. Reason: the offset of a zone changes + with daylight saving time and with the law. The ID gives the right offset at that date. +- Do not read the system clock in domain code. Give the code a clock, for example `TimeProvider` + in .NET or `java.time.Clock` in Java, or a function that returns the current time. Reason: a + test can then set the time. See [tdd/deterministic-tests.md](../tdd/deterministic-tests.md). +- Measure a duration with a monotonic clock, for example `performance.now()` in JavaScript, + `time.monotonic()` in Python or `Stopwatch` in .NET. Reason: the wall clock can jump forward + or back when the system sets the time. +- Use the date and time library of the platform for calendar arithmetic and parsing. Do not add + `86400` seconds for "one day" and do not parse dates with your own code. Reason: a day in a + zone with daylight saving time can have 23 or 25 hours. + +## Good example + +A booking service stores `startsAt: "2027-03-28T09:00"` with `zone: "Europe/Prague"`. The +reminder job gets a clock. It computes the instant with the time zone library at the time of the +reminder. A test sets the clock to the night of the daylight saving change and checks the +reminder time. + +## Bad example + +A booking service stores the start as `2027-03-28T09:00+01:00`. The clocks in Prague change to +`+02:00` that night. The reminder comes one hour late. The job calls `new Date()` directly, so no +test can set the time to that night. + +Related: [configuration](../architecture/configuration.md). diff --git a/test/time.test.mjs b/test/time.test.mjs new file mode 100644 index 0000000..b600a00 --- /dev/null +++ b/test/time.test.mjs @@ -0,0 +1,42 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { fileURLToPath } from "node:url"; +import { dirname, join } from "node:path"; +import { readFile } from "./helpers.mjs"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); + +async function text(path) { + return readFile(join(root, path), "utf8"); +} + +test("coding/time.md states the time and date rules", async () => { + const page = await text("coding/time.md"); + for (const part of [ + "# Time and dates", + "UTC", + "RFC 3339", + "IANA time zone", + "Europe/Prague", + "clock", + "TimeProvider", + "java.time.Clock", + "monotonic", + "performance.now()", + "time.monotonic()", + "tdd/deterministic-tests.md", + "86400", + ]) { + assert.ok(page.includes(part), `missing ${part}`); + } +}); + +test("coding/time.md has a good and a bad example", async () => { + const page = await text("coding/time.md"); + assert.ok(page.includes("## Good example"), "missing good example"); + assert.ok(page.includes("## Bad example"), "missing bad example"); +}); + +test("coding/index.md lists coding/time.md", async () => { + assert.ok((await text("coding/index.md")).includes("(time.md)"), "coding/index.md does not list the page"); +}); From 429477f794f564e14759f187b3e1ca691563857a Mon Sep 17 00:00:00 2001 From: Daniel Kindl Date: Wed, 25 Nov 2026 17:17:16 +0100 Subject: [PATCH 2/2] docs(time): state the instants in the bad example --- coding/time.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/coding/time.md b/coding/time.md index d880a39..45452c2 100644 --- a/coding/time.md +++ b/coding/time.md @@ -35,8 +35,9 @@ reminder time. ## Bad example -A booking service stores the start as `2027-03-28T09:00+01:00`. The clocks in Prague change to -`+02:00` that night. The reminder comes one hour late. The job calls `new Date()` directly, so no -test can set the time to that night. +A booking service stores the start as `2027-03-28T09:00+01:00`. That is 08:00 UTC. The user meant +09:00 in Prague. The clocks in Prague changed to `+02:00` at 02:00 that night, so 09:00 in Prague +is 07:00 UTC. The reminder comes one hour late. The job calls `new Date()` directly, so no test +can set the time to that night. Related: [configuration](../architecture/configuration.md).