onekiloparsec / SwiftAA

The most comprehensive collection of accurate astronomical algorithms in (C++, Objective-C and) Swift.
http://www.onekiloparsec.dev/
MIT License
171 stars 31 forks source link

Rise/Set date difference by one day? #97

Closed vincentneo closed 3 years ago

vincentneo commented 3 years ago

Sorry to bother you again, but here's something I don't quite get. If I were to use the Paris coordinates from the example you gave, on its local time, rise, transit and set dates are of the same day.

However, if I were to try at Singapore or San Francisco coordinates as some examples, on their local time, either the rise or set date is off by one day. Is that supposed to be normal?

I opened this as a separate issue as it's not quite related with the previous one, I feel.

Here's the code I've tried:

    let jd = JulianDay(year: 2020, month: 09, day: 08)
    let paris = GeographicCoordinates(positivelyWestwardLongitude: Degree(.minus, 2, 21, 07), latitude: Degree(.plus, 48, 51, 24))
    let times = Earth(julianDay: jd).twilights(forSunAltitude: 0, coordinates: paris)

    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    formatter.timeZone = TimeZone(abbreviation: "UTC+2")
    print(formatter.string(from: times.riseTime!.date))         // 2020-09-08T07:21:41+0200
    print(formatter.string(from: times.transitTime!.date))      // 2020-09-08T13:46:57+0200
    print(formatter.string(from: times.setTime!.date))          // 2020-09-08T20:11:16+0200

    formatter.timeZone = TimeZone(abbreviation: "UTC+8")
    let singapore = GeographicCoordinates(CLLocation(latitude: 1.290, longitude: 103.852))
    let timesSG = Earth(julianDay: jd).twilights(forSunAltitude: 0, coordinates: singapore)
    print(formatter.string(from: timesSG.riseTime!.date))       // 2020-09-09T07:00:19+0800
    print(formatter.string(from: timesSG.transitTime!.date))    // 2020-09-08T13:01:03+0800
    print(formatter.string(from: timesSG.setTime!.date))        // 2020-09-08T19:01:27+0800

    formatter.timeZone = TimeZone(abbreviation: "UTC-7")
    let sf = GeographicCoordinates(CLLocation(latitude: 37.7740, longitude: -122.4313))
    let timesSF = Earth(julianDay: jd).twilights(forSunAltitude: 0, coordinates: sf)
    print(formatter.string(from: timesSF.riseTime!.date))       // 2020-09-08T06:49:19-0700
    print(formatter.string(from: timesSF.transitTime!.date))    // 2020-09-08T13:05:58-0700
    print(formatter.string(from: timesSF.setTime!.date))        // 2020-09-07T19:23:31-0700
onekiloparsec commented 3 years ago

No bothering at all, things get improved when serious users ask questions. I'll have a look.

vincentneo commented 3 years ago

Ignore my previous comment (deleted). Turns out I did a static JulianDay somewhere else and the code I was playing with was using that while I thought the one using Date() was the issue...

Guess its too late at night to work with codes 😅