[ad_1]
I would like to create a function that returns a BOOL in order to know if we are in the aeronautical night or not.
I use the solar package which allows me to know the civil sunset.
The aeronautical night ends 1 hour before the civil sunrise
So can you help me with:
-> Calculate the aeronautical night
-> Compare the 2 hours and return the correct Bool
import Foundation
import Solar
class Time: Comparable, Equatable {
init(_ date: Date) {
//get the current calender
let calendar = Calendar.current
//get just the minute and the hour of the day passed to it
let dateComponents = calendar.dateComponents([.hour, .minute], from: date)
//calculate the seconds since the beggining of the day for comparisions
let dateSeconds = dateComponents.hour! * 3600 + dateComponents.minute! * 60
//set the varibles
secondsSinceBeginningOfDay = dateSeconds
hour = dateComponents.hour!
minute = dateComponents.minute!
}
init(_ hour: Int, _ minute: Int) {
//calculate the seconds since the beggining of the day for comparisions
let dateSeconds = hour * 3600 + minute * 60
//set the varibles
secondsSinceBeginningOfDay = dateSeconds
self.hour = hour
self.minute = minute
}
var hour : Int
var minute: Int
var date: Date {
//get the current calender
let calendar = Calendar.current
//create a new date components.
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
return calendar.date(byAdding: dateComponents, to: Date())!
}
/// the number or seconds since the beggining of the day, this is used for comparisions
private let secondsSinceBeginningOfDay: Int
//comparisions so you can compare times
static func == (lhs: Time, rhs: Time) -> Bool {
return lhs.secondsSinceBeginningOfDay == rhs.secondsSinceBeginningOfDay
}
static func < (lhs: Time, rhs: Time) -> Bool {
return lhs.secondsSinceBeginningOfDay < rhs.secondsSinceBeginningOfDay
}
static func <= (lhs: Time, rhs: Time) -> Bool {
return lhs.secondsSinceBeginningOfDay <= rhs.secondsSinceBeginningOfDay
}
static func >= (lhs: Time, rhs: Time) -> Bool {
return lhs.secondsSinceBeginningOfDay >= rhs.secondsSinceBeginningOfDay
}
static func > (lhs: Time, rhs: Time) -> Bool {
return lhs.secondsSinceBeginningOfDay > rhs.secondsSinceBeginningOfDay
}
}
extension Date {
var time: Time {
return Time(self)
}
}
and in other swift file
import UIKit
import Foundation
import CoreLocation
import CoreLocationUI
import Solar
let profileViewModel = ProfileViewModel.shared
/*
=> starts 30 minutes after sunset at latitudes between 30 ° and 60 °
=> ends 30 minutes before sunrise at latitudes between 30 ° and 60 °
=> starts 15 minutes after sunset at latitudes between 0 ° and 30 °
=> ends 15 minutes before sunrise at latitudes between 0 ° and 30 °
*/
func latitudeCheck(latitude: CLLocationDegrees) -> (Bool) {
let locationManager = LocationManager()
if locationManager.location != nil {
if locationManager.location?.latitude ?? 0 < 30 &&
locationManager.location?.latitude ?? 0 > 60 {
return true
} else {
return false
}
}else {
return false
}
}
/*
The fuel reserve during the day is 30 minutes of flight
and at night the reserve is 45 minutes
*/
func solarSystem() -> Int {
var location: CLLocationDegrees?
if latitudeCheck(latitude: location ?? 0){
}
return 1
}
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
let manager = CLLocationManager()
@Published var location: CLLocationCoordinate2D?
override init() {
super.init()
manager.delegate = self
}
func requestLocation() {
manager.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
location = locations.first?.coordinate
}
}
thanks
[ad_2]