From 74460ffcd165f921cae03a86eaf3117501af03eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Teemu=20Po=CC=88ntelin?= Date: Sun, 17 Aug 2014 23:24:28 +0300 Subject: [PATCH] Fixed incorrect week numbers in DateField (#14437) Daylight saving time caused problems with the week number calculation in the DateTimeService.getISOWeekNumber(Date d) method. If the given date was inside a DST period, there was a rounding error when calculating the number of days from the beginning of the year. As a result the week numbers were calculated incorrectly in a case where the year started with a Thursday (like for example 2015 and 2026). Change-Id: Ib3d045ea0b9a1acc44b6f28487b064b3c2b76bb9 --- client/src/com/vaadin/client/DateTimeService.java | 6 +++++- .../src/com/vaadin/client/DateTimeServiceTest.java | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/client/src/com/vaadin/client/DateTimeService.java b/client/src/com/vaadin/client/DateTimeService.java index bf57261c72..f4cfe7a278 100644 --- a/client/src/com/vaadin/client/DateTimeService.java +++ b/client/src/com/vaadin/client/DateTimeService.java @@ -281,7 +281,11 @@ public class DateTimeService { Date firstOfJanuary = new Date(nearestThursday.getYear(), 0, 1); long timeDiff = nearestThursday.getTime() - firstOfJanuary.getTime(); - int daysSinceFirstOfJanuary = (int) (timeDiff / MILLISECONDS_PER_DAY); + + // Rounding the result, as the division doesn't result in an integer + // when the given date is inside daylight saving time period. + int daysSinceFirstOfJanuary = (int) Math.round((double) timeDiff + / MILLISECONDS_PER_DAY); int weekNumber = (daysSinceFirstOfJanuary) / 7 + 1; diff --git a/client/tests/src/com/vaadin/client/DateTimeServiceTest.java b/client/tests/src/com/vaadin/client/DateTimeServiceTest.java index 3f4828678f..f9351d1cc0 100755 --- a/client/tests/src/com/vaadin/client/DateTimeServiceTest.java +++ b/client/tests/src/com/vaadin/client/DateTimeServiceTest.java @@ -48,6 +48,16 @@ public class DateTimeServiceTest extends TestCase { isoWeekNumbers.put(getDate(2010, 1, 4), 1); isoWeekNumbers.put(getDate(2010, 1, 5), 1); isoWeekNumbers.put(getDate(2010, 10, 10), 40); + isoWeekNumbers.put(getDate(2015, 3, 24), 13); + isoWeekNumbers.put(getDate(2015, 3, 31), 14); + isoWeekNumbers.put(getDate(2015, 10, 13), 42); + isoWeekNumbers.put(getDate(2015, 10, 20), 43); + isoWeekNumbers.put(getDate(2015, 10, 27), 44); + isoWeekNumbers.put(getDate(2026, 3, 24), 13); + isoWeekNumbers.put(getDate(2026, 3, 31), 14); + isoWeekNumbers.put(getDate(2026, 10, 13), 42); + isoWeekNumbers.put(getDate(2026, 10, 20), 43); + isoWeekNumbers.put(getDate(2026, 10, 27), 44); } -- 2.39.5