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 69 forks source link

Error in Timezone Offset #9

Closed bhaskar-c closed 9 years ago

bhaskar-c commented 10 years ago

The sunsrise/sunet calculator works good but there is an error in SolarEventCalculator.java which seems to be rounding the TimeZone offset.

private BigDecimal getUTCOffSet(Calendar date) {
        int offSetInMillis = date.get(Calendar.ZONE_OFFSET);
        BigDecimal offSet = new BigDecimal(offSetInMillis / 3600000);
        //Log.e("offset", offSet.setScale(0, RoundingMode.HALF_EVEN)+"");
       return offSet.setScale(0, RoundingMode.HALF_EVEN);
}

If I pass "Asia/Kolkata" as the Timezone, which has an offset of UTC + 5.30, this code returns the offset as 5, which in turn causes a major difference in the time output by the program. I tested this by adding a Log to the offset value.

I corrected the error by changing the offSetInMillis in code from int to double and simply returning offSet.setScale(2);

        double offSetInMillis = date.get(Calendar.ZONE_OFFSET);
        BigDecimal offSet = new BigDecimal(offSetInMillis / 3600000);
        return offSet.setScale(2);
saket commented 9 years ago

Is this bug still present?

TheNephilim88 commented 9 years ago

Just check the above mentioned fix...

No, SolarEventCalculator.java still contains "return offSet.setScale(0, RoundingMode.HALF_EVEN);" instead the fix. Just do the fix in your fork.

mikereedell commented 9 years ago

Issue has been fixed in most recent commit.

saket commented 9 years ago

Also, thank you Mike for this library. People like you is why we are able to develop awesome apps :)