SunKit is a Swift package which uses math and trigonometry to compute several information about the Sun. This package has been developed by a team of learners relatively new to the Swift programming language, which means that there could be a lot of space for improvements. Every contribution is welcome.
SunKit was first developed as part of a bigger project: Sunlitt. Even though Sunlitt is not meant to be released as Open Source we decided to wrap the fundamental logic of the app and make an open source library out of it.
To compute Sunrise, Sunset, Golden Hour and so on we only need a CLLocation and the time zone of that location. CoreLocation framework is required for SunKit to work.
SunKit only needs a location and the relative time zone to compute every information you need about the Sun.
Everything is computed locally, no internet connection is needed.
Creating a Sun
// Creating a CLLocation object with the coordinates you are interested in let naplesLocation: CLLocation = .init(latitude: 40.84014, longitude: 14.25226) // Timezone for the location of interest. It's highly recommended to initialize it via identifier let timeZoneNaples: Timezone = .init(identifier: "Europe/Rome") ?? .current // Creating the Sun instance which will store all the information you need about sun events and his position var mySun: Sun = .init(location: naplesLocation, timeZone: timeZoneNaples)
// Creating a Date instance let myDate: Date = Date() // Your current date // Setting inside mySun object the date of interest mySun.setDate(myDate) // All the following informations are related to the given location for the date that has just been set // Azimuth of the Sun mySun.azimuth.degrees // Altitude of the Sun mySun.altitude.degrees // Sunrise Date mySun.sunrise // Sunset Date mySun.sunset // Afternoon Golden Hour Start Date mySun.goldenHourStart // Afternoon Golden Hour End Date mySun.goldenHourEnd // To know all the information you can retrieve go to the **Features** section.
Working with Timezones and Dates
To properly show the Sun Date Events use the following DateFormatter.
//Creting a DateFormatter let dateFormatter = DateFormatter() //Properly setting his attributes dateFormatter.locale = .current dateFormatter.timeZone = timeZoneNaples // It shall be the same as the one used to initilize mySun dateFormatter.timeStyle = .full dateFormatter.dateStyle = .full //Printing Sun Date Events with the correct Timezone print("Sunrise: \(dateFormatter.string(from: mySun.sunrise))")
- Sun Azimuth
- Sun Altitude
- First Light Time
- Last Light Time
- Sunrise Time
- Solar Noon Time
- Golden Hour Time
- Sunset Time
- Astronomical Sunrise
- Astronomical Sunset
- Nautical Sunrise
- Nautical Sunset
- Sun Azimuth at Sunrise
- Sun Azimuth at Sunset
- Sun Azimuth at Solar Noon
- Total Daylight Duration
- Total Night Duration
- March Equinox
- June Solstice
- September Equinox
- December Solstice
- NOAA Global Monitoring Division. General Solar Position Calculations: Link.
- PV Education: Link.
- Celestial Calculations: A Gentle Introduction to Computational Astronomy: Link.
Take a look to the other Package, this time about the Moon.
Mobile App Development Best Practices – 03.10
iOS MetaCodable – Supercharge Swift’s Codable implementations with macros meta-programming How to build a Tuist plugin and publish it using...
How to make and use BOM (Bill of Materials) dependencies in Android projects
By using a BOM dependency, you can avoid specifying the versions of each individual library in your app, and let...
Telegram turns 10 years old and revenues stagnate
Telegram seems to want to grow not only through messaging but also through communities, which pretty much means it wants...
MetaCodable – Supercharge Swift’s Codable implementations with macros meta-programming
Supercharge Swift‘s Codable implementations with macros. Overview MetaCodable framework exposes custom macros which can be used to generate dynamic Codable implementations. The core of the framework...
How to get started with Swift Concurrency 🧵 (Beginner Tutorial)
Swift has built-in support for writing asynchronous and parallel code in a structured way. Asynchronous code can be suspended and resumed later,...
Mobile App Development Best Practices – 02.10
Data.ai has summarized the interim results of the year – and once again we have a record. Annual consumer spending...