Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions Solar/Solar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
56 changes: 56 additions & 0 deletions SolarTests/SolarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading