Modern Apps git

← Modern-Apps.git

weather: map opens on the selected hour, not the UTC date start

Commit
9dd3fa5d323c9bd9ec28d68a59145aabb1cfa608
Author
Vayun Mathur <[email protected]>
When
2026-11-30 19:05 UTC (5 days ago)
Parents
e7e023e
The metric graph sheet handed the map a naive location-local ISO
string (e.g. 2026-07-15T14:00 wall-clock in Denver), while the map's
valid_times are UTC strings. indexForIso compared raw text prefixes,
so every non-UTC location fell back to the first entry sharing the
UTC date — 00:00Z — and the map opened on the wrong hour (20 hours
early in the Denver example; Tokyo local midnight landed on the
wrong UTC day entirely).

The open-map action, the one place that knows the forecast's
utcOffsetSeconds, now converts the selection with localIsoToUtcIso
before routing, and indexForIso compares parsed instants (exact,
then nearest step) ahead of the legacy prefix fallbacks, so
equivalent UTC shapes also match. New regression test covers the
Denver 14:00 -> 20:00Z conversion, Tokyo midnight crossing to the
previous UTC day, nearest-step and malformed input. R2 pack decoding,
grid sampling, unit derivations, staleness and place search were
audited and cleared. Verified: :weather:testDevUnitTest 233 tests,
0 failures; lint errors are pre-existing RouteScreenshotCoverage
findings on untouched Route.kt.

Diff

