Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor common portions of conversion algorithms #26

Merged
merged 45 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
815eec7
Refactor common portions of conversion algorithms
sbooth Nov 14, 2023
a3f1c00
Refactor Gregorian-type intercalation
sbooth Nov 15, 2023
30bc28e
Refactor
sbooth Nov 15, 2023
84c54b7
Fix punctuation
sbooth Nov 15, 2023
1d618ac
Further refactoring
sbooth Nov 15, 2023
2eb6fce
Consolidate source files
sbooth Nov 15, 2023
90dfbc5
More consolidation
sbooth Nov 15, 2023
36f9a12
Add documentation
sbooth Nov 15, 2023
65bae4a
Rename JDN.swift
sbooth Nov 15, 2023
0eea744
Consolidate JD into JulianDayNumberConverting
sbooth Nov 15, 2023
8a613eb
Remove unneeded intercalating cycle
sbooth Nov 15, 2023
decc929
Use conversion params already present
sbooth Nov 15, 2023
11e3a78
Use struct for algorithm constants
sbooth Nov 15, 2023
4168bb6
Add intercalating cycle struct
sbooth Nov 15, 2023
d459ddb
Refactor Gregorian-type intercalating
sbooth Nov 15, 2023
4484bc0
Add precondition
sbooth Nov 15, 2023
56896c4
New arithmetic limits
sbooth Nov 15, 2023
e69dffa
Adjust arithmetic limits
sbooth Nov 15, 2023
d1f4093
Adjust arithmetic limits
sbooth Nov 15, 2023
83f9187
Adjust arithmetic limits
sbooth Nov 15, 2023
ff4388a
Adjust arithmetic limits
sbooth Nov 15, 2023
5fcd6ee
Adjust arithmetic limits
sbooth Nov 15, 2023
1d8bb38
Adjust arithmetic limits
sbooth Nov 15, 2023
18eecc8
Adjust arithmetic limits
sbooth Nov 15, 2023
b794da4
Adjust arithmetic limits
sbooth Nov 15, 2023
5d12be3
Adjust arithmetic limit
sbooth Nov 15, 2023
14ca6e1
Adjust arithmetic limits
sbooth Nov 15, 2023
b5502bd
Update arithmetic limits
sbooth Nov 15, 2023
a19ab68
Update arithmetic limits
sbooth Nov 15, 2023
fc5bcf2
Update limits
sbooth Nov 15, 2023
1ca03b1
Add code voice
sbooth Nov 15, 2023
96f80a1
Add grouping
sbooth Nov 15, 2023
08e6bfb
Add grouping
sbooth Nov 15, 2023
a429e7f
Revert "Add grouping"
sbooth Nov 15, 2023
d631a82
Revert "Add grouping"
sbooth Nov 15, 2023
fc621c6
Move parameters declarations
sbooth Nov 15, 2023
c222b54
Refactor algorithms
sbooth Nov 16, 2023
7860a61
Remove extraneous comment
sbooth Nov 16, 2023
45643dd
Expand comment
sbooth Nov 16, 2023
10c5cec
Minor edits
sbooth Nov 16, 2023
9f65808
Reword description
sbooth Nov 16, 2023
b368288
Add more calendar information
sbooth Nov 16, 2023
9765887
Fix table
sbooth Nov 16, 2023
8001a85
Remove descriptions
sbooth Nov 16, 2023
8c3eed0
Rename converters
sbooth Nov 16, 2023
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
95 changes: 36 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fsbooth%2FJulianDayNumber%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/sbooth/JulianDayNumber)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fsbooth%2FJulianDayNumber%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/sbooth/JulianDayNumber)

Julian day number (JDN) and Julian date (JD) calculations supporting the following calendars:
- [Armenian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/armeniancalendar)
- [Astronomical](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/astronomicalcalendar)
- [Baháʼí](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/bahaicalendar)
- [Coptic](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/copticcalendar)
- [Egyptian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/egyptiancalendar)
- [Ethiopian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/ethiopiancalendar)
- [French Republican](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/frenchrepublicancalendar)
- [Gregorian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/gregoriancalendar)
- [Hebrew](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/hebrewcalendar)
- [Islamic](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/islamiccalendar)
- [Julian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/juliancalendar)
- [Khwarizmian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/khwarizmiancalendar)
- [Maya](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/mayacalendar)
- [Śaka](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/sakacalendar)

Most of the JDN conversion algorithms are adapted from Richards, E.G. 2012, "[Calendars](https://aa.usno.navy.mil/downloads/c15_usb_online.pdf)," from the *Explanatory Supplement to the Astronomical Almanac, 3rd edition*, S.E Urban and P.K. Seidelmann eds., (Mill Valley, CA: University Science Books), Chapter 15, pp. 585-624.

