ThreeTen / threeten

This project was the home of code used to develop a modern date and time library for JDK8. Development has moved to OpenJDK and a separate backport project, threetenbp.
http://threeten.github.io/
191 stars 37 forks source link

Test case where location moves from -12:00 to +12:00 and vice versa #316

Closed jodastephen closed 10 years ago

jodastephen commented 11 years ago

Some locations have moved across the international date line, changing their local clock by a whole day. This should be explicitly tested for, due to complexities involved. We should find both the actual examples in TZDB and create artificial examples.

jodastephen commented 11 years ago
# HG changeset patch
# User scolebourne
# Date 1372692757 -3600
# Node ID 12662b1fc3d4ce28493eaa6a15e245f60f04a1f0
# Parent  81d32359e09023be1f642f27181b38137295e457
Test jumps over the international date line
See #316

diff --git a/test/java/time/tck/java/time/zone/TCKZoneRules.java b/test/java/time/tck/java/time/zone/TCKZoneRules.java
--- a/test/java/time/tck/java/time/zone/TCKZoneRules.java
+++ b/test/java/time/tck/java/time/zone/TCKZoneRules.java
@@ -920,6 +920,65 @@
         assertEquals(test.nextTransition(last.getInstant()), null);
     }

+    //-----------------------------------------------------------------------
+    // Apia
+    //-----------------------------------------------------------------------
+    private ZoneRules pacificApia() {
+        return ZoneId.of("Pacific/Apia").getRules();
+    }
+
+    public void test_Apia_nextTransition_historic() {
+        ZoneRules test = pacificApia();
+        List<ZoneOffsetTransition> trans = test.getTransitions();
+
+        ZoneOffsetTransition first = trans.get(0);
+        assertEquals(test.nextTransition(first.getInstant().minusNanos(1)), first);
+
+        for (int i = 0; i < trans.size() - 1; i++) {
+            ZoneOffsetTransition cur = trans.get(i);
+            ZoneOffsetTransition next = trans.get(i + 1);
+
+            assertEquals(test.nextTransition(cur.getInstant()), next);
+            assertEquals(test.nextTransition(next.getInstant().minusNanos(1)), next);
+        }
+    }
+
+    public void test_Apia_jumpOverInternationalDateLine_M10_to_P14() {
+        // transition occurred at 2011-12-30T00:00-10:00
+        ZoneRules test = pacificApia();
+        Instant instantBefore = LocalDate.of(2011, 12, 27).atStartOfDay(ZoneOffset.UTC).toInstant();
+        ZoneOffsetTransition trans = test.nextTransition(instantBefore);
+        assertEquals(trans.getDateTimeBefore(), LocalDateTime.of(2011, 12, 30, 0, 0));
+        assertEquals(trans.getDateTimeAfter(), LocalDateTime.of(2011, 12, 31, 0, 0));
+        assertEquals(trans.isGap(), true);
+        assertEquals(trans.isOverlap(), false);
+        assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-10)), false);
+        assertEquals(trans.isValidOffset(ZoneOffset.ofHours(+14)), false);
+        assertEquals(trans.getDuration(), Duration.ofHours(24));
+        assertEquals(trans.getInstant(), LocalDateTime.of(2011, 12, 31, 0, 0).toInstant(ZoneOffset.ofHours(+14)));
+
+        ZonedDateTime zdt = ZonedDateTime.of(2011, 12, 29, 23, 0, 0, 0, ZoneId.of("Pacific/Apia"));
+        assertEquals(zdt.plusHours(2).toLocalDateTime(), LocalDateTime.of(2011, 12, 31, 1, 0));
+    }
+
+    public void test_Apia_jumpForwardOverInternationalDateLine_P12_to_M12() {
+        // transition occurred at 1879-07-04T00:00+12:33:04
+        ZoneRules test = pacificApia();
+        Instant instantBefore = LocalDate.of(1879, 7, 2).atStartOfDay(ZoneOffset.UTC).toInstant();
+        ZoneOffsetTransition trans = test.nextTransition(instantBefore);
+        assertEquals(trans.getDateTimeBefore(), LocalDateTime.of(1879, 7, 5, 0, 0));
+        assertEquals(trans.getDateTimeAfter(), LocalDateTime.of(1879, 7, 4, 0, 0));
+        assertEquals(trans.isGap(), false);
+        assertEquals(trans.isOverlap(), true);
+        assertEquals(trans.isValidOffset(ZoneOffset.ofHoursMinutesSeconds(+12, 33, 4)), true);
+        assertEquals(trans.isValidOffset(ZoneOffset.ofHoursMinutesSeconds(-11, -26, -56)), true);
+        assertEquals(trans.getDuration(), Duration.ofHours(-24));
+        assertEquals(trans.getInstant(), LocalDateTime.of(1879, 7, 4, 0, 0).toInstant(ZoneOffset.ofHoursMinutesSeconds(-11, -26, -56)));
+
+        ZonedDateTime zdt = ZonedDateTime.of(1879, 7, 4, 23, 0, 0, 0, ZoneId.of("Pacific/Apia"));
+        assertEquals(zdt.plusHours(2).toLocalDateTime(), LocalDateTime.of(1879, 7, 4, 1, 0, 0));
+    }
+
     //-------------------------------------------------------------------------
     @Test(expectedExceptions=UnsupportedOperationException.class)
     public void test_getTransitions_immutable() {
RogerRiggs commented 10 years ago

looks fine AFAICT

jodastephen commented 10 years ago

Fixed by http://hg.openjdk.java.net/threeten/threeten/jdk/rev/bb4def168896