Solar icon indicating copy to clipboard operation
Solar copied to clipboard

Sunrise and Sunset dates off by 1 day.

Open neilt opened this issue 6 years ago • 3 comments

It seems that the dates are 1 day in the future if the local time is the next day UTC.

Not sure if this is the same problem as reported in #24?

Here is a complete test. Note that the reported times are one day in the future from the correct times.

    ///    Sun and Moon Data for One Day
    ///
    ///    U.S. Naval Observatory
    ///    Astronomical Applications Department
    ///
    ///    (Longitude W 94° 0', Latitude N 39° 0')
    ///
    ///    Friday, May 3, 2019    Universal Time - 5h
    ///    Sun
    ///    Begin civil twilight    05:47
    ///    Sunrise    06:16
    ///    Sun transit    13:13
    ///    Sunset    20:11
    ///    End civil twilight    20:40
    ///    Moon
    ///    Moonrise    05:56
    ///    Moon transit    12:24
    ///    Moonset    19:00
    ///
    /// [https://aa.usno.navy.mil/data/docs/RS_OneDay.php](https://aa.usno.navy.mil/data/docs/RS_OneDay.php)
    func testPlainLatLon() {
        var tdf: DateFormatter = {
            let df = DateFormatter()
            df.dateFormat = "MM/dd/yyyy HH:mm zzz"
            return df
        }()
        let testCriticalEvening = tdf.date(from: "05/03/2019 19:20 CDT")!
        let testLocation: CLLocation = CLLocation(latitude: 39.00, longitude: -94.0)
        let expectedSunrise = "05/03/2019 06:16 CDT"
        let expectedSunset = "05/03/2019 20:11 CDT"
        let solar = Solar(for: testCriticalEvening, coordinate: testLocation.coordinate)

        XCTAssert(tdf.string(from: solar!.sunrise!) == expectedSunrise,
                  "Expected: \(expectedSunrise), Received: \(tdf.string(from: solar!.sunrise!))")
        XCTAssert(tdf.string(from: solar!.sunset!) == expectedSunset,
                  "Expected: \(expectedSunset), Received: \(tdf.string(from: solar!.sunset!))")
    }

The results of the test are:

UI Test Activity: 
Assertion Failure: Solar_iOSTests.swift:198: XCTAssertTrue failed - Expected: 05/03/2019 06:16 CDT, Received: 05/04/2019 06:14 CDT

UI Test Activity: 
Assertion Failure: Solar_iOSTests.swift:200: XCTAssertTrue failed - Expected: 05/03/2019 20:11 CDT, Received: 05/04/2019 20:11 CDT

neilt avatar May 03 '19 19:05 neilt

I am also having this same issue

wbrannock avatar Jun 16 '19 00:06 wbrannock

Yep, I ended up not using this lib because the API is IMO wrong to determine accurate local sunrise and sunset. The API needs to know the original time zone which corresponds to the lat and lon. This is not available using just a Date object.

I ended up converting http://stjarnhimlen.se/comp/sunriset.c to swift. It works like a charm.

I just added the converted code here https://gist.github.com/neilt/6bc284ac7ce00d566002bc45bc0d86dd

neilt avatar Jun 16 '19 16:06 neilt

Any updates on this issue? It seems like @aaronfreimark's solution also has a bug and was not merged.

Was this issue ever fixed?

dasmer avatar Feb 26 '22 16:02 dasmer