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

Add timezone support #26

Merged
merged 7 commits into from
Jul 9, 2024
Merged

Add timezone support #26

merged 7 commits into from
Jul 9, 2024

Conversation

mannylopez
Copy link
Owner

@mannylopez mannylopez commented Jun 28, 2024

Closes #21 and closes #28

Adds timezone support to TinyMoon and fixes a major flaw within the library.

Timezones are needed to calculate an exact time. Before, everything was calculated off of the current user's system timezone, leading to incorrect results.

I added extensive timezone tests: f4773e5#diff-86442e8ab0a16ee48438bbc6f15bed110625d172a752c18b258604cf840ff9d8

Now that I am using a Date with timezones, I can remove a lot of the Julian Day usage. I've updated function signatures, doccumentation, and tests to reflect this.

Other updates:
Fixed a bug where minorMoonPhase was returning a major moon phase

    static func minorMoonPhase(phaseFraction: Double) -> MoonPhase {
--      if phaseFraction < 0.23 {
--        .waxingCrescent
--      } else if phaseFraction < 0.48 {
--        .waxingGibbous
--      } else if phaseFraction < 0.73 {
--        .waningGibbous
--      } else if phaseFraction < 0.98 {
--        .waningCrescent
++      if phaseFraction < 0.25 {
++        return .waxingCrescent
++      } else if phaseFraction < 0.50 {
++        return .waxingGibbous
++      } else if phaseFraction < 0.75 {
++        return .waningGibbous
      } else {
--        .newMoon
++        return .waningCrescent
      }
    }

Refactor julianStartAndEndOfDay to take in a Date instead of a Julian Day

-- static func startAndEndOfJulianDay(julianDay: Double) -> (start: Double, end: Double) {
--       let base = floor(julianDay) + (julianDay.truncatingRemainder(dividingBy: 1) < 0.5 ? -1 : 0)
--       let arr = [0.5, 1.4993].map { base + $0 }
--       return (start: arr[0], end: arr[1])
++ static func julianStartAndEndOfDay(date: Date, timeZone: TimeZone = TimeZone.current) -> (start: Double, end: Double) {
++      var calendar = Calendar.current
++      calendar.timeZone = timeZone
++      let startOfDay = calendar.startOfDay(for: date)
++      let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)
++      let startJulianDay = AstronomicalConstant.julianDay(startOfDay)
++      let endJulianDay = AstronomicalConstant.julianDay(endOfDay!)
++      return (start: startJulianDay, end: endJulianDay)
    }

@mannylopez mannylopez marked this pull request as ready for review July 9, 2024 01:16
@mannylopez mannylopez changed the title [First pass] Add timezone support Add timezone support Jul 9, 2024
@mannylopez mannylopez merged commit 39a9eee into main Jul 9, 2024
@mannylopez mannylopez deleted the ml--timezone-support branch July 25, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Refactor] Remove JulianDate Account for time zones
1 participant