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_utc | The 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_intervals | Day 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_date | Same month and day one year earlier; 29 February gives 28 February (a period's bound) |
previous_year_last(d) | previous_year_last | The 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:
| Kind | First and last day | Shift |
|---|---|---|
py | previous_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 before | that number of months |
pp, otherwise | the same number of days immediately before | the period's length in days |
from:<date>, whole months and the date a first of a month | that number of whole months from the date | the months from the date to the period's first month |
from:<date>, otherwise | that number of days from the date | the 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:
- The day a comparison day counts in (the totals and the buckets):
greatest(day + shift, first day of the period). Adding whole months keeps the day of the month and ends on the last day of a shorter month (PostgreSQL'sdate + interval 'n months',elo_core.comparison.add_months), so 31 March counts in 30 April and 29 February a year on in 28 February; the bound puts the 28 February before a period from 29 February in the first day. Every comparison day is in one bucket. - The cell of a comparison hour (the like-for-like variance): the cell of
day + shiftwith the hour's clock hour and occurrence, when that day has the same day of the month (by days always). 29 February a year on and 31 March against April have none. - The counterpart of a period cell: the cell of
day − shiftwith its clock hour and occurrence, when that day has the same day of the month and the hour (a 23-hour day has no 02:00, only a 25-hour day a second one). 29 to 31 March against February have none. Withpythis isprevious_year_cell, cell for cell (a test checks it for every day of 2023–2028).
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).
- Candidate days: the
lookback_dayslocal days that end yesterday (today − 45 … today − 1). - Active consumption series (
calc.active_consumption_series,is_active): the series of an E17 root (root_mp_id = mp_id,type_code = 'E17') whosecore.metering_point.statusis 1 and whoseingest.mp_statehascurrent_accesstrue and areadable_from, with at least onets.day_loadrow withpoints > 0. An MP with no access (status 3, orcurrent_accessnot true) is not active. - Per day: the active series with
readable_fromon or before the day; coverage = Σ minutes with a quantity ÷ Σ expected minutes (the day's own length per series, so 1 380 and 1 500 on the DST days). Minutes with a quantity areminutes_measured + minutes_estimated + minutes_incomplete + minutes_adjustedofts.day_load(A04, A03, A05, A01; A02 has no quantity). A series with no row that day counts none of them and all the expected minutes. A day on which no series is active is not a candidate. - Result: the latest day with coverage ≥ threshold (
ok); otherwise the day with the highest coverage, the latest among equals (low_coverage); with no candidate day,no_dataand NULLs. A hole before the frontier does not move it (23 of 24 hours is 95.8 %: that day never qualifies, and the frontier goes past it). - Exact comparison:
minutes ≥ threshold × expectedand the ranking of days use integer multiplication in SQL andFractionin Python, never a division or a float, so the two cannot differ in a rounding. mp_idsNULL selects every MP; an ID that does not exist selects nothing.
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.