mikereedell / sunrisesunsetlib-java

Library for computing the sunrise/sunset from GPS coordinates and a date, in Java.
http://mikereedell.github.com/sunrisesunsetlib-java/
Apache License 2.0
299 stars 70 forks source link

Incorrect sunrise time #14

Open JCDubs opened 8 years ago

JCDubs commented 8 years ago

Hi Mike,

I am trying to get the sunrise time for today using your calculator. I have looked at a couple of sources to get the official sunrise time for today and all confirm the time to be about 5:32 AM. When I run the calculator with the same date and location it produces a sunrise time exactly 2 hours behind (3:32AM). The code and setup is as follows:

Location location = new Location("-3.581405","53.284355");
SunriseSunsetCalculator sunriseSunsetCalculator = new SunriseSunsetCalculator(location, TimeZone.getTimeZone("Europe/London"));
String sunriseTimeString = sunriseSunsetCalculator.getOfficialSunriseForDate(GregorianCalendar.getInstance(TimeZone.getTimeZone("Europe/London")));

Does the above code look correct to you? Can you think of any reasons why this would happen with the above code?

mikereedell commented 8 years ago

I plugged your location, TZ, and date into my test harness and got the same bad result: 03:32. I'll have to debug this to determine what's going wrong.

-Mike

JCDubs commented 8 years ago

Thanks @mikereedell

kwojtas commented 8 years ago

I had also problem with sunrise time (almost 6 hours difference). It comes from tan(x) calculations. Increasing scale from 4 to 8 helped in my case (SolarEventCalculator.java). Hope it will help.

private BigDecimal divideBy(BigDecimal dividend, BigDecimal divisor) {
    return dividend.divide(divisor, 8, RoundingMode.HALF_EVEN);
}

private BigDecimal setScale(BigDecimal number) {
    return number.setScale(8, RoundingMode.HALF_EVEN);
}
JCDubs commented 8 years ago

Cheers kwojtas. I will give it a go.

Kneke commented 1 year ago

This pull request applies this mentioned fix #44