jceaser / sun

A basic python->Swift port of Michel Anders' popular code to calculate rising and setting times for the sun given a latitude, longitude, and timezone.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sun rise and setting times in Swift

Swift code to calculate sun rise and sun setting times based on timezone and latitude and longitude values.

NOTE: This project is a direct port of python code to Swift code. Only the most basic of changes were made to get it to compile in Swift.

Based on

Usage

Getting help:

>sun --help
Returns JSON for the current raise and setting times of the sun.

Flag Long Flag    : Description
---- ------------ : ----------------------------------------------------------------
-h   --help       : Help/Manual
-l   --latitude   : Decimal value for latitude
-L   --longitude  : Decimal value for longitude
-t   --time-zone  : Offset in hours

Example call which returns JSON:

>sun --latitude 38.88333 --longitude -77.03333 -t -5
{
"sunrise": {"hour": 6, "minute": 56, "second": 45} ,
"noon": {"hour": 12, "minute": 22, "second": 7} ,
"sunset": {"hour": 17, "minute": 47, "second": 29}
}

Using [jq][jq] to pull out specific values:

>sun -t 5 \
	--latitude 38.88333 \
	--longitude -77.03333 \
	| jq '.sunrise.hour,.sunrise.minute'
6
56

Code

Create a Sun object with the latitude and longitude and set timezone offset if needed. Then call sunrise, solarnoon, s.sunset to get back JSON string.

let s = Sun(lat: latitude, long: longitude)
s.timezone = -5.0
let rise: String = s.sunrise(when: Date()) //JSON string
let noon: String = s.solarnoon(when: Date()) //JSON string
let setting: String = s.sunset(when: Date()) //JSON string

Versions:

  • Written and compiled with XCode Version 13.2.1 (13C100).
  • Written under Swift version of 5.5.2 was used.

License

There is a "donut hole" of licenses here. All files but one are under BSD 2-Clause License. The exception is the SunData.swift file. SunData.swift is based on work from others and that code has no posted license being offered to the internet in an instructional manor, meaning that it is probably in the public domain. It is this project's believe that any derived work on that file can not be locked down to any license.

About

A basic python->Swift port of Michel Anders' popular code to calculate rising and setting times for the sun given a latitude, longitude, and timezone.

License:BSD 2-Clause "Simplified" License


Languages

Language:Swift 100.0%