Repository navigation
Decode the COSEM clock without the process timezone - #1262
Merged
Merged
Conversation
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]>
🔧 PR Build ArtifactsVersion: All environments built successfully. Download the zip files:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
decodeCosemDateTime()converts the meter's broken-down clock withmktime(), which interprets the fields as local time per the process timezone. The meter's own UTCdeviationis 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.7usedmakeTime()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 andmktime()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/clockwas exactly 7200 s behind at sixteen consecutive hourly frames: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/timestampis 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.cppdeliberately depends on nothing butCosem.h/byteorder.h/time.h, so pulling in the Timezone header just to reachmakeTime()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 underTZ=UTCis unchanged, so the golden master does not move.Why no existing test caught this
test_kamstrup_timezonedoes assert a meter timestamp, but it cannot reach the bug:IEC6205675::adjustForKnownIssuesdiscardsdecodeCosemDateTime()'s result and recomputes the epoch itself withmakeTime()(IEC6205675.cpp:1428), so themktime()line never runs.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 generictz->toUTC()fallback. Note the nativemakeTime()stub (test/stubs/Timezone.h:20) is implemented withtimegm(), which is why simply running the suite under a non-UTCTZdoes not expose it either.test_meter_clock_is_timezone_independentdecodes two Aidon hourly fixtures underEurope/Oslo,Pacific/Auckland,America/DenverandAsia/Kathmanduand 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)Expected 1765724400 Was 1765720800, −3600 for these December frames) and passes on the new oneTZ=Europe/Oslopio run -e esp32s2builds cleantest_unencrypted_goldenunchanged — no fixture decodes differentlyScope
Fix only. Three other things surfaced during the comparison and are deliberately not here: the
services60 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