SunKit 是一个 Swift 软件包,它使用数学和三角学来计算关于太阳的几个信息。
SunKit 最初是作为一个更大项目的一部分开发的:Sunlitt。 即使 Sunlitt 并不打算开源,我们还是决定封装该应用程序的基本逻辑,并构建一个库,供任何人免费使用、采纳和扩展。
SunKit 在 Apache License 2.0 许可下发布。 为了署名,我们希望您包含 SunKit 标志的未修改矢量图,可从此处获取,以及我们的组织名称“Sunlitt”,两者都以清晰可见的尺寸显示,并且它们链接回我们的网站。
如果您正在为 Apple 平台开发应用程序,我们还要求您在应用程序的 Settings.bundle 文件的“Acknowledgments”(致谢)部分中包含 SunKit 的许可证和版权信息。 我们提供了一个 Settings.bundle 示例,可以从此处下载,并按原样导入到您的 Xcode 项目中。
署名对于确保我们的辛勤工作得到适当认可至关重要,我们感谢您遵守我们的要求。
SunKit 需要 CoreLocation 框架才能工作。 SunKit 只需要一个位置和相应的时区。 所有计算都在本地进行,不需要互联网连接。
// 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
// Evening Golden Hour Start Date
mySun.eveningGoldenHourStart
// Evening Golden Hour End Date
mySun.eveningGoldenHourEnd
// To know all the information you can retrieve go to the **Features** section.
要正确显示太阳日期事件,请使用以下 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))")
看看 SunKit 的精神兄弟:MoonKit。