diff --git a/Solar/Solar.swift b/Solar/Solar.swift index d2d67e6..b244b20 100644 --- a/Solar/Solar.swift +++ b/Solar/Solar.swift @@ -214,14 +214,23 @@ extension Solar { return false } - let beginningOfDay = sunrise.timeIntervalSince1970 - let endOfDay = sunset.timeIntervalSince1970 + let todaySunrise = sunrise.timeIntervalSince1970 + let todaySunset = sunset.timeIntervalSince1970 let currentTime = self.date.timeIntervalSince1970 - let isSunriseOrLater = currentTime >= beginningOfDay - let isBeforeSunset = currentTime < endOfDay + let yesterday = date.addingTimeInterval(TimeInterval(exactly: -86400.0)!) + let yesterdaySunrise = calculate(.sunrise, for: yesterday, and: .official)?.timeIntervalSince1970 + let yesterdaySunset = calculate(.sunset, for: yesterday, and: .official)?.timeIntervalSince1970 - return isSunriseOrLater && isBeforeSunset + let tomorrow = date.addingTimeInterval(TimeInterval(exactly: 86400.0)!) + let tomorrowSunrise = calculate(.sunrise, for: tomorrow, and: .official)?.timeIntervalSince1970 + let tomorrowSunSet = calculate(.sunset, for: tomorrow, and: .official)?.timeIntervalSince1970 + + let isDayYesterday = yesterdaySunrise! < currentTime && currentTime < yesterdaySunset! + let isDayToday = todaySunrise < currentTime && currentTime < todaySunset + let isDayTomorrow = tomorrowSunrise! < currentTime && currentTime < tomorrowSunSet! + + return isDayYesterday || isDayToday || isDayTomorrow } /// Whether the location specified by the `latitude` and `longitude` is in nighttime on `date`