Writing Conditions on Dates and Times

A condition about a calendar deadline depends on the date, time zone, and interpretation of the endpoint.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
2 min
In this article4 chapters
  1. Compare like units
  2. Build deterministic cases
  3. Write the rule in the calendar it belongs to
  4. Make testing independent of today's clock

Compare like units

If shipping is available until 17:00 local time exclusive, 16:59 is eligible and 17:00 is outside the cutoff. An instant recorded in UTC must be interpreted in the store's zone before reading its local clock time. Unit-aware calculations keep the comparison coherent.

Build deterministic cases

Pass a supplied time into the rule and test both adjacent cases as well as a different day when hours vary. Otherwise the result can change while a test runs. See reproducing program runs for identifying such external inputs.[1][2]

Write the rule in the calendar it belongs to

A pickup can be booked until 17:00 on a working day in the store's time zone. A UTC instant must be converted to the shop's local date and wall-clock time before applying the comparison. The weekday can differ across time zones near midnight. For a close at 17:00 exclusive, verify 16:59 is open and 17:00 is closed on the same working day. Then use a weekend at the same local hour. Those independent tests show both boundaries. Absolute timestamps and local dates explain the representations.

Make testing independent of today's clock

Pass a chosen instant and store policy into the rule. A fixture can then exercise a different date, zone, or opening schedule without waiting. If the operation spans a clock change, compare actual instants for elapsed durations and local calendar values for opening hours. Always specify how ambiguous or absent local times are handled when accepting them as input.

Avoid repeatedly reading the current time for one decision if it could cross the boundary between reads. Capture one observation, name it, and compare each relevant rule against it. See clock dependencies for supplying a deterministic observation.

References

  1. Python Software Foundation, Python Tutorial: Control Flow ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