From aa6032c354ee6da00654adfc2332037452984265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Rodr=C3=ADguez?= Date: Mon, 27 Apr 2026 01:07:58 +0200 Subject: [PATCH] Fix adjustForDST to use actual DST savings SolarEventCalculator.adjustForDST() always adds 1 hour during DST, but some timezones have different DST savings (for example, 2 hours in Antarctica/Troll, or 30 minutes in Australia/Lord_Howe). Also, some timezones have changed their DST rules historically. So, compute DST savings as: TimeZone.getOffset(millis) - TimeZone.getRawOffset() which returns the actual value for the given timestamp, instead of TimeZone.getDSTSavings(), which would return the timezone's "current" or "default" DST savings. Fixes #18 --- .../sunrisesunset/calculator/SolarEventCalculator.java | 4 +++- .../calculator/SolarEventCalculatorTest.java | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/luckycatlabs/sunrisesunset/calculator/SolarEventCalculator.java b/src/main/java/com/luckycatlabs/sunrisesunset/calculator/SolarEventCalculator.java index 323fc7a..ffdc64b 100644 --- a/src/main/java/com/luckycatlabs/sunrisesunset/calculator/SolarEventCalculator.java +++ b/src/main/java/com/luckycatlabs/sunrisesunset/calculator/SolarEventCalculator.java @@ -282,7 +282,9 @@ private BigDecimal getLocalTime(BigDecimal localMeanTime, Calendar date) { private BigDecimal adjustForDST(BigDecimal localMeanTime, Calendar date) { BigDecimal localTime = localMeanTime; if (timeZone.inDaylightTime(date.getTime())) { - localTime = localTime.add(BigDecimal.ONE); + long dstMillis = timeZone.getOffset(date.getTimeInMillis()) - timeZone.getRawOffset(); + BigDecimal dstSavings = divideBy(BigDecimal.valueOf(dstMillis), BigDecimal.valueOf(3600000)); + localTime = localTime.add(dstSavings); } if (localTime.doubleValue() > 24.0) { localTime = localTime.subtract(BigDecimal.valueOf(24)); diff --git a/src/test/java/com/luckycatlabs/sunrisesunset/calculator/SolarEventCalculatorTest.java b/src/test/java/com/luckycatlabs/sunrisesunset/calculator/SolarEventCalculatorTest.java index ae54293..517b689 100644 --- a/src/test/java/com/luckycatlabs/sunrisesunset/calculator/SolarEventCalculatorTest.java +++ b/src/test/java/com/luckycatlabs/sunrisesunset/calculator/SolarEventCalculatorTest.java @@ -69,4 +69,13 @@ public void testGetLocalTimeAsCalendarForNegative() { assertEquals(14, localTime.get(Calendar.HOUR_OF_DAY)); assertEquals(0, localTime.get(Calendar.MINUTE)); } + + @Test + public void testAdjustForDST() { + // Antarctica/Troll: standard UTC+0, DST UTC+2 (savings = +2 h). + super.setup(Calendar.APRIL, 1, 2024, "-72.0117", "2.5350", "Antarctica/Troll"); + SolarEventCalculator trollCalc = new SolarEventCalculator(location, "Antarctica/Troll"); + assertEquals("08:43", trollCalc.computeSunriseTime(Zenith.OFFICIAL, eventDate)); + assertEquals("19:02", trollCalc.computeSunsetTime(Zenith.OFFICIAL, eventDate)); + } }