From 40336b570fe0335a051715eb7408f2179333e794 Mon Sep 17 00:00:00 2001 From: Avery Sterk Date: Mon, 20 Apr 2026 09:01:20 -0500 Subject: [PATCH] WeatherSpec: compute night correctly when sunrise and sunset are on different UTC dates --- .../freeyourgadget/gadgetbridge/model/WeatherSpec.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.kt b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.kt index b62601da12..39fc2f1a25 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.kt +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.kt @@ -168,9 +168,12 @@ class WeatherSpec() : Parcelable { fun isTimeNight(unixTimeMilliSeconds: Long): Boolean { if (isPolarNight()) return true if (isPolarDay()) return false - val millisPastMidnight = unixTimeMilliSeconds % 86400000 - if ( millisPastMidnight < (this.sunRise % 86400 * 1000L ) ) return true - return ( millisPastMidnight > (this.sunSet % 86400 * 1000L ) ) + // Compute where our time falls relative to sunrise. Negative numbers mean before sunrise. + val millisAfterSunrise = unixTimeMilliSeconds - (this.sunRise * 1000L) + // Compute where sunset falls relative to sunrise. We assume it's always after, thus giving a positive number. + val lengthOfSolarDayInMillis = (this.sunSet - this.sunRise) * 1000L + // Map the input time into positive time in a 24-hour solar cycle, and compare to sunset. + return ( millisAfterSunrise.mod(86400000) > lengthOfSolarDayInMillis ) } fun getLocationObject(): Location? {