Skip to content

Decode the COSEM clock without the process timezone - #1262

Merged
gskjold merged 1 commit into
mainfrom
fix/cosem-datetime-timezone
Sep 7, 2026
Merged

gskjold merged 1 commit into
mainfrom
fix/cosem-datetime-timezone

Conversation

@gskjold

@gskjold gskjold commented Sep 7, 2026

Copy link
Copy Markdown
Member

What

decodeCosemDateTime() converts the meter's broken-down clock with mktime(), which interprets the fields as local time per the process timezone. The meter's own UTC deviation is then added on top, so the zone offset is applied twice and the meter clock lands one whole UTC offset in the past.

v2.5.7 used makeTime() here (lib/AmsDecoder/src/Cosem.cpp) — pure calendar arithmetic, timezone-free. The regression arrived with c9fd6ce, when the decoder was made Arduino-free: makeTime() is an Arduino Time-library function and mktime() is the natural, but timezone-aware, substitute.

How it was found

A v2.5.7 device and a current build were run side by side for ~16 hours against the same HAN port (same Aidon meter, both Europe/Oslo, both publishing to the same broker). Every other value matched — instantaneous power, all three phases, the hourly accumulated registers, peaks, monthmax, the whole price curve — but <topic>/meter/clock was exactly 7200 s behind at sixteen consecutive hourly frames:

hourly frame v2.5.7 (correct) this build delta
13:00 CEST 1788692400 1788685200 −7200
14:00 CEST 1788696000 1788688800 −7200
… … … −7200
04:00 CEST 1788746400 1788739200 −7200

The error equals the zone's current UTC offset, so −2h in CEST and −1h in CET — it changes at the DST boundary. meter/dlms/timestamp is unaffected; it takes a different path.

The fix

Convert with days_from_civil — integer arithmetic only, identical on device and native. No new include: Cosem.cpp deliberately depends on nothing but Cosem.h/byteorder.h/time.h, so pulling in the Timezone header just to reach makeTime() again was not an option.

Validated against timegm() over all 47847 dates from 1970-01-01 to 2100-12-31, including every leap-year edge — no mismatch. Behaviour under TZ=UTC is unchanged, so the golden master does not move.

Why no existing test caught this

test_kamstrup_timezone does assert a meter timestamp, but it cannot reach the bug:

  1. The Kamstrup branch of IEC6205675::adjustForKnownIssues discards decodeCosemDateTime()'s result and recomputes the epoch itself with makeTime() (IEC6205675.cpp:1428), so the mktime() line never runs.
  2. That test also builds its expectation with makeTime() (utc_epoch()), leaving both sides of the assertion timezone-free.

So no test asserted a clock on a path that keeps the mktime()-derived value — which is every non-Kamstrup meter: Aidon, and the generic tz->toUTC() fallback. Note the native makeTime() stub (test/stubs/Timezone.h:20) is implemented with timegm(), which is why simply running the suite under a non-UTC TZ does not expose it either.

test_meter_clock_is_timezone_independent decodes two Aidon hourly fixtures under Europe/Oslo, Pacific/Auckland, America/Denver and Asia/Kathmandu and asserts each matches the UTC decode. Asserting the invariant rather than a hand-derived epoch keeps it honest regardless of which zones the CI host has.

Verification

  • pio test -e native — 18/18 pass (17 before + the new one)
  • New test fails on the old implementation (Expected 1765724400 Was 1765720800, −3600 for these December frames) and passes on the new one
  • Full suite also passes under TZ=Europe/Oslo
  • pio run -e esp32s2 builds clean
  • test_unencrypted_golden unchanged — no fixture decodes differently

Scope

Fix only. Three other things surfaced during the comparison and are deliberately not here: the services 60 s republish is intentional (explicit heartbeat guard), the broker holding no retained messages is broker-side and affects both devices equally, and a ~1 NOK step in the persisted day/month cost accumulators across a reboot was not reproducible from passive observation — every live cost increment matched to the cent, and the standing offsets cleared the moment their accumulator reset at midnight.

🤖 Generated with Claude Code

decodeCosemDateTime() converted the broken-down meter clock with mktime(),
which interprets the fields as LOCAL time according to the process timezone.
The meter's own UTC deviation is then added on top, so the zone offset is
applied twice and the meter clock lands one whole UTC offset in the past.

v2.5.7 used makeTime() here (lib/AmsDecoder/src/Cosem.cpp), which is pure
calendar arithmetic and timezone-free. The regression came in with c9fd6ce,
when the decoder was made Arduino-free: makeTime() is an Arduino Time-library
function, and mktime() is the natural — but timezone-aware — substitute.

Observed on a live Aidon meter (Europe/Oslo) running side by side with a
v2.5.7 device against the same HAN port: mqtt <topic>/meter/clock was exactly
7200 s behind the v2.5.7 reading at sixteen consecutive hourly frames, e.g.
at 14:00 CEST v2.5.7 published 1788696000 (correct) and this build published
1788688800. The error equals the zone's current UTC offset, so -2h in CEST
and -1h in CET. meter/dlms/timestamp is unaffected — it takes another path.

Convert with days_from_civil instead: integer arithmetic only, identical on
device and native, no new include (Cosem.cpp deliberately depends on nothing
but Cosem.h/byteorder.h/time.h, so pulling in the Timezone header to reach
makeTime() again was not an option). Verified against timegm() over all 47847
dates from 1970-01-01 to 2100-12-31, including every leap-year edge: no
mismatch. Behaviour under TZ=UTC is unchanged, so the golden master does not
move.

Why no existing test caught it: test_kamstrup_timezone asserts a meter
timestamp, but the Kamstrup branch of IEC6205675::adjustForKnownIssues
discards decodeCosemDateTime()'s result and recomputes the epoch with
makeTime(), so it never reaches the mktime() line — and it builds its own
expectation with makeTime() too, leaving both sides timezone-free. No test
asserted a clock on a path that keeps the mktime()-derived value, which is
every non-Kamstrup meter: Aidon, and the generic tz->toUTC() fallback.

test_meter_clock_is_timezone_independent decodes two Aidon hourly fixtures
under Europe/Oslo, Pacific/Auckland, America/Denver and Asia/Kathmandu and
asserts each matches the UTC decode. It fails on the old implementation
(-3600 for these December frames) and passes on the new one. Asserting the
invariant rather than a hand-derived epoch keeps it honest regardless of
which zones the CI host has.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

🔧 PR Build Artifacts

Version: ec8b31e

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

@gskjold
gskjold merged commit 0389407 into main Sep 7, 2026
8 checks passed
@gskjold
gskjold deleted the fix/cosem-datetime-timezone branch September 7, 2026 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant