Skip to content

Commit

Permalink
fix-wrong-solar-midnight (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Mariniello <nmariniello.dev@icloud.com>
  • Loading branch information
davideilmito and seldon1000 authored Jun 23, 2024
1 parent e9ae20c commit 086d345
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
Binary file not shown.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// swift-tools-version:5.9
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SunKit",
platforms: [
.iOS(.v14), .macOS(.v10_15), .tvOS(.v9), .watchOS(.v6)
.iOS(.v14), .macOS(.v10_15), .tvOS(.v12), .watchOS(.v6)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
Expand Down
18 changes: 7 additions & 11 deletions Sources/SunKit/Sun.swift
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,7 @@ public struct Sun: Identifiable, Sendable {
let secondsForSolarNoon = secondsForUTCSolarNoon + Double(timeZoneInSeconds)
let startOfTheDay = calendar.startOfDay(for: date)

let hoursMinutesSeconds: (Int, Int, Int) = secondsToHoursMinutesSeconds(Int(secondsForSolarNoon))

let solarNoon = calendar.date(bySettingHour: hoursMinutesSeconds.0, minute: hoursMinutesSeconds.1, second: hoursMinutesSeconds.2, of: startOfTheDay)
let solarNoon = calendar.date(byAdding: .second, value: Int(secondsForSolarNoon) , to: startOfTheDay)

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

let secondsForUTCSolarMidnight = (-4 * location.coordinate.longitude - equationOfTime) * 60

var calendarUTC:Calendar = .init(identifier: .gregorian)
calendarUTC.timeZone = .init(secondsFromGMT: 0)!

let startOfTheDay = calendarUTC.startOfDay(for: date)

let solarMidnight = startOfTheDay.addingTimeInterval(secondsForUTCSolarMidnight)
let secondsForUTCSolarMidnight = (0 - 4 * location.coordinate.longitude - equationOfTime) * 60
let secondsForSolarMidnight = secondsForUTCSolarMidnight + Double(timeZoneInSeconds)
let startOfTheDay = calendar.startOfDay(for: date)

let solarMidnight = calendar.date(byAdding: .second, value: Int(secondsForSolarMidnight) , to: startOfTheDay)

return solarMidnight
}



/// Computes the Sunrise time for self.date
/// - Returns: Sunrise time
private func getSunrise() -> Date? {
Expand Down

0 comments on commit 086d345

Please sign in to comment.