Coder Social home page Coder Social logo

martinc / daylight Goto Github PK

View Code? Open in Web Editor NEW
14.0 2.0 5.0 42 KB

A swift package that provides NOAA astronomical algorithms for sunrise and sunset times

License: MIT License

Swift 96.58% Ruby 3.42%
swift sunrise sunset dawn dusk noaa astronomy

daylight's Introduction

daylight

A swift package that provides NOAA astrological algorithms for sunrise and sunset times

A port of the go library astrotime

You can either use the interface with a Day struct containing the calendar date of interest, or use the interface of a Date object.

Note that using the Date object's interface will use the calendar date at the timezone of the Location.

The SolarEvent enum is used to specify the event of interest.

Example

let julyFirst = Day(year: 2015, month: 7, day: 1)
let newyork = Location(tz: TimeZone(identifier: "America/New_York")!,
                       coords: Coords(latitude: 40.642, longitude: -74.017))
let sunrise = julyFirst.timeOf(.sunrise, at: newyork)

//Format it to display in location's timezone
let dateFormatter = DateFormatter()
dateFormatter.timeZone = newyork.tz
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .long
print("Sunrise: \(dateFormatter.string(from: sunrise))")

Interface

public enum SolarEvent {
    case sunrise
    case noon
    case sunset
    case civilDawn
    case civilDusk
    case nauticalDawn
    case nauticalDusk
    case astronomicalDawn
    case astronomicalDusk
}

public struct Location {
    public let tz: TimeZone
    public let coords: CLLocationCoordinate2D
}

public struct Day {
    public let year: Int, month: Int, day: Int
    public func timeOf(_ solarEvent: SolarEvent, at location: Location) -> Date
}

public extension Date {
    public func timeOf(_ solarEvent: SolarEvent, at location: Location) -> Date
    public func timeOfNext(_ solarEvent: SolarEvent, at location: Location) -> Date
}

Running tests

In addition to with XCode, you can run the test suite via the command-line with the following command:

swift test

daylight's People

Contributors

martinc avatar mjradwin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

daylight's Issues

Enhancement - Location is in Daylight

I stumbled across this looking for a map overlay for MapKit to show daylight/night over map of the world. I am glad to see a solid foundation in this project.

Here's an enhancement I'd like to see:

Given a longitude and latitude, at the current time, is that location in daylight or darkness.

[User could choose what they consider daylight from the SolarEvent enum. It would be great if we did not have to worry about time zone and just a location on earth at the current time.]

So maybe something like:

let currentLocation = CLLocationCoordinate2D(latitude: 40.642, longitude: -74.017)
let inDaylight: Bool = currentLocation.inDaylight(usingDawn: .civilDawn, andDusk: .civilDusk)

I'll be playing around with what you have. Beats starting from NASA's codeI've seen since this is already migrated to Swift.

Daylight framework crashes app if the location is inside polar ring.

Locations inside polar ring, will have polar days (sun never sets) and polar nights (sun never rises).

private func hourAngleOfSunrise(latitude: Float64, solarDeclination: Float64, solarElevation: Float64) throws -> Float64

This function return NAN when sun never rises or sun never sets.

I have made a work around to solve this problem by throw an error from there (and re-throw it along the method chain). I don't have a better solution to handle this situation, so I hope you can look into it.

The workaround I've made was in commit eca031c.

Thank you.

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.