Skip to content

Commit ba36242

Browse files
Merge pull request #136 from isenberg/fixed_results_before_year_1
Fixed results for solstice, equinox and lengthOfSeason calculation for Earth before Gregorian year 1BC (epoch 0)
2 parents add1a71 + 1c85998 commit ba36242

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

Sources/SwiftAA/Earth.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class Earth: Object, PlanetaryBase, PlanetaryOrbits {
7474
- returns: A julian day of the TT time of the equinox.
7575
*/
7676
public func equinox(of equinoxType: EarthEquinoxType) -> JulianDay {
77-
let year = self.julianDay.date.year
77+
let year = self.julianDay.year
7878
switch equinoxType {
7979
case .northwardSpring:
8080
return JulianDay(KPCAAEquinoxesAndSolstices_NorthwardEquinox(year, self.highPrecision))
@@ -92,7 +92,7 @@ public class Earth: Object, PlanetaryBase, PlanetaryOrbits {
9292
- returns: A julian day of the TT time of the solstice.
9393
*/
9494
public func solstice(of solsticeType: EarthSolsticeType) -> JulianDay {
95-
let year = self.julianDay.date.year
95+
let year = self.julianDay.year
9696
switch solsticeType {
9797
case .northernSummer:
9898
return JulianDay(KPCAAEquinoxesAndSolstices_NorthernSolstice(year, self.highPrecision))
@@ -110,7 +110,7 @@ public class Earth: Object, PlanetaryBase, PlanetaryOrbits {
110110
- returns: A length in (Julian) Days.
111111
*/
112112
public func lengthOfSeason(_ season: Season, northernHemisphere: Bool) -> Day {
113-
let year = self.julianDay.date.year
113+
let year = self.julianDay.year
114114
switch season {
115115
case .spring:
116116
return Day(KPCAAEquinoxesAndSolstices_LengthOfSpring(year, northernHemisphere, self.highPrecision))

Sources/SwiftAA/JulianDay.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public extension JulianDay {
6666
let decimalSeconds = KPCAADate_GetSecond(aaDate)
6767
let roundedSeconds = decimalSeconds.rounded(.towardZero)
6868
let nanoseconds = (decimalSeconds - roundedSeconds) * 1e9
69-
let components = DateComponents(year: KPCAADate_GetYear(aaDate),
69+
let aaYear = KPCAADate_GetYear(aaDate)
70+
// the aaYear sequence is -2, -1, 0, 1, 2 while the Gregorian years are counted era0 2, era0 1, era1 1, era1 2
71+
let components = DateComponents(era: aaYear > 0 ? 1 : 0,
72+
year: aaYear > 0 ? aaYear : abs(1 - aaYear),
7073
month: KPCAADate_GetMonth(aaDate),
7174
day: KPCAADate_GetDay(aaDate),
7275
hour: KPCAADate_GetHour(aaDate),
@@ -79,6 +82,20 @@ public extension JulianDay {
7982
return date
8083
}
8184

85+
/// Returns the astronomical year count of this Julian Day value. The year before 1 is returned as 0, the year before 0 returned as -1.
86+
/// This astronomical counting differs from the Gregorian year where the year before year 1 is 1BC.
87+
/// The reason to offer this separate attribute is the difficulty of extracting the year before 1 from a Swift Date object
88+
/// and before the year equivalent to JulianDay 0, Swift Date always returns era 0 year 4712
89+
/// and many SwiftAA functions use an intermediate conversion from a JulianDay to a Gregorian year, and will fail when used on a Swift Date object for years before 1
90+
/// and they expect a year 0 and not the 0-skip of the Gregorian calendar.
91+
/// This intermediate use of the Gregorian calendar in general instead of Julian Day is risky and most likely slow, but changing would require a lot of work.
92+
var year: Int {
93+
let aaDate = KPCAADate_CreateWithJulianDay(value, true)
94+
let aaYear = KPCAADate_GetYear(aaDate)
95+
KPCAADate_Destroy(aaDate)
96+
return aaYear
97+
}
98+
8299
/// Returns the so-called Modified Julian Day corresponding to the Julian Day value.
83100
/// Contrary to the JD, the Modified Julian Day begins at Greenwhich mean midnight.
84101
/// It is equal to JD - 2400 000.5

0 commit comments

Comments
 (0)