Coder Social home page Coder Social logo

Comments (6)

guidove avatar guidove commented on August 20, 2024 1

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.

BrandonRoehl avatar BrandonRoehl commented on August 20, 2024 1

I had encountered this and had a fix in my fork

from solar.

hungri-yeti avatar hungri-yeti commented on August 20, 2024

Thank you guidove, my use case matches yours and your solution helps.

from solar.

ceeK avatar ceeK commented on August 20, 2024

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.

guidove avatar guidove commented on August 20, 2024

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.

rawmean avatar rawmean commented on August 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.