Comments (6)
I found a workaround that works for me for the moment. I basically strip the date component from the dates and only use the time components. It's not that precise as it could compare the current date/time to the sunrise & sunset time of tomorrow, but that's ok for my usage (I use it to switch App color theme at sunset / sunrise).
It's not fail safe as it doesn't work for very northern / very southern locations where the sunset is after midnight / sunrise if before midnight or even doesn't set or rise at all, so a fix or other workaround would be appreciated.
Here it is:
public var isDaytime: Bool {
guard
let sunrise = sunrise,
let sunset = sunset
else {
return false
}
let beginningOfDay = sunrise.secondsFromMidnight() // previously sunrise.timeIntervalSince1970
let endOfDay = sunset.secondsFromMidnight() // previously sunset.timeIntervalSince1970
let currentTime = self.date.secondsFromMidnight() // previously self.date.timeIntervalSince1970
let isSunriseOrLater = currentTime >= beginningOfDay
let isBeforeSunset = currentTime < endOfDay
return isSunriseOrLater && isBeforeSunset
}
And the date extension:
extension Date {
func secondsFromMidnight() -> Int {
let hours = Calendar.current.component(.hour, from: self)
let minutes = Calendar.current.component(.minute, from: self)
let seconds = Calendar.current.component(.second, from: self)
return hours*60*60 + minutes*60 + seconds
}
}
from solar.
I had encountered this and had a fix in my fork
from solar.
Thank you guidove, my use case matches yours and your solution helps.
from solar.
Hi @guidove! Thank you for the report.
Just to help me pin down this bug and implement a correct fix, would you mind creating a failing test that represents your scenario? This would help tons!
from solar.
Sorry, do not have any experience with making tests. But it's very easy to reproduce by feeding it the lat & lon mentioned above (is Mex city coordinate) and you'll need to set your device timezone to Mexico city as well probably. Then print out calculated sunrise and sunset date&time and compare them to the official ones. (Just Google 'mexico city sunrise sunset' and it will tell you right in the search results.)
from solar.
This solved the problem for me. I hope that this fix is not affected by daylight savings.
sunrise = calculate(.sunrise, for: date, and: .official)! - 3600*24
sunset = calculate(.sunset, for: date, and: .official)! - 3600*24
from solar.
Related Issues (20)
- Needs more Swift
- isNighttime calculation incorrect HOT 4
- Solar Noon HOT 1
- error when excute carthage update HOT 4
- Daylight Saving time HOT 1
- Late sunsets give incorrect date for time zone HOT 1
- Support installation via Carthage for macOS and tvOS
- Is it possible to get the times for a given sun position on a given date ? HOT 2
- Data used is inconsistent with other sources
- Xcode 10 build error HOT 2
- Day Light Saving Issue HOT 1
- Update documentation HOT 1
- Sunrise and Sunset dates off by 1 day. HOT 3
- Cocoapods 'stuck at v.2.1' HOT 5
- Return only time HOT 1
- Xcode 12.5 Build Warning HOT 3
- isDaytime and isNightTime return incorrect values for polar day HOT 1
- WeatherKit HOT 3
- Mismatched times when device timezone and input lat/lon are different
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from solar.