Modern Apps git

← Modern-Apps.git

clock-wear: mirror the fall-back alarm fix into the wear protocol copy

Commit
1f35afb2f28a50a76c040d43cd2e670f13cc9899
Author
Vayun Mathur <[email protected]>
When
2026-12-01 15:07 UTC (4 days ago)
Parents
f4d6b9f
Mirroring f4d6b9fc5 into the deliberately identical wear
copy, which still resolved only the earlier-offset instant:
an alarm set on the watch during the second fall-back hour
would fire a day/week late. Both pairs are now identical
modulo package and header phrasing (214/143 lines), the
wear ratchet no longer flags the pair, and the regression
test came across verbatim. No other drift existed.
Verified: :clock:wear:testDebugUnitTest 7/7 protocol tests
and lint BUILD SUCCESSFUL.

Diff

clock-wear/src/main/java/com/vayunmathur/clock/wear/domain/WearClockProtocol.kt
diff --git a/clock-wear/src/main/java/com/vayunmathur/clock/wear/domain/WearClockProtocol.kt b/clock-wear/src/main/java/com/vayunmathur/clock/wear/domain/WearClockProtocol.ktindex 215ac3b105..405e75d23f 100644--- a/clock-wear/src/main/java/com/vayunmathur/clock/wear/domain/WearClockProtocol.kt+++ b/clock-wear/src/main/java/com/vayunmathur/clock/wear/domain/WearClockProtocol.kt@@ -3,11 +3,13 @@ package com.vayunmathur.clock.wear.domain import kotlinx.datetime.DateTimeUnit import kotlinx.datetime.LocalDateTime import kotlinx.datetime.TimeZone+import kotlinx.datetime.offsetAt import kotlinx.datetime.plus import kotlinx.datetime.toInstant import kotlinx.datetime.toLocalDateTime import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json+import kotlin.time.Duration.Companion.days import kotlin.time.Instant  /**@@ -157,6 +159,11 @@ fun buildWearClockSnapshot(  * [timeZone]. Calendar-based day stepping, matching the phone scheduler:  * adding a fixed 24h would land an hour off across DST transitions.  * An alarm whose time is exactly now fires tomorrow / on its next day.+ *+ * On a fall-back overlap day the wall time occurs twice; when "now" is+ * already inside the second pass (the earlier-offset instant is past but+ * the later-offset one is still ahead) the alarm fires at that second+ * occurrence today, matching the phone scheduler.  */ fun nextWearAlarmTriggerMillis(     hour: Int,@@ -167,24 +174,41 @@ fun nextWearAlarmTriggerMillis( ): Long {     val now = Instant.fromEpochMilliseconds(nowMillis)     val today = now.toLocalDateTime(timeZone).date-    var candidate = LocalDateTime(today, kotlinx.datetime.LocalTime(hour, minute))-    if (candidate.toInstant(timeZone) <= now) {-        candidate = LocalDateTime(-            candidate.date.plus(1, DateTimeUnit.DAY), candidate.time,-        )-    }-    if (daysMask == 0) return candidate.toInstant(timeZone).toEpochMilliseconds()-    repeat(7) {-        val bit = when (candidate.dayOfWeek) {-            kotlinx.datetime.DayOfWeek.SUNDAY -> 0-            else -> candidate.dayOfWeek.ordinal + 1 // Mon ordinal 0 -> bit 1+    val time = kotlinx.datetime.LocalTime(hour, minute)+    // Scan today through a week out (8 dates: a weekday-only alarm whose+    // time already passed today legitimately fires 7 days from today).+    for (dayOffset in 0..7) {+        val candidate = LocalDateTime(today.plus(dayOffset, DateTimeUnit.DAY), time)+        if (daysMask != 0) {+            val bit = when (candidate.dayOfWeek) {+                kotlinx.datetime.DayOfWeek.SUNDAY -> 0+                else -> candidate.dayOfWeek.ordinal + 1 // Mon ordinal 0 -> bit 1+            }+            if (daysMask and (1 shl bit) == 0) continue         }-        if (daysMask and (1 shl bit) != 0) {-            return candidate.toInstant(timeZone).toEpochMilliseconds()+        for (occurrence in candidate.wearOccurrences(timeZone)) {+            if (occurrence > now) return occurrence.toEpochMilliseconds()         }-        candidate = LocalDateTime(-            candidate.date.plus(1, DateTimeUnit.DAY), candidate.time,-        )     }-    return candidate.toInstant(timeZone).toEpochMilliseconds()+    return LocalDateTime(today.plus(8, DateTimeUnit.DAY), time)+        .toInstant(timeZone).toEpochMilliseconds()+}++/**+ * The instant(s) this wall time occurs on its date in [timeZone]: one,+ * except on a fall-back overlap day where it occurs twice — the+ * earlier-offset instant plus the same wall time under the later offset.+ * The local-time round-trip rejects gap resolutions and offset changes+ * that belong to the next day rather than to a second pass today.+ */+private fun LocalDateTime.wearOccurrences(timeZone: TimeZone): List<Instant> {+    val first = toInstant(timeZone)+    val laterOffset = timeZone.offsetAt(first.plus(1.days))+    if (laterOffset == timeZone.offsetAt(first)) return listOf(first)+    val second = toInstant(laterOffset)+    return if (second > first && second.toLocalDateTime(timeZone) == this) {+        listOf(first, second)+    } else {+        listOf(first)+    } }
clock-wear/src/test/java/com/vayunmathur/clock/wear/domain/WearClockProtocolTest.kt
diff --git a/clock-wear/src/test/java/com/vayunmathur/clock/wear/domain/WearClockProtocolTest.kt b/clock-wear/src/test/java/com/vayunmathur/clock/wear/domain/WearClockProtocolTest.ktindex 7421d507fb..d216eb25b1 100644--- a/clock-wear/src/test/java/com/vayunmathur/clock/wear/domain/WearClockProtocolTest.kt+++ b/clock-wear/src/test/java/com/vayunmathur/clock/wear/domain/WearClockProtocolTest.kt@@ -109,6 +109,18 @@ class WearClockProtocolTest {         )     } +    @Test+    fun nextTriggerDuringSecondFallBackOccurrenceFiresToday() {+        // 2026-11-01 fall-back in New York: at 06:15Z the wall clock reads+        // 01:15 EST (second pass through 01:00-02:00), so a 01:30 alarm+        // fires at 06:30Z today, not tomorrow (one-shot) or next Sunday.+        val newYork = TimeZone.of("America/New_York")+        val now = kotlin.time.Instant.parse("2026-11-01T06:15:00Z").toEpochMilliseconds()+        val expected = kotlin.time.Instant.parse("2026-11-01T06:30:00Z").toEpochMilliseconds()+        assertEquals(expected, nextWearAlarmTriggerMillis(1, 30, 0, now, newYork))+        assertEquals(expected, nextWearAlarmTriggerMillis(1, 30, 1 shl 0, now, newYork))+    }+     @Test     fun nextTriggerRecurringUsesSundayBitZero() {         val mondayAfter = utcMillis(LocalDateTime(2026, 1, 5, 8, 0))