The JDN algorithms use integer math to avoid rounding errors and the implementations have been round-trip tested for all valid Julian day numbers in the years -999,999 to +999,999.
Julian day number and Julian date calculations supporting the following calendars:

| Calendar | Epoch ¹ |
| --- | --- |
| [Armenian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/armeniancalendar) | July 11, 552 CE |
| [Astronomical](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/astronomicalcalendar) | January 1, 1 CE |
| [Baháʼí](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/bahaicalendar) | March 21, 1844 |
| [Coptic](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/copticcalendar) | August 29, 284 CE | year. |
| [Egyptian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/egyptiancalendar) | February 26, 747 BCE |
| [Ethiopian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/ethiopiancalendar) | August 29, 8 CE |
| [French Republican](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/frenchrepublicancalendar) | September 22, 1792 |
| [Gregorian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/gregoriancalendar) | January 1, 1 CE |
| [Hebrew](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/hebrewcalendar) | October 7, 3761 BCE |
| [Islamic](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/islamiccalendar) | July 16, 622 CE |
| [Julian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/juliancalendar) | January 1, 1 CE |
| [Khwarizmian](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/khwarizmiancalendar) | June 21, 632 CE |
| [Maya](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/mayacalendar) | September 6, 3114 BCE |
| [Śaka](https://swiftpackageindex.com/sbooth/juliandaynumber/main/documentation/juliandaynumber/sakacalendar) | March 24, 79 CE |

¹ Epochs given in Julian calendar

The Julian day number interconverting algorithms use integer math to avoid rounding errors and the implementations have been round-trip tested for all valid Julian day numbers in the years -999,999 to +999,999.

## Installation

Expand Down Expand Up @@ -67,49 +70,23 @@ The [latest documentation](https://swiftpackageindex.com/sbooth/JulianDayNumber/

## Limits

### Julian Day Numbers

The following table summarizes the limits for Julian day numbers. Julian day numbers outside these values will cause an arithmetic overflow.
The following table summarizes the arithmetic limits for Julian day number calculations.

| Calendar | Minimum JDN | Maximum JDN |
| --- | --- | --- |
| Armenian | -9223372036854775514 | 9223372036854775490 |
| Baháʼí | -9223372036854719351 | 2305795661307959248 |
| Coptic | -9223372036854775664 | 2305843009213693827 |
| Egyptian | -9223372036854775514 | 9223372036854775760 |
| Ethiopian | -9223372036854775664 | 2305843009213693827 |
| French Republican | -9223372036854719351 | 2305795661307960548 |
| Gregorian | -9223372036854719351 | 2305795661307959247 |
| Hebrew | -9223372036747815981 ¹ | 355839970905570 |
| Islamic | -9223372036854775352 | 307445734561818195 |
| Julian | -9223372036854775664 | 2305843009213692550 |
| Khwarizmian | -9223372036854775514 | 9223372036854775490 |
| Maya Long Count | -9223372036854191525 ² | Int.max |
| Śaka | -9223372036854719351 | 2305795661307959298 |

¹ The smallest round-trippable JDN for the Hebrew calendar is -9223372036747815627.
² The smallest round-trippable JDN for the Maya Long Count is -9223372036854191517.

### Julian Dates

The following table summarizes the limits for Julian dates. Julian dates outside these values will cause an arithmetic overflow.

| Calendar | Minimum JD | Maximum JD |
| --- | --- | --- |
| Armenian | -0x1.fffffffffffffp+62 | 0x1.fffffffffffffp+62 |
| Baháʼí | -0x1.fffffffffffc8p+62 | 0x1.fffd4eff4e5d7p+60 |
| Coptic | -0x1.fffffffffffffp+62 | 0x1.fffffffffffffp+60 |
| Egyptian | -0x1.fffffffffffffp+62 | 0x1.fffffffffffffp+62 |
| Ethiopian | -0x1.fffffffffffffp+62 | 0x1.fffffffffffffp+60 |
| French Republican | -0x1.fffffffffffc8p+62 | 0x1.fffd4eff4e5dcp+60 |
| Gregorian | -0x1.fffffffffffc8p+62 | 0x1.fffd4eff4e5d7p+60 |
| Hebrew | -0x1.ffffffffe67fbp+62 ¹ | 0x1.43a273100de27p+48 |
| Islamic | -0x1.fffffffffffffp+62 | 0x1.1111111111099p+58 |
| Julian | -0x1.fffffffffffffp+62 | 0x1.ffffffffffffap+60 |
| Khwarizmian | -0x1.fffffffffffffp+62 | 0x1.fffffffffffffp+62 |
| Śaka | -0x1.fffffffffffc8p+62 | 0x1.fffd4eff4e5d8p+60 |

¹ The smallest round-trippable JD for the Hebrew calendar is -0x1.ffffffffe67fap+62.
| Armenian | `Int.min` + 341 | `Int.max` - 317 |
| Baháʼí | `Int.min` + 56457 | 2305795661307959248 |
| Coptic | `Int.min` + 384 | 2305843009213693827 |
| Egyptian | `Int.min` + 611 | `Int.max` - 47 |
| Ethiopian | `Int.min` + 384 | 2305843009213693827 |
| French Republican | `Int.min` + 56759 | 2305795661307960548 |
| Gregorian | `Int.min` + 56457 | 2305795661307959247 |
| Hebrew | `Int.min` + 106960181 | 355839970905570 |
| Islamic | `Int.min` + 325 | 307445734561818195 |
| Julian | `Int.min` + 144 | 2305843009213692550 |
| Khwarizmian | `Int.min` + 341 | `Int.max` - 317 |
| Maya Long Count | `Int.min` + 584291 | `Int.max` |
| Śaka | `Int.min` + 56457 | 2305795661307959298 |

## License

Expand Down
84 changes: 0 additions & 84 deletions Sources/JulianDayNumber/ArmenianCalendar+JDN.swift

This file was deleted.

16 changes: 16 additions & 0 deletions Sources/JulianDayNumber/ArmenianCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,19 @@ public struct ArmenianCalendar {
return monthLengths[M - 1]
}
}

extension ArmenianCalendar: JulianDayNumberConverting {
/// A date in the Armenian calendar consists of a year, month, and day.
public typealias DateType = (year: Year, month: Month, day: Day)

/// The converter for the Armenian calendar.
static let converter = JDNConverter(y: 5268, j: 317, m: 0, n: 13, r: 1, p: 365, q: 0, v: 0, u: 1, s: 30, t: 0, w: 0)

public static func julianDayNumberFromDate(_ date: DateType) -> JulianDayNumber {
converter.julianDayNumberFromDate(date)
}

public static func dateFromJulianDayNumber(_ J: JulianDayNumber) -> DateType {
converter.dateFromJulianDayNumber(J)
}
}
36 changes: 0 additions & 36 deletions Sources/JulianDayNumber/AstronomicalCalendar+JDN.swift

This file was deleted.

29 changes: 29 additions & 0 deletions Sources/JulianDayNumber/AstronomicalCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,32 @@ public struct AstronomicalCalendar {
Y < gregorianCalendarEffectiveDate.year ? JulianCalendar.easter(year: Y) : GregorianCalendar.easter(year: Y)
}
}

