Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.0
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 32 additions & 14 deletions Daylight.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
25455E7D2BEA51F500CABDF3 /* Daylight */ = {
isa = PBXGroup;
children = (
OBJ_9 /* Daylight.swift */,
5B402B651E9B29A20079BE20 /* DaylightUtilities.swift */,
5B402B671E9B29FF0079BE20 /* DaylightAstronomy.swift */,
);
path = Daylight;
sourceTree = "<group>";
};
OBJ_10 /* Tests */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -92,22 +102,11 @@
OBJ_7 /* Sources */ = {
isa = PBXGroup;
children = (
OBJ_8 /* Daylight */,
25455E7D2BEA51F500CABDF3 /* Daylight */,
);
path = Sources;
sourceTree = "<group>";
};
OBJ_8 /* Daylight */ = {
isa = PBXGroup;
children = (
OBJ_9 /* Daylight.swift */,
5B402B651E9B29A20079BE20 /* DaylightUtilities.swift */,
5B402B671E9B29FF0079BE20 /* DaylightAstronomy.swift */,
);
name = Daylight;
path = Sources;
sourceTree = SOURCE_ROOT;
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -158,6 +157,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
);
mainGroup = OBJ_5;
Expand Down Expand Up @@ -228,13 +228,20 @@
);
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = Daylight.xcodeproj/Daylight_Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACOSX_DEPLOYMENT_TARGET = 11.0;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Daylight;
PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SUPPORTED_PLATFORMS = "appletvos appletvsimulator iphoneos iphonesimulator macosx watchos watchsimulator";
SUPPORTS_MACCATALYST = NO;
TARGETED_DEVICE_FAMILY = "1,2";
TARGET_NAME = Daylight;
TVOS_DEPLOYMENT_TARGET = 16.0;
WATCHOS_DEPLOYMENT_TARGET = 9.0;
};
name = Debug;
};
Expand All @@ -248,13 +255,20 @@
);
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = Daylight.xcodeproj/Daylight_Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACOSX_DEPLOYMENT_TARGET = 11.0;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Daylight;
PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SUPPORTED_PLATFORMS = "appletvos appletvsimulator iphoneos iphonesimulator macosx watchos watchsimulator";
SUPPORTS_MACCATALYST = NO;
TARGETED_DEVICE_FAMILY = "1,2";
TARGET_NAME = Daylight;
TVOS_DEPLOYMENT_TARGET = 16.0;
WATCHOS_DEPLOYMENT_TARGET = 9.0;
};
name = Release;
};
Expand Down Expand Up @@ -297,10 +311,12 @@
buildSettings = {
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 0.1.0;
DEBUG_INFORMATION_FORMAT = dwarf;
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_NS_ASSERTIONS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MACOSX_DEPLOYMENT_TARGET = 10.10;
ONLY_ACTIVE_ARCH = YES;
OTHER_SWIFT_FLAGS = "-DXcode";
Expand All @@ -309,7 +325,7 @@
SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = SWIFT_PACKAGE;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
USE_HEADERMAP = NO;
};
name = Debug;
Expand All @@ -319,17 +335,19 @@
buildSettings = {
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = YES;
CURRENT_PROJECT_VERSION = 0.1.0;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DYLIB_INSTALL_NAME_BASE = "@rpath";
GCC_OPTIMIZATION_LEVEL = s;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MACOSX_DEPLOYMENT_TARGET = 10.10;
OTHER_SWIFT_FLAGS = "-DXcode";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = SWIFT_PACKAGE;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
USE_HEADERMAP = NO;
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
TargetAttributes = {
5B7EA00A1EA84DC900B58F3A = {
CreatedOnToolsVersion = 8.2.1;
LastSwiftMigration = 0920;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -339,7 +340,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.emergencestudios.DaylightExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -353,7 +354,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.emergencestudios.DaylightExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -376,6 +377,7 @@
5B7EA01F1EA84DC900B58F3A /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
20 changes: 10 additions & 10 deletions Example/DaylightExample/DaylightExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class ViewController: UIViewController {
let date = Date()
let loc = Location(tz: timeZone, coords: location.coordinate)

let sunrise = date.timeOf(.sunrise, at: loc)
let noon = date.timeOf(.noon, at: loc)
let sunset = date.timeOf(.sunset, at: loc)
let nextSunrise = date.timeOfNext(.sunrise, at: loc)
let nextSunset = date.timeOfNext(.sunset, at: loc)
let sunrise = try? date.timeOf(.sunrise, at: loc)
let noon = try? date.timeOf(.noon, at: loc)
let sunset = try? date.timeOf(.sunset, at: loc)
let nextSunrise = try? date.timeOfNext(.sunrise, at: loc)
let nextSunset = try? date.timeOfNext(.sunset, at: loc)

let numberFormatter = NumberFormatter()
numberFormatter.maximumFractionDigits = 3
Expand All @@ -58,11 +58,11 @@ class ViewController: UIViewController {
let labelStrings = [
"Lon: \(latString)º Lat: \(lonString)º",
"Timezone: \(timeZone.identifier)",
"Sunrise: \(dateFormatter.string(from: sunrise))",
"Solar Noon: \(dateFormatter.string(from: noon))",
"Sunset: \(dateFormatter.string(from: sunset))",
"Next Sunrise: \(dateFormatter.string(from: nextSunrise))",
"Next Sunset: \(dateFormatter.string(from: nextSunset))"
"Sunrise: \(dateFormatter.string(from: sunrise!))",
"Solar Noon: \(dateFormatter.string(from: noon!))",
"Sunset: \(dateFormatter.string(from: sunset!))",
"Next Sunrise: \(dateFormatter.string(from: nextSunrise!))",
"Next Sunset: \(dateFormatter.string(from: nextSunset!))"
]

for view in stackView.subviews {
Expand Down
11 changes: 9 additions & 2 deletions Example/DaylightExample/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Daylight"
name: "Daylight",
platforms: [.iOS(.v13), .macOS(.v10_15)],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Daylight",
targets: ["Daylight"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Daylight",
dependencies: []),
],
swiftLanguageVersions: [.v5]
)
46 changes: 25 additions & 21 deletions Sources/Daylight.swift → Sources/Daylight/Daylight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import Foundation
import CoreLocation

public enum SolarEventError: Error {
case neverRise
case neverSet
}

public enum SolarEvent {
case sunrise
case noon
Expand Down Expand Up @@ -59,69 +64,68 @@ public struct Day {
self.day = day
}

public func timeOf(_ solarEvent: SolarEvent, at location: Location) -> Date {
public func timeOf(_ solarEvent: SolarEvent, at location: Location) throws -> Date {
let components = DateComponents(year: year, month: month, day: day)
let calendar = Calendar.with(tz: location.tz)
let inputDate = calendar.date(from: components)!
return inputDate.timeOf(solarEvent, at: location)
return try inputDate.timeOf(solarEvent, at: location)
}
}

public extension Date {

public func timeOf(_ solarEvent: SolarEvent, at location: Location) -> Date {
func timeOf(_ solarEvent: SolarEvent, at location: Location) throws -> Date {
switch solarEvent {
case .sunrise, .civilDawn, .nauticalDawn, .astronomicalDawn:
return calculateDawn(location: location.coords,
solarElevation: solarEvent.elevation.rawValue,
timezone: location.tz)
return try calculateDawn(location: location.coords,
solarElevation: solarEvent.elevation.rawValue,
timezone: location.tz)
case .sunset, .civilDusk, .nauticalDusk, .astronomicalDusk:
return calculateDusk(location: location.coords,
solarElevation: solarEvent.elevation.rawValue,
timezone: location.tz)
return try calculateDusk(location: location.coords,
solarElevation: solarEvent.elevation.rawValue,
timezone: location.tz)
case .noon:
return calculateNoon(location: location.coords,
return try calculateNoon(location: location.coords,
timezone: location.tz)
}
}

public func timeOfNext(_ solarEvent: SolarEvent, at location: Location) -> Date {
var time = self.timeOf(solarEvent, at: location)
func timeOfNext(_ solarEvent: SolarEvent, at location: Location) throws -> Date {
var time = try timeOf(solarEvent, at: location)
if self >= time {
time = self.dayAfter.timeOf(solarEvent, at: location)
time = try dayAfter.timeOf(solarEvent, at: location)
}
return time
}

private func calculateDawn(location: CLLocationCoordinate2D,
solarElevation: Float64,
timezone: TimeZone) -> Date {
timezone: TimeZone) throws -> Date {

let midnight = self.atMidnight(timeZone: timezone)
let julianDate = midnight.julianTz(timezone)
let sunriseUTCMins = julianDate.sunriseUTC(location: location, solarElevation: solarElevation)
let sunriseUTCMins = try julianDate.sunriseUTC(location: location, solarElevation: solarElevation)

return self.dateFromTimeUTC(timeUTCMins: sunriseUTCMins, timezone: timezone)
}

private func calculateDusk(location: CLLocationCoordinate2D,
solarElevation: Float64,
timezone: TimeZone) -> Date {
timezone: TimeZone) throws -> Date {

let midnight = self.atMidnight(timeZone: timezone)
let julianDate = midnight.julianTz(timezone)
let duskUTCMins = julianDate.sunsetUTC(location: location, solarElevation: solarElevation)
let duskUTCMins = try julianDate.sunsetUTC(location: location, solarElevation: solarElevation)

return self.dateFromTimeUTC(timeUTCMins: duskUTCMins, timezone: timezone)
}

private func calculateNoon(location: CLLocationCoordinate2D,
timezone: TimeZone) -> Date {

let sunrise = calculateDawn(location: location,
timezone: TimeZone) throws -> Date {
let sunrise = try calculateDawn(location: location,
solarElevation: SolarEvent.sunrise.elevation.rawValue,
timezone: timezone)
let sunset = calculateDusk(location: location,
let sunset = try calculateDusk(location: location,
solarElevation: SolarEvent.sunset.elevation.rawValue,
timezone: timezone)
let dayLength = sunset.timeIntervalSince(sunrise)
Expand Down
Loading