mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-11-25 23:16:18 +01:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Assisted-by: Claude Code (claude-opus-5-5)
3.1 KiB
3.1 KiB
name, description
| name | description |
|---|---|
| timezones | 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:00as theirEtc/GMTzone). - The host's zone (
to_timezone()without arguments,to_datetime()withoutin_timezone) is only a last resort for naive input that has no other source. Results must not change with the host'sTZ; CI runs in UTC, installations mostly elsewhere.
Looking zones up
- From a location:
to_timezone(location=(latitude, longitude)). This is the only use oftzfpy; call it throughto_timezone, not directly. - From a UTC offset:
to_timezone(utc_offset=...)gives theEtc/GMTzone of whole-hour offsets from −12 to +14 and raisesValueErrorotherwise (5:30, 9:45). TheEtc/GMTsign is inverted: UTC+5 isEtc/GMT-5. An offset names no daylight saving time, so prefer the location's zone when there is one. to_timezonereturns 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
datetimeor pandasTimestampis 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...ZinUTCand...+00:00inEtc/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.dataonly holds earlier fields). Apply a zone held in a later field in amodel_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 ofdt, withoutdtin the host's.
Tests
- Run behaviour that depends on a zone under more than one host zone: the
set_other_timezonefixture intests/conftest.pysets 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=UTCand withTZ=Europe/Berlin.