diff --git a/AGENTS.md b/AGENTS.md index c1942ec5..0c48ba42 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -32,7 +32,7 @@ maintainable code, testability, and type safety. itself, and boundaries where a third-party API needs its own type (pandas, NumPy, the standard library), converted right at the boundary. Applies to new and changed code; migrate existing direct calls only where the change touches - them anyway. + them anyway. See the `timezones` skill for which zone to use. - **Optimization parameters are always valid**: their preparation never fails and never cancels an optimization run. See the `energy-domain` skill. - Commits written or co-written by an agent end with the trailer @@ -55,6 +55,7 @@ Detailed rules live in `ai/skills/`. Load the relevant skill for the task: | `ai/skills/python-quality/SKILL.md` | Writing/modifying Python | | `ai/skills/rest-api/SKILL.md` | Adding/changing REST endpoints, API error responses | | `ai/skills/testing/SKILL.md` | Adding/updating tests, bug fixes | +| `ai/skills/timezones/SKILL.md` | Date-times, time zones, host-independent results | ### Engineering skills diff --git a/ai/skills/timezones/SKILL.md b/ai/skills/timezones/SKILL.md new file mode 100644 index 00000000..973053f2 --- /dev/null +++ b/ai/skills/timezones/SKILL.md @@ -0,0 +1,62 @@ +--- +name: timezones +description: Use when code or tests create, parse, render or compare date-times, choose a time zone, or depend on the host's time zone. +--- + +# Time zones + +`akkudoktoreos.utils.datetimeutil` is the single place for time zones, as the +rule in `AGENTS.md` says. This skill holds what that rule leaves out: which +zone to use and how to keep results independent of the host. + +## Which zone + +- **The location's zone** is `config.general.timezone`: `timezone_override`, or + the zone looked up from latitude and longitude. Use it for local dates, day + boundaries, wall-clock times and data shown to the user. +- **UTC** when there is no location zone, and for stored or exchanged instants. +- **The data's own zone** when data arrives with one: take it with + `to_timezone(date_time=...)` (zoneinfo, pytz and Pendulum zones by name, fixed + offsets such as pandas' `UTC+02:00` as their `Etc/GMT` zone). +- **The host's zone** (`to_timezone()` without arguments, `to_datetime()` + without `in_timezone`) is only a last resort for naive input that has no other + source. Results must not change with the host's `TZ`; CI runs in UTC, + installations mostly elsewhere. + +## Looking zones up + +- From a location: `to_timezone(location=(latitude, longitude))`. This is the + only use of `tzfpy`; call it through `to_timezone`, not directly. +- From a UTC offset: `to_timezone(utc_offset=...)` gives the `Etc/GMT` zone of + whole-hour offsets from −12 to +14 and raises `ValueError` otherwise (5:30, + 9:45). The `Etc/GMT` sign is inverted: UTC+5 is `Etc/GMT-5`. An offset names + no daylight saving time, so prefer the location's zone when there is one. +- `to_timezone` returns names that mean UTC (`GMT`, `Etc/GMT`) as `"UTC"`; + `to_datetime(in_timezone=...)` keeps the name it is given. + +## Parsing and rendering + +- A string without an offset is wall-clock time in `in_timezone`, or in the + host's zone without it. Pass the zone the string was meant in. +- A naive `datetime` or pandas `Timestamp` is taken as UTC, a naive string as + local time. Make input aware at the boundary where it enters. +- Render with `to_datetime(..., as_string=True, in_timezone=zone)`. The same + instant renders as `...Z` in `UTC` and `...+00:00` in `Etc/UTC`, so + compare parsed instants (`compare_datetimes`), not strings. +- In Pydantic models, a validator cannot read a field declared after the one it + validates (`info.data` only holds earlier fields). Apply a zone held in a + later field in a `model_validator(mode="after")`. +- Do day arithmetic (start of day, hours in a day, adding days) on date-times + in the location's zone: days have 23 or 25 hours at DST changes. + `hours_in_day(dt)` counts in the zone of `dt`, without `dt` in the host's. + +## Tests + +- Run behaviour that depends on a zone under more than one host zone: the + `set_other_timezone` fixture in `tests/conftest.py` sets Pendulum's local + zone for one test and restores it. +- Golden files whose text depends on the host's zone (naive input, the + host-zone fallback) pin the host zone, so they hold the same text on every + host. +- Before declaring a time-zone change done, run its tests with `TZ=UTC` and + with `TZ=Europe/Berlin`. diff --git a/docs/develop/ai.md b/docs/develop/ai.md index f81697b8..9d4ce6ee 100644 --- a/docs/develop/ai.md +++ b/docs/develop/ai.md @@ -50,6 +50,7 @@ Agents get their project knowledge from two places in the repository: | `python-environment` | How the agent runs `uv`, `make`, pytest, mypy and ruff without breaking your `.venv` | | `python-quality` | Ruff, mypy, typing and docstring rules | | `testing` | Where and how to add tests | +| `timezones` | Which time zone to use, rendering date-times, tests independent of the host's time zone | | `filename-conventions` | File name rules enforced by `scripts/lint_filenames.py` | | `application-configuration` | Configuration models, defaults and migration | | `energy-domain` | Optimization, adapters, units and sign conventions |