Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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.
Expand Down
1 change: 1 addition & 0 deletions coding/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Coding standards and practices.
- [feature-flags.md](feature-flags.md): Each flag has an owner and an issue that removes it.
- [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
Expand Down
43 changes: 43 additions & 0 deletions coding/time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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`. 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).
42 changes: 42 additions & 0 deletions test/time.test.mjs
Original file line number Diff line number Diff line change
@@ -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");
});