Michiel Rop pushed to branch master at cms-community / hippo-repository
Commits: e8ab0978 by Michiel Rop at 2017-03-21T15:54:31+01:00 REPO-1636:Take into account daylight saving * Take into account time zones to calculate the expected length of the interval between dates. java.util.Date has not been designed for internationalization and does not have a timezone concept. In the unit test java.util.Date was used to calcutate the length of the interval in days. Since the timezone was not taken into account that fails if the moment of changing to daylight saving and back lies within the interval. The tested method returns the correct value. - - - - - 1 changed file: - utilities/src/test/java/org/onehippo/repository/util/DateMathParserTest.java Changes: ===================================== utilities/src/test/java/org/onehippo/repository/util/DateMathParserTest.java ===================================== --- a/utilities/src/test/java/org/onehippo/repository/util/DateMathParserTest.java +++ b/utilities/src/test/java/org/onehippo/repository/util/DateMathParserTest.java @@ -21,12 +21,12 @@ import java.text.ParseException; import org.junit.Test; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; public class DateMathParserTest { + public static final int DAY_IN_MILLIS = 1000 * 60 * 60 * 24; + private static Calendar getNow() { Calendar startCal = Calendar.getInstance(); startCal.setTime(new Date()); @@ -43,8 +43,13 @@ public class DateMathParserTest { long startTime = startDate.getTime(); long endTime = endDate.getTime(); long diffTime = endTime - startTime; - long diff = diffTime / (1000 * 60 * 60 * 24); - assertTrue(diff == 7); + long diff = diffTime / DAY_IN_MILLIS; + Calendar nextWeek = getNow(); + nextWeek.add(Calendar.DATE,7); + final long utcEndTime = endTime - nextWeek.getTimeZone().getOffset(endTime); + final long utcStartTime = startTime - now.getTimeZone().getOffset(startTime); + long utcDiff = utcEndTime - utcStartTime; + assertEquals(utcDiff/ DAY_IN_MILLIS, diff); } catch (IllegalStateException ex) { fail(); @@ -60,8 +65,13 @@ public class DateMathParserTest { assertNotNull(endDate); long startTime = startDate.getTime(); long endTime = endDate.getTime(); - long diff = (startTime / (1000 * 60 * 60 * 24)) - (endTime / (1000 * 60 * 60 * 24)); - assertTrue(diff == 7); + long diff = (startTime / DAY_IN_MILLIS) - (endTime / DAY_IN_MILLIS); + Calendar previousWeek = getNow(); + previousWeek.add(Calendar.DATE,-7); + final long utcStartTime = startTime - now.getTimeZone().getOffset(startTime); + final long utcEndTime = endTime - previousWeek.getTimeZone().getOffset(endTime); + long utcDiff = utcStartTime - utcEndTime; + assertEquals(utcDiff/ DAY_IN_MILLIS, diff); } catch (IllegalStateException ex) { fail(); View it on GitLab: https://code.onehippo.org/cms-community/hippo-repository/commit/e8ab0978b4ca35c44f62b0a9977fe993bde1ca19
_______________________________________________ Hippocms-svn mailing list Hippocms-svn@lists.onehippo.org https://lists.onehippo.org/mailman/listinfo/hippocms-svn