From 4641098d33cac5e09f8fb18116c21d7d0d4e2126 Mon Sep 17 00:00:00 2001 From: Chris Howell Date: Sun, 30 Aug 2026 23:10:07 +0100 Subject: [PATCH] fix: isDaytime wrong near UTC midnight for non-UTC longitudes isDaytime compared date against the sunrise/sunset window anchored to date's UTC calendar day. When the local solar day straddles UTC midnight (evenings west of Greenwich, mornings east of it), the window containing date belongs to the previous or next UTC day, so the sun could be up while isDaytime returned false. Check the stored window first, then fall back to the windows computed for the adjacent UTC days, skipping days with no sunrise/sunset near the polar circles. Boundary semantics (>= sunrise, < sunset) are unchanged. Adds regression tests pinning both sides of the boundary in each direction (Minneapolis evening, Tokyo morning). Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01PYPBCzQiXBvadC3y6Ae6FH --- Solar/Solar.swift | 35 +++++++++++++---------- SolarTests/SolarTests.swift | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 15 deletions(-) diff --git a/Solar/Solar.swift b/Solar/Solar.swift index d2d67e6..97fa9ac 100644 --- a/Solar/Solar.swift +++ b/Solar/Solar.swift @@ -204,24 +204,29 @@ public struct Solar { extension Solar { - /// Whether the location specified by the `latitude` and `longitude` is in daytime on `date` + fileprivate static let secondsPerDay: TimeInterval = 60 * 60 * 24 + + /// Whether the location specified by the `latitude` and `longitude` is in daytime on `date`. + /// The `sunrise` / `sunset` window is anchored to the UTC calendar day of `date`, so when + /// the local solar day straddles UTC midnight the window containing `date` can belong to + /// the previous or next UTC day; those windows are checked too. /// - Complexity: O(1) public var isDaytime: Bool { - guard - let sunrise = sunrise, - let sunset = sunset - else { - return false + if let sunrise = sunrise, let sunset = sunset, date >= sunrise, date < sunset { + return true + } + + return [-Solar.secondsPerDay, Solar.secondsPerDay].contains { dayOffset in + let candidateDate = date.addingTimeInterval(dayOffset) + guard + let sunrise = calculate(.sunrise, for: candidateDate, and: .official), + let sunset = calculate(.sunset, for: candidateDate, and: .official) + else { + return false + } + + return date >= sunrise && date < sunset } - - let beginningOfDay = sunrise.timeIntervalSince1970 - let endOfDay = sunset.timeIntervalSince1970 - let currentTime = self.date.timeIntervalSince1970 - - let isSunriseOrLater = currentTime >= beginningOfDay - let isBeforeSunset = currentTime < endOfDay - - return isSunriseOrLater && isBeforeSunset } /// Whether the location specified by the `latitude` and `longitude` is in nighttime on `date` diff --git a/SolarTests/SolarTests.swift b/SolarTests/SolarTests.swift index 05e9429..be03bac 100644 --- a/SolarTests/SolarTests.swift +++ b/SolarTests/SolarTests.swift @@ -122,6 +122,62 @@ struct SolarTests { #expect(solar.isNighttime, "isNighttime is false for date: \(afterSunset) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)") } + // MARK: - UTC day boundary + // + // For locations west of Greenwich in the evening (and east of Greenwich in the + // morning), the local solar day straddles UTC midnight. isDaytime must reflect + // whether the sun is actually up at `date`, not whether `date` falls within the + // sunrise/sunset window of its UTC calendar day. + + private static let minneapolis = CLLocationCoordinate2D(latitude: 44.9778, longitude: -93.2650) + private static let tokyo = CLLocationCoordinate2D(latitude: 35.6762, longitude: 139.6503) + + @Test("isDaytime is true before sunset when the UTC day is already tomorrow") + func isDaytimeIsTrueBeforeSunsetWhenUTCDayIsAlreadyTomorrow() throws { + // Minneapolis, 2026-06-15 20:30 CDT (2026-06-16 01:30 UTC). + // The sun is up until ~21:02 CDT (~02:02 UTC), but in UTC the date is already June 16. + let eveningSunUp = Date(timeIntervalSince1970: 1781573400) + + let solar = try #require(Solar(for: eveningSunUp, coordinate: Self.minneapolis)) + + #expect(solar.isDaytime, "isDaytime is false for date: \(eveningSunUp) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)") + #expect(!solar.isNighttime, "isNighttime is true for date: \(eveningSunUp) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)") + } + + @Test("isDaytime is false after sunset when the UTC day is already tomorrow") + func isDaytimeIsFalseAfterSunsetWhenUTCDayIsAlreadyTomorrow() throws { + // Minneapolis, 2026-06-15 21:30 CDT (2026-06-16 02:30 UTC), shortly after the ~21:02 CDT sunset. + let eveningSunDown = Date(timeIntervalSince1970: 1781577000) + + let solar = try #require(Solar(for: eveningSunDown, coordinate: Self.minneapolis)) + + #expect(!solar.isDaytime, "isDaytime is true for date: \(eveningSunDown) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)") + #expect(solar.isNighttime, "isNighttime is false for date: \(eveningSunDown) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)") + } + + @Test("isDaytime is true after sunrise when the UTC day is still yesterday") + func isDaytimeIsTrueAfterSunriseWhenUTCDayIsStillYesterday() throws { + // Tokyo, 2026-06-16 08:30 JST (2026-06-15 23:30 UTC). + // The sun rose at ~04:25 JST, but in UTC the date is still June 15. + let morningSunUp = Date(timeIntervalSince1970: 1781566200) + + let solar = try #require(Solar(for: morningSunUp, coordinate: Self.tokyo)) + + #expect(solar.isDaytime, "isDaytime is false for date: \(morningSunUp) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)") + #expect(!solar.isNighttime, "isNighttime is true for date: \(morningSunUp) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)") + } + + @Test("isDaytime is false before sunrise when the UTC day is still yesterday") + func isDaytimeIsFalseBeforeSunriseWhenUTCDayIsStillYesterday() throws { + // Tokyo, 2026-06-16 02:30 JST (2026-06-15 17:30 UTC), well before the ~04:25 JST sunrise. + let nightBeforeSunrise = Date(timeIntervalSince1970: 1781544600) + + let solar = try #require(Solar(for: nightBeforeSunrise, coordinate: Self.tokyo)) + + #expect(!solar.isDaytime, "isDaytime is true for date: \(nightBeforeSunrise) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)") + #expect(solar.isNighttime, "isNighttime is false for date: \(nightBeforeSunrise) with sunrise: \(solar.sunrise!), sunset: \(solar.sunset!)") + } + @Test("Solar init returns nil given an invalid coordinate") func solarInitReturnsNilGivenInvalidCoordinate() { let invalidCoordinate1 = CLLocationCoordinate2D(latitude: -100, longitude: 0)