Merge branch 'dev/its-time-tai' into 'main'

Count ITS time in TAI, with the leap seconds since 2004

See merge request urban-mobility-lab/microbu/microbuapp!2
This commit is contained in:
Niklas Dathe
2026-10-02 19:19:10 +02:00
10 changed files with 136 additions and 31 deletions
@@ -42,9 +42,6 @@ object DenmUperCodec {
private const val MESSAGE_ID_DENM = 1
private const val PROTOCOL_VERSION = 2
/** TimestampIts epoch: 2004-01-01T00:00:00Z in Unix epoch milliseconds. */
private const val TS_ITS_EPOCH_MS = 1_072_915_200_000L
/**
* Decodes a UPER DENM into a [DenmEvent].
*
@@ -147,7 +144,8 @@ object DenmUperCodec {
// on it; if any of it is ever needed, the unparsed optionals must be consumed in order
// first or every later read lands at the wrong bit offset.
val detectionTimeMs = detectionTimeIts + TS_ITS_EPOCH_MS
// TimestampIts is TAI-based: back to UTC through the one conversion the transmit side uses.
val detectionTimeMs = ItsTime.epochMs(detectionTimeIts)
return DenmEvent(
stationId = originatingStationId,
@@ -1,30 +1,65 @@
package com.hawhamburg.micr0bu.domain.asn1
/**
* ITS time, as used by every timestamp this app puts on the air.
* ITS time, as used by every timestamp this app puts on the air or reads from it.
*
* TimestampIts (ETSI TS 102 894-2) counts milliseconds from 2004-01-01T00:00:00Z. Two fields in a
* single transmitted frame come from it: the CAM's generationDeltaTime (modulo 65536) and the
* GeoNetworking Source Position Vector's TST (modulo 2^32). A receiver can compare the two, so
* they must follow one rule. Both go through here so they cannot drift apart.
* TimestampIts (ETSI TS 102 894-2 V2.5.1) is "the number of elapsed (TAI) milliseconds since the ITS
* Epoch", 2004-01-01T00:00:00.000 UTC, counted without interruption, so it is not altered by the leap
* seconds UTC inserts. It therefore runs ahead of UTC by the leap seconds inserted since 2004: 5 s since
* 2017-01-01 (the standard's own example: 2007-01-01T00:00:00.000 UTC is 94 694 401 000 ms, one leap
* second included). Three fields in a single transmitted frame come from it: the CAM's and VAM's
* generationDeltaTime (modulo 65536), the GeoNetworking Source Position Vector's TST (modulo 2^32) and,
* on the station-link path, the PoTi timestamp that sets the micrOBU's ITS clock, which the security
* entity stamps into every signature's generationTime and checks received messages against. A receiver
* compares them with its own TAI clock, so they all go through here and cannot drift apart.
*
* Until 2026-10 this was Unix time minus the epoch, without the leap seconds: the micrOBU's clock ran
* 5 s behind, so it rejected correctly timed VAMs from other stations as Invalid_Timestamp and restarted
* its stack whenever another source (the PC tools, which add the leap seconds) had set it correctly.
*
* **Which clock.** The input should be GNSS time, not the phone's wall clock. A phone with no SIM
* and no internet time has no automatic time source at all, and the bench phone was found 24
* minutes fast that way. `GnssTimeSource` moves a timestamp onto GNSS time, using [onGnssTime],
* before it gets here.
*
* **Open question: leap seconds.** This is Unix time minus the 2004 epoch, with no leap-second
* term. If TimestampIts is read as TAI-based, the correct value is currently 5 s higher, for the
* five leap seconds inserted since 2004. Whichever reading turns out right, it is changed here and
* nowhere else. Settling it needs a frame from a third-party stack with a trusted clock, such as
* the RSU's CAM compared against GNSS time, and no such traffic was on air when this was written.
* before it gets here. Android reports GNSS time as UTC (Unix epoch ms), so the leap seconds are
* added here, not there.
*/
object ItsTime {
/** 2004-01-01T00:00:00Z in Unix epoch milliseconds. */
const val EPOCH_MS = 1_072_915_200_000L
/** TimestampIts for wall-clock [epochMs], before any modulo is applied. */
fun timestampIts(epochMs: Long): Long = epochMs - EPOCH_MS
/**
* Unix epoch ms (UTC) from which each leap second inserted after the ITS epoch counts, from IERS via
* the IANA leap-seconds.list (TAI-UTC 32 s at the epoch, 37 s since 2017-01-01; the file of
* 2026-07 announces none before its expiry on 2027-06-28). A new leap second is added here when
* IERS Bulletin C announces one.
*/
private val LEAP_SECONDS_FROM_MS = longArrayOf(
1_136_073_600_000L, // 2006-01-01
1_230_768_000_000L, // 2009-01-01
1_341_100_800_000L, // 2012-07-01
1_435_708_800_000L, // 2015-07-01
1_483_228_800_000L, // 2017-01-01
)
/** Leap seconds inserted between the ITS epoch and the UTC instant [epochMs]. */
fun leapSecondsAt(epochMs: Long): Int = LEAP_SECONDS_FROM_MS.count { epochMs >= it }
/** TimestampIts for the UTC instant [epochMs] (Unix epoch ms), before any modulo is applied. */
fun timestampIts(epochMs: Long): Long = epochMs - EPOCH_MS + 1_000L * leapSecondsAt(epochMs)
/**
* The UTC instant (Unix epoch ms) of [timestampIts], the inverse of [timestampIts]. During an
* inserted leap second, which UTC cannot name, the first millisecond after it is returned.
*/
fun epochMs(timestampIts: Long): Long {
var leaps = 0
for ((index, from) in LEAP_SECONDS_FROM_MS.withIndex()) {
val itsAtFrom = timestampIts(from) // first TimestampIts after leap second index + 1
if (timestampIts >= itsAtFrom) leaps = index + 1
else if (timestampIts >= itsAtFrom - 1_000L) return from // inside the inserted second
}
return timestampIts + EPOCH_MS - 1_000L * leaps
}
/**
* Moves [systemMs], a reading of this phone's wall clock, onto GNSS time, using one pair of