Skip to content

Fix getUTCOffSet rounding for non-integer timezones#49

Open
guillerodriguez wants to merge 1 commit intomikereedell:masterfrom
guillerodriguez:fix/utc-offset-significant-digits
Open

Fix getUTCOffSet rounding for non-integer timezones#49
guillerodriguez wants to merge 1 commit intomikereedell:masterfrom
guillerodriguez:fix/utc-offset-significant-digits

Conversation

@guillerodriguez
Copy link
Copy Markdown

SolarEventCalculator.getUTCOffSet() uses BigDecimal.divide(3600000, new MathContext(2)) to convert ZONE_OFFSET into fractional hours. This was introduced in commit 30f64ec to fix handling of zones with non-integer offsets, such as Asia/Kolkata (UTC +5:30).

However, MathContext(2) caps the result to 2 significant digits (not "2 decimal places"), so timezones whose offset has more than 2 significant digits were rounded incorrectly.

Examples:

Asia/Kathmandu      +5:45 (5.75)   -> 5.8 (off by  3 min)
Australia/Eucla     +8:45 (8.75)   -> 8.8 (off by  3 min)
Pacific/Chatham     +12:45 (12.75) -> 13  (off by 15 min)
Australia/Lord_Howe +10:30 (10.5)  -> 11  (off by 30 min)

Fix by using the existing divideBy() helper instead.

Fixes: #48

SolarEventCalculator.getUTCOffSet() uses BigDecimal.divide(3600000,
new MathContext(2)) to convert ZONE_OFFSET into fractional hours.
This was introduced in commit 30f64ec to fix handling of zones with
non-integer offsets, such as Asia/Kolkata (UTC +5:30).

However, MathContext(2) caps the result to 2 *significant digits*
(not "2 decimal places"), so timezones whose offset has more than
2 significant digits were rounded incorrectly.

Examples:

    Asia/Kathmandu      +5:45 (5.75)   -> 5.8 (off by  3 min)
    Australia/Eucla     +8:45 (8.75)   -> 8.8 (off by  3 min)
    Pacific/Chatham     +12:45 (12.75) -> 13  (off by 15 min)
    Australia/Lord_Howe +10:30 (10.5)  -> 11  (off by 30 min)

Fix by using the existing divideBy() helper instead.

Fixes: mikereedell#48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getUTCOffSet still returns wrong results for some non-integer timezone offsets

1 participant