Expand description
Splitting a session across quarter hours, exactly.
§Why this exists
A charging session that runs from 10:07 to 11:23 has to be settled against
six quarter hours. Under German pass-through charging (NZR-EMob / Modell 2)
the operator assigns each quarter hour’s energy to the balance group of the
supplier the driver chose, quarter hour by quarter hour [A6 §IV.1], and
mako-emob will not accept a set of numbers that does not add up.
So this module has one hard requirement and one honest one.
§Conservation is by construction, not by reconciliation
The naive approach computes each slot’s energy independently and then discovers the sum is a few milliwatt-hours off the session total, because rounding happened six times. The usual fix is to shove the difference into the last slot, which silently misattributes energy to whoever held 11:15.
Instead, this computes the cumulative energy at each quarter-hour boundary once, and takes differences:
slot[i] = cumulative(boundary[i+1]) − cumulative(boundary[i])The sum telescopes: every interior boundary appears once positive and once
negative and cancels exactly, whatever it was rounded to. What is left is
cumulative(end) − cumulative(start), which is the session total, to the
last digit, always. SessionSplit::conserves proves it, and a property
test runs it over hundreds of generated sessions.
§The grid is not the only thing that cuts a session
A quarter hour is where the energy settles. It is not where the price
changes: [AFIR Art. 5(4)] lets a fast charger add an occupancy fee per
minute for the time a vehicle is connected and not charging, and a
vehicle stops charging when it stops charging, not at :15:00. A slot that
ran from 10:15 to 10:30 with the charge finishing at 10:20 is ten minutes of
occupancy and five minutes of charging, and one flag cannot say so.
So into_periods takes extra cut instants beside the grid — the session’s
own state changes, for the caller that has them — and every one of them is
just another boundary in the same telescoping sum. Conservation is
unaffected: interior boundaries cancel whatever they were rounded to,
wherever they fall. Session::split passes the session’s history, so a
CDR built from it prices each minute at the rate that minute earned.
§Where the numbers come from is recorded
A boundary that a Sample.Clock reading landed on exactly is
Provenance::Measured. A boundary between two readings is
Provenance::Interpolated — and that assumes constant power across the
gap, which a tapering charge curve does not deliver. The error lands on
whichever supplier held the boundary, so the assumption travels with the
number rather than being forgotten.
§…and constant power means constant power while charging
A straight line between two readings is the honest guess across a gap the
session says nothing about. It is a wrong guess across a gap the session
says something about: a transaction that opens EVConnected, starts
charging thirty seconds later and sends its next meter value at the quarter
hour — the ordinary OCPP 2.0.1 shape — has a register that did not move for
those thirty seconds, and a line drawn from the opening reading attributes
thirty seconds’ worth of energy to an interval the operator’s own state
machine calls suspended. The CDR builder then refuses the record for a
contradiction the arithmetic invented.
So into_periods takes the session’s idle intervals beside its cuts,
and interpolates over charging time only: the register is held flat across
an idle interval and the gap’s energy is spread over the seconds that remain.
Where a gap has no charging time at all and the register moved anyway, the
line is drawn across the whole gap and the contradiction is left standing for
the builder to report — it is now a real one.
§Quarter hours and daylight saving
A quarter hour here is an instant plus fifteen minutes of real time. Every civil UTC offset in the world is a whole number of quarter hours, so a UTC quarter-hour boundary is a local quarter-hour boundary everywhere — and the 92- and 100-slot days of a clock change are simply days with fewer or more instants in them. Nothing here counts to 96, and there is no DST branch.
Structs§
- Session
Split - A session, split across the quarter hours it touched.
- Slot
- One slice of a session, inside one quarter hour.
Enums§
- Provenance
- Where a number came from.
- Split
Error - Why a session could not be split.
Functions§
- into_
periods - Split a session’s meter series across quarter hours, cutting also at every
instant in
cutsand interpolating only across the time outsideidle. - into_
quarter_ hours - Split a session’s meter series across quarter hours.
Type Aliases§
- Idle
- An interval
[from, to)in which the session says no energy flowed.