weather: render clock and hour labels in the forecast location's time zone
- Commit
ea2089285a073d86c275cf047f3107a303d0cad4- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:43 UTC (28 days ago)
- Parents
05b6fca
Hourly-strip hours, the metric-graph hour axis, and sunrise/sunset and moonrise/moonset times were formatted with TimeZone.currentSystemDefault(), while the underlying instants come from parseLocalIsoToEpochSec, which combines the API's local-time ISO strings with the response's utc_offset_seconds into true UTC epochs. For any saved location outside the device's time zone every clock label on the home page was therefore off by the zone difference: a Tokyo hour strip viewed from Denver showed Denver hours, and sunrise/sunset could even land on the wrong day. The daily cards were already correct (day boundaries come from the API's local-date strings), which made the inconsistency visible on the same screen: the hourly strip's day labels could disagree with the daily row above it. Thread forecast.utcOffsetSeconds (nullable, default null preserves the device-zone behavior for previews and zone-less callers) through formatStripHour, formatHourAxisLabel, formatClockTime, HourlyCard's day-label helper, and SunBlock/MoonBlock via WeatherBlocks, with ForecastColumn passing the active forecast's offset at each call site. The offset is applied as shift-then-read-in-UTC because this tree's kotlinx-datetime version keeps Instant.toLocalDateTime(UtcOffset) internal. The fix is app-side (domain/ + ui/); shared library code is only read, not changed. Verified with :weather:testDevUnitTest (9 tests, 0 failures), including a new epochLabelsLocationOffset test that pins UTC+5:30 and UTC-5 rendering of the same instant and asserts explicit-offset rendering is independent of the device default time zone.
Diff
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 b259d32332..2d9720ba31 100644--- a/weather/src/main/java/com/vayunmathur/weather/domain/DateTimeFormat.kt+++ b/weather/src/main/java/com/vayunmathur/weather/domain/DateTimeFormat.kt@@ -60,20 +60,33 @@ private val DayMonthLabel: DateTimeFormat<LocalDate> get() = LocalDate.Format { monthName(MonthNames(localizedMonthNames(DateNameStyle.SHORT))) } -private fun localTimeAt(epochSec: Long): LocalTime =- Instant.fromEpochSeconds(epochSec).toLocalDateTime(TimeZone.currentSystemDefault()).time+/**+ * Wall-clock time for [epochSec] in the forecast location's zone. Pass the+ * response's `utc_offset_seconds`; null keeps the device time zone (previews+ * and callers with no location context).+ */+private fun localTimeAt(epochSec: Long, utcOffsetSeconds: Int? = null): LocalTime {+ // A location's wall clock is the UTC instant shifted by its offset, read+ // back as UTC fields. (Instant.toLocalDateTime(UtcOffset) is internal in+ // this kotlinx-datetime version, so shift-then-read-in-UTC instead.)+ return if (utcOffsetSeconds != null) {+ Instant.fromEpochSeconds(epochSec + utcOffsetSeconds).toLocalDateTime(TimeZone.UTC).time+ } else {+ Instant.fromEpochSeconds(epochSec).toLocalDateTime(TimeZone.currentSystemDefault()).time+ }+} -/** Hour-axis label "3 PM" / "09:00" for an epoch second in the system time zone. */-fun formatHourAxisLabel(epochSec: Long, use24Hour: Boolean): String =- DateString.hourLabel(localTimeAt(epochSec), use24Hour)+/** Hour-axis label "3 PM" / "09:00" for an epoch second in the location's zone. */+fun formatHourAxisLabel(epochSec: Long, use24Hour: Boolean, utcOffsetSeconds: Int? = null): String =+ DateString.hourLabel(localTimeAt(epochSec, utcOffsetSeconds), use24Hour) -/** Hourly-strip label "3 PM" / "9" for an epoch second in the system time zone. */-fun formatStripHour(epochSec: Long, use24Hour: Boolean): String =- localTimeAt(epochSec).format(if (use24Hour) HourOfDayBare else HourAmPm)+/** Hourly-strip label "3 PM" / "9" for an epoch second in the location's zone. */+fun formatStripHour(epochSec: Long, use24Hour: Boolean, utcOffsetSeconds: Int? = null): String =+ localTimeAt(epochSec, utcOffsetSeconds).format(if (use24Hour) HourOfDayBare else HourAmPm) -/** Clock time "3:05 PM" / "15:00" for an epoch second in the system time zone. */-fun formatClockTime(epochSec: Long, use24Hour: Boolean): String =- DateString.time(localTimeAt(epochSec), use24Hour)+/** Clock time "3:05 PM" / "15:00" for an epoch second in the location's zone. */+fun formatClockTime(epochSec: Long, use24Hour: Boolean, utcOffsetSeconds: Int? = null): String =+ DateString.time(localTimeAt(epochSec, utcOffsetSeconds), use24Hour) /** "Wed 25 Jun" for an ISO date like 2026-06-25; echoes the input on parse failure. */ fun formatDayMonthLabel(isoDate: String): String {
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 c055e8a342..161602fc99 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt@@ -177,6 +177,7 @@ internal fun ForecastColumn( pressureUnit = units.pressure, use24Hour = units.use24Hour, nowEpochSec = nowEpochSec,+ utcOffsetSeconds = forecast.utcOffsetSeconds, ) } }@@ -188,7 +189,7 @@ internal fun ForecastColumn( title = stringResource(gm.title), points = metricSeries(forecast, gm, selected), valueLabel = metricValueFormatter(gm, units.temperature, units.wind, units.pressure),- timeLabel = { epoch -> formatHourAxisLabel(epoch, units.use24Hour) },+ timeLabel = { epoch -> formatHourAxisLabel(epoch, units.use24Hour, forecast.utcOffsetSeconds) }, onOpenMap = { val iso = when (val s = selected) { is SelectedDateOrTime.Time -> s.isoTime
weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.ktindex 02908a5cf7..870f5a5972 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt@@ -114,8 +114,8 @@ fun HourlyCard( val cell = cells[index] val isNow = cell.iso == nowIsoHour HourlyItem(- time = if (isNow) stringResource(R.string.now) else formatStripHour(cell.epochSec, use24Hour),- dayLabel = formatDayLabel(cell.epochSec, nowEpochSec),+ time = if (isNow) stringResource(R.string.now) else formatStripHour(cell.epochSec, use24Hour, utcOffsetSeconds),+ dayLabel = formatDayLabel(cell.epochSec, nowEpochSec, utcOffsetSeconds), precipitationProbability = cell.precip, temperature = cell.temperature, isNow = isNow,@@ -226,10 +226,11 @@ private data class HourCell( ) @Composable-private fun formatDayLabel(epochSec: Long, nowEpochSec: Long): String {- val tz = TimeZone.currentSystemDefault()- val date = Instant.fromEpochSeconds(epochSec).toLocalDateTime(tz).date- val today = Instant.fromEpochSeconds(nowEpochSec).toLocalDateTime(tz).date+private fun formatDayLabel(epochSec: Long, nowEpochSec: Long, utcOffsetSeconds: Int): String {+ // Same shift-then-read-in-UTC as domain localTimeAt: Instant.toLocalDateTime(UtcOffset)+ // is internal in this kotlinx-datetime version.+ val date = Instant.fromEpochSeconds(epochSec + utcOffsetSeconds).toLocalDateTime(TimeZone.UTC).date+ val today = Instant.fromEpochSeconds(nowEpochSec + utcOffsetSeconds).toLocalDateTime(TimeZone.UTC).date val tomorrow = today.plus(1, DateTimeUnit.DAY) return when (date) { today -> stringResource(R.string.today_short)
weather/src/main/java/com/vayunmathur/weather/ui/components/WeatherBlocks.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/WeatherBlocks.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/WeatherBlocks.ktindex 191ee06382..3cdec3f1f6 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/WeatherBlocks.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/components/WeatherBlocks.kt@@ -63,6 +63,8 @@ fun WeatherBlocks( * preview can pin it — otherwise the marker lands somewhere different every render. */ nowEpochSec: Long = System.currentTimeMillis() / 1000,+ /** `utc_offset_seconds` for the forecast location; null renders times in the device zone. */+ utcOffsetSeconds: Int? = null, ) { LazyVerticalGrid( columns = GridCells.Adaptive(minSize = 140.dp),@@ -87,8 +89,8 @@ fun WeatherBlocks( item { Graphable({ onMetricSelected(WeatherMetric.CloudCover) }) { CloudCoverBlock(current = current) } } item { Graphable({ onMetricSelected(WeatherMetric.Pressure) }) { PressureBlock(current = current, pressureUnit = pressureUnit) } } item { Graphable({ onMetricSelected(WeatherMetric.Visibility) }) { VisibilityBlock(current = current, useMiles = windUnit == WindUnit.Mph) } }- item { SunBlock(sunriseEpochSec = sunriseEpochSec, sunsetEpochSec = sunsetEpochSec, use24Hour = use24Hour, daylightDurationSec = daylightDurationSec, nowEpochSec = nowEpochSec) }- item { MoonBlock(moonPhase = moonPhase, moonriseEpochSec = moonriseEpochSec, moonsetEpochSec = moonsetEpochSec, use24Hour = use24Hour, nowEpochSec = nowEpochSec) }+ item { SunBlock(sunriseEpochSec = sunriseEpochSec, sunsetEpochSec = sunsetEpochSec, use24Hour = use24Hour, daylightDurationSec = daylightDurationSec, nowEpochSec = nowEpochSec, utcOffsetSeconds = utcOffsetSeconds) }+ item { MoonBlock(moonPhase = moonPhase, moonriseEpochSec = moonriseEpochSec, moonsetEpochSec = moonsetEpochSec, use24Hour = use24Hour, nowEpochSec = nowEpochSec, utcOffsetSeconds = utcOffsetSeconds) } item { AirQualityBlock(air = air) } item { PollenBlock(air = air) } }
weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/MoonBlock.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/MoonBlock.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/MoonBlock.ktindex 9b39965371..ea51ec0497 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/MoonBlock.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/MoonBlock.kt@@ -52,6 +52,8 @@ fun MoonBlock( use24Hour: Boolean, /** Where to put the moon marker. Pinned by previews so the arc renders identically. */ nowEpochSec: Long = System.currentTimeMillis() / 1000,+ /** `utc_offset_seconds` for the forecast location; null renders in the device zone. */+ utcOffsetSeconds: Int? = null, ) { SquareBlock { Box(Modifier.align(Alignment.TopStart)) {@@ -107,12 +109,12 @@ fun MoonBlock( verticalArrangement = Arrangement.spacedBy(3.dp), ) { RiseSetTimeRow(- text = moonriseEpochSec?.let { formatClockTime(it, use24Hour) }+ text = moonriseEpochSec?.let { formatClockTime(it, use24Hour, utcOffsetSeconds) } ?: stringResource(R.string.weather_no_data), icon = { m, c -> IconKeyboardArrowUp(m, c) }, ) RiseSetTimeRow(- text = moonsetEpochSec?.let { formatClockTime(it, use24Hour) }+ text = moonsetEpochSec?.let { formatClockTime(it, use24Hour, utcOffsetSeconds) } ?: stringResource(R.string.weather_no_data), icon = { m, c -> IconKeyboardArrowDown(m, c) }, )
weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/SunBlock.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/SunBlock.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/SunBlock.ktindex ce524939c6..85ee0e97b4 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/SunBlock.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/SunBlock.kt@@ -40,6 +40,8 @@ fun SunBlock( daylightDurationSec: Double? = null, /** Where to put the sun marker. Pinned by previews so the arc renders identically. */ nowEpochSec: Long = System.currentTimeMillis() / 1000,+ /** `utc_offset_seconds` for the forecast location; null renders in the device zone. */+ utcOffsetSeconds: Int? = null, ) { SquareBlock { Box(Modifier.align(Alignment.TopStart)) {@@ -92,11 +94,11 @@ fun SunBlock( verticalArrangement = Arrangement.spacedBy(3.dp), ) { RiseSetTimeRow(- text = sunriseEpochSec?.let { formatClockTime(it, use24Hour) } ?: "—",+ text = sunriseEpochSec?.let { formatClockTime(it, use24Hour, utcOffsetSeconds) } ?: "—", icon = { m, c -> IconSunny(m, c) }, ) RiseSetTimeRow(- text = sunsetEpochSec?.let { formatClockTime(it, use24Hour) } ?: "—",+ text = sunsetEpochSec?.let { formatClockTime(it, use24Hour, utcOffsetSeconds) } ?: "—", icon = { m, c -> IconClearNight(m, c) }, ) if (daylightDurationSec != null) {
weather/src/test/java/com/vayunmathur/weather/domain/DomainFormatTest.kt
diff --git a/weather/src/test/java/com/vayunmathur/weather/domain/DomainFormatTest.kt b/weather/src/test/java/com/vayunmathur/weather/domain/DomainFormatTest.ktindex 9b278dfba8..d9c2713131 100644--- a/weather/src/test/java/com/vayunmathur/weather/domain/DomainFormatTest.kt+++ b/weather/src/test/java/com/vayunmathur/weather/domain/DomainFormatTest.kt@@ -92,6 +92,26 @@ class DomainFormatTest { assertTrue(formatHourAxisLabel(epoch, true).isNotBlank()) } + @Test fun epochLabelsLocationOffset() {+ // 2026-01-01T00:00Z viewed as a location at UTC+5:30 (05:30 local).+ val epoch = 1767225600L+ assertEquals(epoch, parseLocalIsoToEpochSec("2026-01-01T05:30", 19800))+ assertEquals("5 AM", formatStripHour(epoch, false, 19800))+ assertEquals("5", formatStripHour(epoch, true, 19800))+ assertEquals("05:30", formatClockTime(epoch, true, 19800))+ assertTrue(formatClockTime(epoch, false, 19800).contains("5:30"))+ assertEquals("05:00", formatHourAxisLabel(epoch, true, 19800))+ // Same instant viewed as a location at UTC-5 (19:00 on the previous day).+ assertEquals("7 PM", formatStripHour(epoch, false, -18000))+ assertEquals("19", formatStripHour(epoch, true, -18000))+ assertEquals("19:00", formatClockTime(epoch, true, -18000))+ // Explicit-offset rendering must not depend on the device time zone.+ java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("America/Denver"))+ assertEquals("5 AM", formatStripHour(epoch, false, 19800))+ assertEquals("05:30", formatClockTime(epoch, true, 19800))+ java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("UTC"))+ }+ @Test fun metricMetadata() { assertEquals("temperature_2m", WeatherMetric.Temperature.bundleHourlyVar) assertEquals("uv_index", WeatherMetric.UvIndex.bundleHourlyVar)