Skip to content

imvenj/SunCalc.swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SunCalc.swift

A direct port of suncalc javascript library (commit b08d1f6f8). Which means this port inherits the same accuracy and issues with the original javascript library.

Updated

Now SunCalc can handle polar region's "Mid-night sun". Errors are now embeded in SunCalc class:

SunCalc.SolarEventError.sunNeverRise
SunCalc.SolarEventError.sunNeverSet

SunCalc.LunarEventError.moonNeverRise(Date?) // An optional date for moonset time.
SunCalc.LunarEventError.moonNeverSet(Date?) // An optional date for moonrise time.

Location now become SunCalc.Location.

Examples

// DateFormatter
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short
formatter.timeZone = .current

// Sunrise and sunset
let sunCalc = SunCalc()
let now = Date()
let location = SunCalc.Location(latitude: 25.0, longitude: 120.0)
if let riseTime = try? sunCalc.time(ofDate: now, forSolarEvent: .sunrise, atLocation: location) {
    let sunrise = formatter.string(from: riseTime)
    print("Sunrise: \(sunrise)")
}
if let setTime = try? sunCalc.time(ofDate: now, forSolarEvent: .sunset, atLocation: location) {
    let sunset = formatter.string(from: setTime)
    print("Sunset: \(sunset)")
}

// Moon rise and moon set
let moonTimes = try? sunCalc.moonTimes(date: now, location: location)
if let moonTimes = moonTimes {
    print("Moonrise: \(formatter.string(from: moonTimes.moonRiseTime))")
    print("Moonset: \(formatter.string(from: moonTimes.moonSetTime))")
}

Known Issue

Moon rise and set time sometimes not correct. See Issue #1

LICENSE

BSD License

Copyright (c) 2018, Venj Chu

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

About

A swift port of suncalc javascript library.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages