Skip to content

Commit 086d345

Browse files
fix-wrong-solar-midnight (#53)
Co-authored-by: Nicolas Mariniello <[email protected]>
1 parent e9ae20c commit 086d345

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// swift-tools-version:5.9
1+
// swift-tools-version:5.3
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
77
name: "SunKit",
88
platforms: [
9-
.iOS(.v14), .macOS(.v10_15), .tvOS(.v9), .watchOS(.v6)
9+
.iOS(.v14), .macOS(.v10_15), .tvOS(.v12), .watchOS(.v6)
1010
],
1111
products: [
1212
// Products define the executables and libraries a package produces, and make them visible to other packages.

Sources/SunKit/Sun.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,7 @@ public struct Sun: Identifiable, Sendable {
600600
let secondsForSolarNoon = secondsForUTCSolarNoon + Double(timeZoneInSeconds)
601601
let startOfTheDay = calendar.startOfDay(for: date)
602602

603-
let hoursMinutesSeconds: (Int, Int, Int) = secondsToHoursMinutesSeconds(Int(secondsForSolarNoon))
604-
605-
let solarNoon = calendar.date(bySettingHour: hoursMinutesSeconds.0, minute: hoursMinutesSeconds.1, second: hoursMinutesSeconds.2, of: startOfTheDay)
603+
let solarNoon = calendar.date(byAdding: .second, value: Int(secondsForSolarNoon) , to: startOfTheDay)
606604

607605
return solarNoon
608606
}
@@ -611,19 +609,17 @@ public struct Sun: Identifiable, Sendable {
611609
/// - Returns: Solar midnight time
612610
private func getSolarMidnight() -> Date? {
613611

614-
let secondsForUTCSolarMidnight = (-4 * location.coordinate.longitude - equationOfTime) * 60
615-
616-
var calendarUTC:Calendar = .init(identifier: .gregorian)
617-
calendarUTC.timeZone = .init(secondsFromGMT: 0)!
618-
619-
let startOfTheDay = calendarUTC.startOfDay(for: date)
620-
621-
let solarMidnight = startOfTheDay.addingTimeInterval(secondsForUTCSolarMidnight)
612+
let secondsForUTCSolarMidnight = (0 - 4 * location.coordinate.longitude - equationOfTime) * 60
613+
let secondsForSolarMidnight = secondsForUTCSolarMidnight + Double(timeZoneInSeconds)
614+
let startOfTheDay = calendar.startOfDay(for: date)
622615

616+
let solarMidnight = calendar.date(byAdding: .second, value: Int(secondsForSolarMidnight) , to: startOfTheDay)
617+
623618
return solarMidnight
624619
}
625620

626621

622+
627623
/// Computes the Sunrise time for self.date
628624
/// - Returns: Sunrise time
629625
private func getSunrise() -> Date? {

0 commit comments

Comments
 (0)