1919import Foundation
2020import CoreLocation
2121
22- public class Sun {
22+ public struct Sun : Identifiable , Sendable {
23+ public let id : UUID = UUID ( )
2324
2425 /*--------------------------------------------------------------------
2526 Public get Variables
2627 *-------------------------------------------------------------------*/
2728
2829 public private( set) var location : CLLocation
2930 public private( set) var timeZone : TimeZone
30- public private( set) var date : Date = Date ( )
31+ public private( set) var date : Date
3132
3233 /*--------------------------------------------------------------------
3334 Sun Events during the day
@@ -70,22 +71,22 @@ public class Sun {
7071
7172 ///Date at which morning Blue Hour starts. Sun at -6 degrees elevation = civil dusk
7273 public var morningBlueHourStart : Date {
73- return civilDawn
74+ civilDawn
7475 }
7576
7677 ///Date at which morning Blue Hour ends. Sun at -4 degrees elevation = morning golden hour start
7778 public var morningBlueHourEnd : Date {
78- return morningGoldenHourStart
79+ morningGoldenHourStart
7980 }
8081
8182 ///Date at which evening Blue Hour starts. Sun at -4 degrees elevation = evening golden hour end
8283 public var eveningBlueHourStart : Date {
83- return eveningGoldenHourEnd
84+ eveningGoldenHourEnd
8485 }
8586
8687 ///Date at which morning Blue Hour ends. Sun at -6 degrees elevation = Civil Dawn
8788 public var eveningBlueHourEnd : Date {
88- return civilDusk
89+ civilDusk
8990 }
9091
9192
@@ -102,12 +103,12 @@ public class Sun {
102103
103104 // Sun azimuth for (Location,Date) in Self
104105 public var azimuth : Angle {
105- return self . sunHorizonCoordinates. azimuth
106+ sunHorizonCoordinates. azimuth
106107 }
107108
108109 // Sun altitude for (Location,Date) in Self
109110 public var altitude : Angle {
110- return self . sunHorizonCoordinates. altitude
111+ sunHorizonCoordinates. altitude
111112 }
112113
113114 public private( set) var sunEquatorialCoordinates : EquatorialCoordinates = . init( declination: . zero)
@@ -132,12 +133,12 @@ public class Sun {
132133
133134 /// Longitude of location
134135 public var longitude : Angle {
135- return . init( degrees: self . location. coordinate. longitude)
136+ . init( degrees: location. coordinate. longitude)
136137 }
137138
138139 /// Latitude of Location
139140 public var latitude : Angle {
140- return . init( degrees: self . location. coordinate. latitude)
141+ . init( degrees: location. coordinate. latitude)
141142 }
142143
143144 /// Returns daylight time in seconds
@@ -162,9 +163,9 @@ public class Sun {
162163 /// Returns True if is night
163164 public var isNight : Bool {
164165 if !isCircumPolar {
165- return date < sunrise || date > sunset
166+ date < sunrise || date > sunset
166167 } else {
167- return isAlwaysNight
168+ isAlwaysNight
168169 }
169170 }
170171
@@ -219,23 +220,27 @@ public class Sun {
219220
220221 /// Returns true if for (Location,Date) is always daylight (e.g Tromso city in Winter)
221222 public var isAlwaysNight : Bool {
222- return sunset - TWO_HOURS_IN_SECONDS < sunrise
223+ sunset - TWO_HOURS_IN_SECONDS < sunrise
223224 }
224225
225226 /*--------------------------------------------------------------------
226227 Initializers
227228 *-------------------------------------------------------------------*/
228229
229- public init ( location: CLLocation , timeZone: Double ) {
230- let timeZoneSeconds : Int = Int ( timeZone * SECONDS_IN_ONE_HOUR)
231- self . timeZone = TimeZone . init ( secondsFromGMT: timeZoneSeconds) ?? . current
230+ public init ( location: CLLocation , timeZone: TimeZone , date: Date = Date ( ) ) {
231+ self . timeZone = timeZone
232232 self . location = location
233+ self . date = date
234+
233235 refresh ( )
234236 }
235237
236- public init ( location: CLLocation , timeZone: TimeZone ) {
237- self . timeZone = timeZone
238+ init ( location: CLLocation , timeZone: Double , date: Date = Date ( ) ) {
239+ let timeZoneSeconds : Int = Int ( timeZone * SECONDS_IN_ONE_HOUR)
240+ self . timeZone = TimeZone . init ( secondsFromGMT: timeZoneSeconds) ?? . current
238241 self . location = location
242+ self . date = date
243+
239244 refresh ( )
240245 }
241246
@@ -247,7 +252,7 @@ public class Sun {
247252 Changing date of interest
248253 *-------------------------------------------------------------------*/
249254
250- public func setDate( _ newDate: Date ) {
255+ public mutating func setDate( _ newDate: Date ) {
251256 let newDay = calendar. dateComponents ( [ . day, . month, . year] , from: newDate)
252257 let oldDay = calendar. dateComponents ( [ . day, . month, . year] , from: date)
253258
@@ -266,15 +271,15 @@ public class Sun {
266271 /// - Parameters:
267272 /// - newLocation: New location
268273 /// - newTimeZone: New timezone for the given location. Is highly recommanded to pass a Timezone initialized via .init(identifier: ) method
269- public func setLocation( _ newLocation: CLLocation , _ newTimeZone: TimeZone ) {
274+ public mutating func setLocation( _ newLocation: CLLocation , _ newTimeZone: TimeZone ) {
270275 timeZone = newTimeZone
271276 location = newLocation
272277 refresh ( )
273278 }
274279
275280 /// Changing only the location
276281 /// - Parameter newLocation: New Location
277- public func setLocation( _ newLocation: CLLocation ) {
282+ public mutating func setLocation( _ newLocation: CLLocation ) {
278283 location = newLocation
279284 refresh ( )
280285 }
@@ -284,7 +289,7 @@ public class Sun {
284289 /// - Parameters:
285290 /// - newLocation: New Location
286291 /// - newTimeZone: New Timezone express in Double. For timezones which differs of half an hour add 0.5,
287- public func setLocation( _ newLocation: CLLocation , _ newTimeZone: Double ) {
292+ public mutating func setLocation( _ newLocation: CLLocation , _ newTimeZone: Double ) {
288293 let timeZoneSeconds : Int = Int ( newTimeZone * SECONDS_IN_ONE_HOUR)
289294 timeZone = TimeZone ( secondsFromGMT: timeZoneSeconds) ?? . current
290295 location = newLocation
@@ -298,14 +303,14 @@ public class Sun {
298303
299304 /// Changing only the timezone.
300305 /// - Parameter newTimeZone: New Timezone
301- public func setTimeZone( _ newTimeZone: TimeZone ) {
306+ public mutating func setTimeZone( _ newTimeZone: TimeZone ) {
302307 timeZone = newTimeZone
303308 refresh ( )
304309 }
305310
306311 /// Is highly recommanded to use the other method to change timezone. This will be kept only for backwards retrocompatibility.
307312 /// - Parameter newTimeZone: New Timezone express in Double. For timezones which differs of half an hour add 0.5,
308- public func setTimeZone( _ newTimeZone: Double ) {
313+ public mutating func setTimeZone( _ newTimeZone: Double ) {
309314 let timeZoneSeconds : Int = Int ( newTimeZone * SECONDS_IN_ONE_HOUR)
310315 timeZone = TimeZone ( secondsFromGMT: timeZoneSeconds) ?? . current
311316 refresh ( )
@@ -431,7 +436,7 @@ public class Sun {
431436 /// Compute civil dusk and Civil Dawn time
432437 ///
433438 /// - Parameter needToComputeAgainSunEvents: True if Sunrise,Sunset and all the others daily sun events have to be computed.
434- private func refresh( needToComputeSunEvents: Bool = true ) {
439+ private mutating func refresh( needToComputeSunEvents: Bool = true ) {
435440 updateSunCoordinates ( )
436441
437442 if ( needToComputeSunEvents) {
@@ -484,7 +489,7 @@ public class Sun {
484489
485490
486491 /// Updates Horizon coordinates, Ecliptic coordinates and Equatorial coordinates of the Sun
487- private func updateSunCoordinates( ) {
492+ private mutating func updateSunCoordinates( ) {
488493 //Step1:
489494 //Convert LCT to UT, GST, and LST times and adjust the date if needed
490495 let gstHMS = uT2GST ( self . date)
@@ -832,3 +837,19 @@ public class Sun {
832837 }
833838
834839}
840+
841+ extension Sun : Equatable {
842+ public static func == ( lhs: Sun , rhs: Sun ) -> Bool {
843+ lhs. location == rhs. location &&
844+ lhs. timeZone == rhs. timeZone &&
845+ lhs. date == rhs. date
846+ }
847+ }
848+
849+ extension Sun : Hashable {
850+ public func hash( into hasher: inout Hasher ) {
851+ hasher. combine ( location)
852+ hasher. combine ( timeZone)
853+ hasher. combine ( date)
854+ }
855+ }
0 commit comments