Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resubmitted: Fixed issue when SunSet/Sunrise is on a different day from variable date #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 14 additions & 5 deletions Solar/Solar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down