Skip to content

Commit

Permalink
Remove broken code, add beginnings on nextSolarEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
jnewkirk committed Jan 30, 2025
1 parent 187c0d4 commit a66a0a3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
4 changes: 0 additions & 4 deletions Sources/SunKit/Solar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ public struct Solar {
return solars;
}

public func solarAngle(hour: int, minute: int, second: int) {

}

/// TODO: Sunlight angle
/// TODO: Moon phase
/// TODO: Moon illumination
Expand Down
43 changes: 43 additions & 0 deletions Tests/SunKitTests/SolarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,47 @@ struct SolarTests {
let daylight = try #require(solar.daylight)
#expect(daylight.duration == 36497.0)
}

@Test(.disabled())
func nextSolarEvents() async throws {
let now = Date.now.addingTimeInterval(60 * 60 * 2)
let nextSolarEvent = try #require(now.nextSolarEvents(coordinate: Constant.Cupertino))

#expect(nextSolarEvent.isSunrise)
debugPrint(nextSolarEvent.events.actual)
}
}

private extension Date {
static var calendar: Calendar {
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = Date.utcTimezone

return calendar
}

static let utcTimezone = TimeZone(identifier: "UTC")!

func midnightLocal(timeZone: TimeZone = .current) -> Date {
let nowComponents = Date.calendar.dateComponents(in: timeZone, from: self)
let midnightComponents = DateComponents(timeZone: timeZone, year: nowComponents.year, month: nowComponents.month, day: nowComponents.day, hour: 0, minute: 0, second: 0)
return Date.calendar.date(from: midnightComponents)!
}

func nextSolarEvents(coordinate: CLLocationCoordinate2D, timeZone: TimeZone = .current) -> (isSunrise: Bool, events: SolarEvents)? {
let midnightLocal = midnightLocal(timeZone: timeZone)

let todayAndTomorrow = Solar.makeRange(from: midnightLocal, at: coordinate, forDays: 2)
guard let todayDawn = todayAndTomorrow[0].dawn else { return nil }
guard let todayDusk = todayAndTomorrow[0].dusk else { return nil }
guard let tomorrowDawn = todayAndTomorrow[1].dawn else { return nil }

if (self < todayDawn.actual) {
return (isSunrise: true, todayDawn)
} else if (self < todayDusk.actual) {
return (isSunrise: false, todayDusk)
} else {
return (isSunrise: true, tomorrowDawn)
}
}
}

0 comments on commit a66a0a3

Please sign in to comment.