Eloverblik

Dokumentation · Beregningerne

Time rules and the frontier (B10)

The rules of docs/architecture/phase-1.md §2, once in Python (elo_core.timerules, elo_core.frontier) and once in SQL (calc.*, migration 0060_time_rules). They must agree exactly. Both read the time zone database (Europe/Copenhagen); neither reads a table, so they work for any date. ref.calendar_day (2015–2040) is a cache of the same rule, and a test compares it. Nothing depends on the machine's or the session's time zone: every conversion names Europe/Copenhagen or UTC.

services/api/tests/test_time_rules.py is the guarantee: it compares SQL and Python for every local date of 2015–2040, for random and for dense instants (both DST days and the turn of every year, in three session time zones, one of them a half-hour zone) and for random stores read by the frontier.

The functions

SQL (calc.)Python (elo_core.timerules)What it is
local_date(instant)local_date(instant)The local day of an instant
day_start_utc(d), day_end_utc(d)day_start_utc, day_end_utcThe UTC bounds [start, end) of a local day
expected_minutes(d)expected_minutes(d)1 380, 1 440 or 1 500
expected_intervals(d, resolution)expected_intervalsDay length ÷ resolution (15 or 60): 23/24/25 and 92/96/100. Another resolution is an error (SQLSTATE 22023, ValueError)
hour_cell(instant)hour_cell(instant)The cell of the hour the instant is in: hour_utc, local_date, clock_hour 0–23, occurrence 1 or 2
hour_cells(d)hour_cells(d)The 23, 24 or 25 cells of a local day in time order
previous_year_date(d)previous_year_dateSame month and day one year earlier; 29 February gives 28 February (a period's bound)
previous_year_last(d)previous_year_lastThe last day of a day's previous year: 29 February for 28 February of a year after a leap year, else previous_year_date (docs/queries.md, "The previous year")
previous_year_cell(instant)previous_year_cell(cell)The cell a year earlier with the same month, day, clock hour and occurrence; no row / None for 29 February and for the DST hours that have no counterpart
comparison_period(cmp, from, to, cmp_date)elo_core.comparison.correspondence(kind, from, to, cmp_date)B55: the comparison of a resolved period (py, pp, from): its first and last day and the shift that takes a comparison day to its period day, whole months (py 12) or days (below)

The occurrence is 2 only for the second 02:00 of the 25-hour day. A UTC hour is one local hour (Danish offsets are whole hours), so hour_utc is the key of the factor join; the instant is floored to its UTC hour.

An unmatched cell is never moved to a neighbour: the second 02:00 of 25 October 2026 has no counterpart in 2025 (a 24-hour day), the 02:00 of 29 March 2027 has none in 2026 (no clock hour 2), and no hour of 29 February does. B12a's calc.emission_matched applies the same correspondence without a join: it keys every hour by the cell of the current period (an earlier hour by the cell with the same month, day, clock hour and occurrence a year later, from calc.hour_cell) and marks the cells that have a counterpart from calc.hour_cells of the comparison days (docs/emission.md).

The correspondence of two periods (B55)

The previous year is one comparison of three (phase 2 §3.4; docs/queries.md, "The comparison"). Each is a shift: whole months or days, never both, resolved by calc.comparison_period and in Python by elo_core.comparison.correspondence, with the comparison's first and last day:

KindFirst and last dayShift
pyprevious_year_date(from) .. previous_year_last(to)12 months
pp, the period whole calendar months (from a first to a last of a month)the same number of whole months immediately beforethat number of months
pp, otherwisethe same number of days immediately beforethe period's length in days
from:<date>, whole months and the date a first of a monththat number of whole months from the datethe months from the date to the period's first month
from:<date>, otherwisethat number of days from the datethe days from the date to the period's first day

A pp or from: comparison must end before the period starts: the runner refuses it with 422 comparison_not_earlier before any SQL of the query runs. Of the functions, calc.consumption_matched, calc.emission_buckets and calc.emission_matched refuse it by themselves (22023); calc.consumption_buckets and calc.comparison_buckets (SQL functions, their plans left as they are) do not, and rely on their caller. py is not held to that.

Three rules follow from the shift, the same in SQL and Python:

An unmatched cell is never moved, by any kind: by offset, the second 02:00 of 26 October 2025 has no counterpart on 12 October, and 31 March 2025 against 30 March (23 hours) has no 02:00 to match. A day below the 29th keeps its day under any shift in months, which the functions test first, so that the rule costs one date_part for nearly every hour.

The frontier

calc.frontier(mp_ids int[], today date, threshold numeric DEFAULT 0.98, lookback_days int DEFAULT 45) returns one row: local_date, state (ok, low_coverage, no_data), minutes, expected_minutes, coverage (6 decimals, for display). Python: elo_core.frontier.frontier(series, today, threshold=Fraction, lookback_days=45).

Both functions take today as an argument; the caller passes the local day (elo_core.plan.local_today(now)). The thresholds and the look-back are the settings ELO_FRONTIER_THRESHOLD and ELO_FRONTIER_LOOKBACK_DAYS of §1.3: the caller (B15) reads them and passes them; the functions only carry the defaults.

Grants: elo_api may execute every function and select the view; elo_worker has no use for them in phase 1 and is granted none; PUBLIC is revoked.

Kilde: docs/time-rules.md i løsningens repository.