weather/AGENTS.md
diff --git a/weather/AGENTS.md b/weather/AGENTS.mdindex aa85f36070..2aa3706ea5 100644--- a/weather/AGENTS.md+++ b/weather/AGENTS.md@@ -22,6 +22,7 @@ _Install: ./install weather (dev by default)._ - Unit system follows the device locale (`platform/SystemUnits`) — format in `domain/`, never in `ui/` - Map shading is sampled on device from the R2 run tiles in `network/WxStatic`; the Rust `.om` bridge (`domain/map/OmTilesNative`) is not on the read path - Widgets must render from cache alone — never network on the widget path+- Map `valid_times` are UTC; the graph sheet's selected hour is a naive location-local ISO string and is converted with the forecast's `utcOffsetSeconds` (`localIsoToUtcIso`) before seeding the map's time index  ## Key files ### Root — entry + navigation
weather/src/main/java/com/vayunmathur/weather/domain/DateTimeFormat.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/domain/DateTimeFormat.kt b/weather/src/main/java/com/vayunmathur/weather/domain/DateTimeFormat.ktindex 2d9720ba31..c0c7b045fc 100644--- a/weather/src/main/java/com/vayunmathur/weather/domain/DateTimeFormat.kt+++ b/weather/src/main/java/com/vayunmathur/weather/domain/DateTimeFormat.kt@@ -116,11 +116,30 @@ fun formatInstantInZone(utcIso: String, zone: TimeZone, use24Hour: Boolean): Str     return "$hour · $weekday" } -/** Parse a UTC ISO string with a trailing `Z`, an explicit offset, or none. */-private fun parseUtcIsoToEpochSec(iso: String): Long? =+/** Parse a UTC ISO string with a trailing `Z`, an explicit offset, or none (naive = UTC). */+fun parseUtcIsoToEpochSec(iso: String): Long? =     runCatching { Instant.parse(iso).epochSeconds }.getOrNull()         ?: runCatching { LocalDateTime.parse(iso.removeSuffix("Z")).toInstant(UtcOffset.ZERO).epochSeconds }.getOrNull() +/** Format [epochSec] as a UTC ISO string (`2026-07-15T20:00Z`), the shape of the map's `valid_times`. */+fun formatUtcIso(epochSec: Long): String {+    val ldt = Instant.fromEpochSeconds(epochSec).toLocalDateTime(TimeZone.UTC)+    val d = ldt.date+    val t = ldt.time+    return String.format(+        java.util.Locale.US, "%04d-%02d-%02dT%02d:%02dZ",+        d.year, d.monthNumber, d.dayOfMonth, t.hour, t.minute,+    )+}++/**+ * Convert a naive location-local ISO string (e.g. `2026-07-15T14:00`, as in+ * `hourly.time`) to the UTC ISO shape the map's `valid_times` use, via the+ * forecast's [utcOffsetSec]. Null when [iso] does not parse.+ */+fun localIsoToUtcIso(iso: String, utcOffsetSec: Int): String? =+    parseLocalIsoToEpochSec(iso, utcOffsetSec)?.let { formatUtcIso(it) }+ /**  * The bundle mapper emits local-time ISO strings with no offset (e.g.  * `2024-05-30T05:42`). Combine with `utc_offset_seconds` from the response to
weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt b/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.ktindex da7d64b4f8..9c949d3be4 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt@@ -31,6 +31,7 @@ import com.vayunmathur.weather.domain.WeatherMetric import com.vayunmathur.weather.domain.formatHourAxisLabel import com.vayunmathur.weather.domain.metricSeries import com.vayunmathur.weather.domain.metricValueFormatter+import com.vayunmathur.weather.domain.localIsoToUtcIso import com.vayunmathur.weather.domain.parseLocalIsoToEpochSec import com.vayunmathur.weather.domain.resolveConditions import com.vayunmathur.weather.platform.WeatherActions@@ -260,11 +261,16 @@ internal fun ForecastColumn(                 valueLabel = metricValueFormatter(gm, units.temperature, units.wind, units.pressure),                 timeLabel = { epoch -> formatHourAxisLabel(epoch, units.use24Hour, forecast.utcOffsetSeconds) },                 onOpenMap = {-                    val iso = when (val s = selected) {+                    // Selections are naive location-local ISO strings, but the+                    // map's valid_times are UTC: convert with the forecast's+                    // offset so the map opens on the selected hour, not the+                    // same wall-clock string read as UTC.+                    val localIso = when (val s = selected) {                         is SelectedDateOrTime.Time -> s.isoTime                         is SelectedDateOrTime.Day -> "${s.isoDate}T00:00"                         null -> null                     }+                    val iso = localIso?.let { localIsoToUtcIso(it, forecast.utcOffsetSeconds) ?: it }                     onOpenMap(gm, iso)                     graphMetric = null                 },
weather/src/main/java/com/vayunmathur/weather/ui/WeatherMapState.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/WeatherMapState.kt b/weather/src/main/java/com/vayunmathur/weather/ui/WeatherMapState.ktindex e9cb7ff556..63d6d41618 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/WeatherMapState.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/WeatherMapState.kt@@ -3,6 +3,7 @@ package com.vayunmathur.weather.ui import com.vayunmathur.library.log.Log import com.vayunmathur.library.map.GeoBounds import com.vayunmathur.weather.domain.map.OmMapMetadata+import com.vayunmathur.weather.domain.parseUtcIsoToEpochSec import com.vayunmathur.weather.domain.map.colorizeToBitmap import com.vayunmathur.weather.domain.map.omVariable import com.vayunmathur.weather.network.WxStatic@@ -232,14 +233,33 @@ internal fun cacheKey(bbox: GeoBounds, variable: String, validTime: String): Str }  /**- * Index of the timestamp matching [isoTime] (by exact string, then by date+hour- * prefix, then by date), or 0. Handles the trailing `Z` on `valid_times`.+ * Index of the timestamp matching [isoTime] in [times] (UTC `valid_times`),+ * or 0. Comparison is by parsed instant first (exact, then nearest step) so+ * equivalent strings in different shapes (`…T18:00Z` vs `…T18:30:00Z`) still+ * land on the right hour; the string-prefix fallbacks keep malformed inputs+ * (and persisted routes) behaving as before. Callers must pass a UTC string:+ * naive location-local selections are converted by `localIsoToUtcIso` at the+ * call site that knows the forecast offset.  */ internal fun indexForIso(times: List<String>, isoTime: String?): Int {     if (times.isEmpty()) return 0     if (isoTime != null) {         val exact = times.indexOf(isoTime)         if (exact >= 0) return exact+        val target = parseUtcIsoToEpochSec(isoTime)+        if (target != null) {+            var best = -1+            var bestDiff = Long.MAX_VALUE+            for (i in times.indices) {+                val t = parseUtcIsoToEpochSec(times[i]) ?: continue+                val diff = kotlin.math.abs(t - target)+                if (diff < bestDiff) {+                    bestDiff = diff+                    best = i+                }+            }+            if (best >= 0) return best+        }         val hourPrefix = isoTime.take(13) // yyyy-MM-ddTHH         val byHour = times.indexOfFirst { it.take(13) == hourPrefix }         if (byHour >= 0) return byHour
weather/src/test/java/com/vayunmathur/weather/service/WeatherMiscTest.kt
diff --git a/weather/src/test/java/com/vayunmathur/weather/service/WeatherMiscTest.kt b/weather/src/test/java/com/vayunmathur/weather/service/WeatherMiscTest.ktindex 40d0a00440..160e2c2eab 100644--- a/weather/src/test/java/com/vayunmathur/weather/service/WeatherMiscTest.kt+++ b/weather/src/test/java/com/vayunmathur/weather/service/WeatherMiscTest.kt@@ -18,6 +18,7 @@ import com.vayunmathur.weather.service.car.WeatherCarAppService import com.vayunmathur.weather.service.car.WeatherCarState import com.vayunmathur.weather.service.car.cacheKeyFor import com.vayunmathur.weather.service.car.decodeCarForecast+import com.vayunmathur.weather.domain.localIsoToUtcIso import com.vayunmathur.weather.ui.cacheKey import com.vayunmathur.weather.ui.indexForIso import com.vayunmathur.weather.ui.rasterSize@@ -120,6 +121,28 @@ class WeatherMiscTest {         assertEquals(0, indexForIso(listOf("2026-07-01T00:00Z"), null))     } +    // Regression: the graph sheet's selection is a naive location-local ISO+    // string, while the map's valid_times are UTC. The producer converts with+    // the forecast offset (localIsoToUtcIso); the map must then open on that+    // hour — the old string-prefix match read the local wall clock as UTC and+    // landed on 00:00Z of the same UTC date (or index 0).+    @Test fun mapTimeSeedConvertsLocalSelectionToUtc() {+        val times = (0..23).map { h -> "2026-07-15T${h.toString().padStart(2, '0')}:00Z" }+        // Denver (UTC-6): 14:00 local = 20:00Z.+        val denver = localIsoToUtcIso("2026-07-15T14:00", -6 * 3600)+        assertEquals("2026-07-15T20:00Z", denver)+        assertEquals(20, indexForIso(times, denver))+        // Tokyo (UTC+9): local midnight is the previous UTC day.+        val tokyo = localIsoToUtcIso("2026-07-15T00:00", 9 * 3600)+        assertEquals("2026-07-14T15:00Z", tokyo)+        assertEquals(15, indexForIso((0..23).map { h -> "2026-07-14T${h.toString().padStart(2, '0')}:00Z" }, tokyo))+        // Nearest-step: a half-hour UTC string lands on the closest hour.+        assertEquals(18, indexForIso(times, "2026-07-15T18:30:00Z"))+        assertEquals(18, indexForIso(times, "2026-07-15T18:00:00Z"))+        // Unparseable input keeps the old prefix fallback (malformed hour, same day).+        assertEquals(2, indexForIso(listOf("2026-07-01T00:00Z", "2026-07-02T00:00Z", "2026-07-03T00:00Z"), "2026-07-03T99:99"))+    }+     @Test fun mapStateDefaultsAndFallback() {         val s = WeatherMapState("Temperature")         assertEquals(WeatherMetric.Temperature, s.selectedMetric)