haraldh / SunCalc

Garmin Connect IQ Widget to calculate Dusk, Dawn, Sunset, Sunrise, Blue Hour, etc.
https://apps.garmin.com/en-US/apps/87b86650-a443-43ea-9dcb-29e4051a5722
GNU Lesser General Public License v2.1
34 stars 3 forks source link

Calculations for SUNSET, DUSK, NAUTIC_DUSK and ASTRO_DUSK produce identical results #7

Open evilwombat opened 8 months ago

evilwombat commented 8 months ago

Consider the following code:

function testSunsets() {
    var sc = new SunCalc();
    var location = [39.0, -77.0];
    var now = Time.now();
    location[0] = location[0] * 3.14159 / 180.0;
    location[1] = location[1] * 3.14159 / 180.0;
    System.println("SUNSET_START = " + sc.printMoment(sc.calculate(now, location, SUNSET_START)));
    System.println("SUNSET       = " + sc.printMoment(sc.calculate(now, location, SUNSET)));
    System.println("BLUE_HOUR_PM = " + sc.printMoment(sc.calculate(now, location, BLUE_HOUR_PM)));
    System.println("DUSK         = " + sc.printMoment(sc.calculate(now, location, DUSK)));
    System.println("NAUTIC_DUSK  = " + sc.printMoment(sc.calculate(now, location, NAUTIC_DUSK)));
    System.println("ASTRO_DUSK   = " + sc.printMoment(sc.calculate(now, location, ASTRO_DUSK)));
  }

The output I see is as follows:

SUNSET_START = 24.12.2023 19:00:00
SUNSET       = 24.12.2023 19:00:00
BLUE_HOUR_PM = 24.12.2023 19:00:00
DUSK         = 24.12.2023 19:00:00
NAUTIC_DUSK  = 24.12.2023 19:00:00
ASTRO_DUSK   = 24.12.2023 19:00:00

Am I being completely stupid, or are the various sunset types really returning the same moment, despite such seemingly large differences in sun angle?

evilwombat commented 8 months ago

Seemingly getting more reasonable values if I explicitly convert the input location to double-precision:

    function calculate(moment, pos, what) {
        var lat = pos[0].toDouble();
        var lng = pos[1].toDouble();

Either way, THANK YOU for the library!