Modern Apps git

← Modern-Apps.git

weather: keep the repeated fall-back hour on its true epoch

Commit
f87071caa0708236a0e4d3f4116741918ca88be6
Author
Vayun Mathur <[email protected]>
When
2026-12-01 19:31 UTC (4 days ago)
Parents
e2aac3c
On a fall-back day (2026-11-01 in America/New_York) the bundle
mapper emits the naive local hour 01:00 twice, for 05:00Z (EDT)
and 06:00Z (EST). MetricSeries.collectDayPoints resolved each
string independently through the zone, and kotlinx-datetime maps
the ambiguous wall time to the earlier instant for both rows.
The second 01:00 value stacked on the first row's epoch, 06:00Z
received no point, and every metric graph for that day showed
one hour misplaced with a two-hour gap after it.

collectDayPoints now keeps the rows chronological: it tracks
the previous row's epoch (including null-valued rows, so a
missing first value cannot re-anchor the repeat) and pushes a
repeated instant forward an hour at a time until it follows its
predecessor. The second 01:00 lands on 06:00Z; all other days
are unchanged because their resolved epochs already increase.

Regression test MetricSeriesDstTest failed pre-fix on the epoch
list (duplicate 1793509200) and passes post-fix. Verified with
:weather:testDevUnitTest (249 tests, 0 failures; the cargo
build tasks were excluded because the base tree fails them
with a --locked Cargo.lock update error, pre-existing and
unrelated).

Diff

weather/src/main/java/com/vayunmathur/weather/domain/MetricSeries.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/domain/MetricSeries.kt b/weather/src/main/java/com/vayunmathur/weather/domain/MetricSeries.ktindex dc11012900..bd84574df1 100644--- a/weather/src/main/java/com/vayunmathur/weather/domain/MetricSeries.kt+++ b/weather/src/main/java/com/vayunmathur/weather/domain/MetricSeries.kt@@ -153,6 +153,9 @@ private fun formatVisibility(v: Double, windUnit: WindUnit): String = /** Meters in a kilometre. */ private const val METERS_PER_KM = 1000.0 +/** Seconds in an hour (the hourly series step). */+private const val SECONDS_PER_HOUR = 3600L+ private fun formatPrecipitation(v: Double, windUnit: WindUnit): String =     if (windUnit == WindUnit.Mph) {         String.format(java.util.Locale.US, "%.2f in", mmToInches(v))@@ -218,14 +221,29 @@ private fun collectDayPoints( ): List<MetricPoint> {     val zone = forecast.locationZone()     val out = ArrayList<MetricPoint>()+    var prevEpoch: Long? = null     for (i in times.indices) {-        val value = raw.getOrNull(i) ?: continue         val iso = times[i]         if (iso.substringBefore('T') != targetDate) continue         // Zone-aware when the IANA zone resolves (the offset in force at         // that local time); the fetch-time offset is the fallback.-        val epoch = (zone?.let { parseLocalIsoToEpochSec(iso, it) }+        var epoch = (zone?.let { parseLocalIsoToEpochSec(iso, it) }             ?: parseLocalIsoToEpochSec(iso, forecast.utcOffsetSeconds)) ?: continue+        // On a fall-back day the repeated wall hour (e.g. 01:00 in+        // America/New_York) appears twice in the naive-local list, and+        // the zone resolves both spellings to the earlier instant. The+        // rows are chronological, so push a repeated instant forward+        // an hour at a time until it is after its predecessor — the+        // second 01:00 lands on its true (post-transition) epoch+        // instead of stacking on the first, leaving no gap at 06:00Z.+        // Track the epoch even for null-valued rows so a missing value+        // in the first occurrence cannot re-anchor the second.+        val prev = prevEpoch+        if (prev != null) {+            while (epoch <= prev) epoch += SECONDS_PER_HOUR+        }+        prevEpoch = epoch+        val value = raw.getOrNull(i) ?: continue         out.add(MetricPoint(epoch, value))     }     return out
weather/src/test/java/com/vayunmathur/weather/domain/MetricSeriesDstTest.kt
diff --git a/weather/src/test/java/com/vayunmathur/weather/domain/MetricSeriesDstTest.kt b/weather/src/test/java/com/vayunmathur/weather/domain/MetricSeriesDstTest.ktnew file mode 100644index 0000000000..4070543343--- /dev/null+++ b/weather/src/test/java/com/vayunmathur/weather/domain/MetricSeriesDstTest.kt@@ -0,0 +1,59 @@+package com.vayunmathur.weather.domain++import com.vayunmathur.weather.network.ForecastResponse+import com.vayunmathur.weather.network.Hourly+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Fall-back day alignment for [metricSeries].+ *+ * On 2026-11-01 in America/New_York the wall hour 01:00 occurs twice+ * (01:00 EDT = 05:00Z, then 01:00 EST = 06:00Z), so the bundle mapper+ * emits the naive local string "2026-11-01T01:00" for two consecutive+ * hourly rows. Resolving each string independently through the zone+ * maps both rows to the earlier (EDT) instant: the second 01:00 row's+ * value lands on the first row's epoch, and 06:00Z gets no point at+ * all. The day's graph must keep the rows in chronological order, one+ * hour apart.+ */+class MetricSeriesDstTest {++    @Test+    fun fallBackDay_repeatedHourKeepsChronologicalEpochs() {+        val forecast = ForecastResponse(+            latitude = 40.71,+            longitude = -74.01,+            timezone = "America/New_York",+            utcOffsetSeconds = -4 * 3600,+            hourly = Hourly(+                time = listOf(+                    "2026-11-01T00:00",+                    "2026-11-01T01:00",+                    "2026-11-01T01:00",+                    "2026-11-01T02:00",+                    "2026-11-01T03:00",+                ),+                temperature = listOf(10.0, 11.0, 12.0, 13.0, 14.0),+            ),+        )++        val points = metricSeries(+            forecast,+            WeatherMetric.Temperature,+            SelectedDateOrTime.Day("2026-11-01"),+        )++        assertEquals(listOf(10.0, 11.0, 12.0, 13.0, 14.0), points.map { it.value })+        assertEquals(+            listOf(+                1793505600L, // 00:00 EDT = 04:00Z+                1793509200L, // 01:00 EDT = 05:00Z+                1793512800L, // 01:00 EST = 06:00Z (the repeated hour)+                1793516400L, // 02:00 EST = 07:00Z+                1793520000L, // 03:00 EST = 08:00Z+            ),+            points.map { it.epochSec },+        )+    }+}