extension AstronomicalCalendar: JulianDayNumberConverting {
/// A date in the astronomical calendar consists of a year, month, and day in either the Julian or Gregorian calendar.
public typealias DateType = JulianCalendar.DateType

/// Converts a date in the astromical calendar to a Julian day number.
///
/// Dates before October 15, 1582 are treated as dates in the Julian calendar while later dates are treated as dates in the Gregorian calendar.
///
/// - important: No validation checks are performed on the date values.
///
/// - parameter date: A date to convert.
///
/// - returns: The Julian day number corresponding to the specified date.
public static func julianDayNumberFromDate(_ date: DateType) -> JulianDayNumber {
date < gregorianCalendarEffectiveDate ? JulianCalendar.julianDayNumberFromDate(date) : GregorianCalendar.julianDayNumberFromDate(date)
}

/// Converts a Julian day number to a date in the astromical calendar.
///
/// Julian day numbers less than 2299161 treated as dates in the Julian calendar while equal or larger Julian day numbers are treated as dates in the Gregorian calendar.
///
/// - parameter J: A Julian day number.
///
/// - returns: The date corresponding to the specified Julian day number.
public static func dateFromJulianDayNumber(_ J: JulianDayNumber) -> DateType {
J < GregorianCalendar.effectiveJulianDayNumber ? JulianCalendar.dateFromJulianDayNumber(J): GregorianCalendar.dateFromJulianDayNumber(J)
}
}
91 changes: 0 additions & 91 deletions Sources/JulianDayNumber/BahaiCalendar+JDN.swift

This file was deleted.

16 changes: 16 additions & 0 deletions Sources/JulianDayNumber/BahaiCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,19 @@ public struct BahaiCalendar {
}
}
}

extension BahaiCalendar: JulianDayNumberConverting {
/// A date in the Baháʼí calendar consists of a year, month, and day.
public typealias DateType = (year: Year, month: Month, day: Day)

/// The converter for the Baháʼí calendar.
static let converter = JDNGregorianTypeConverter(y: 6560, j: 1412, m: 19, n: 20, r: 4, p: 1461, q: 0, v: 3, u: 1, s: 19, t: 0, w: 0, A: 184, B: 274273, C: -50)

public static func julianDayNumberFromDate(_ date: DateType) -> JulianDayNumber {
converter.julianDayNumberFromDate(date)
}

public static func dateFromJulianDayNumber(_ J: JulianDayNumber) -> DateType {
converter.dateFromJulianDayNumber(J)
}
}
Loading