Permalink
Browse files

Add Query for ZoneOffset

  • Loading branch information...
jodastephen committed Dec 4, 2012
1 parent f2d453b commit 28da1e02dfbfc8334c672960f372007f2fdbbbbf
@@ -879,6 +879,8 @@ public long periodUntil(DateTime endDateTime, PeriodUnit unit) {
public <R> R query(Query<R> query) {
if (query == Query.CHRONO) {
return (R) ISOChrono.INSTANCE;
+ } else if (query == Query.OFFSET) {
+ return (R) getOffset();
}
return super.query(query);
}
@@ -1440,6 +1440,8 @@ public DateTime doWithAdjustment(DateTime dateTime) {
return (R) getDate().getChrono();
} else if (query == Query.TIME_PRECISION) {
return (R) NANOS;
+ } else if (query == Query.OFFSET) {
+ return (R) getOffset();
}
return super.query(query);
}
@@ -818,6 +818,8 @@ public long periodUntil(DateTime endDateTime, PeriodUnit unit) {
public <R> R query(Query<R> query) {
if (query == Query.TIME_PRECISION) {
return (R) NANOS;
+ } else if (query == Query.OFFSET) {
+ return (R) getOffset();
}
return super.query(query);
}
@@ -31,8 +31,11 @@
*/
package javax.time.calendrical;
+import static javax.time.calendrical.ChronoField.OFFSET_SECONDS;
+
import javax.time.DateTimeException;
import javax.time.ZoneId;
+import javax.time.ZoneOffset;
import javax.time.chrono.Chrono;
/**
@@ -214,6 +217,7 @@ public ZoneId doQuery(DateTimeAccessor dateTime) {
* <p>
* If the target {@code DateTimeAccessor} has a chronology, then querying
* it with this constant must return the chronology.
+ * Note that {@code LocalTime} returns null as it is valid for all chronologies.
*/
Query<Chrono<?>> CHRONO = new Query<Chrono<?>>() {
@Override
@@ -239,6 +243,23 @@ public ChronoUnit doQuery(DateTimeAccessor dateTime) {
return null;
}
};
+ /**
+ * A query for the {@code ZoneOffset}.
+ * <p>
+ * This query examines the {@link ChronoField#OFFSET_SECONDS offset-seconds}
+ * field and uses it to create a {@code ZoneOffset}.
+ * Implementations of {@code DateTimeAccessor} may choose to check for this
+ * constant and return a stored offset directly.
+ */
+ Query<ZoneOffset> OFFSET = new Query<ZoneOffset>() {
+ @Override
+ public ZoneOffset doQuery(DateTimeAccessor dateTime) {
+ if (dateTime.isSupported(OFFSET_SECONDS)) {
+ return ZoneOffset.ofTotalSeconds(dateTime.get(OFFSET_SECONDS));
+ }
+ return null;
+ }
+ };
/**
* Implementation of the strategy to query the specified date-time object.
@@ -128,6 +128,7 @@ public LocalTime getTime() {
}
//-------------------------------------------------------------------------
+ @SuppressWarnings("unchecked")
@Override
public <R> R query(Query<R> query) {
if (query == Query.ZONE_ID) {
@@ -136,6 +137,8 @@ public LocalTime getTime() {
return (R) getDate().getChrono();
} else if (query == Query.TIME_PRECISION) {
return (R) NANOS;
+ } else if (query == Query.OFFSET) {
+ return (R) getOffset();
}
return super.query(query);
}
@@ -614,6 +614,11 @@ public void test_query_timePrecision() {
assertEquals(TEST_2007_07_15.query(Query.TIME_PRECISION), null);
}
+ @Test(groups={"tck"})
+ public void test_query_offset() {
+ assertEquals(TEST_2007_07_15.query(Query.OFFSET), null);
+ }
+
@Test(expectedExceptions=NullPointerException.class, groups={"tck"})
public void test_query_null() {
TEST_2007_07_15.query(null);
@@ -889,6 +889,11 @@ public void test_query_timePrecision() {
assertEquals(TEST_2007_07_15_12_30_40_987654321.query(Query.TIME_PRECISION), NANOS);
}
+ @Test(groups={"tck"})
+ public void test_query_offset() {
+ assertEquals(TEST_2007_07_15_12_30_40_987654321.query(Query.OFFSET), null);
+ }
+
@Test(expectedExceptions=NullPointerException.class, groups={"tck"})
public void test_query_null() {
TEST_2007_07_15_12_30_40_987654321.query(null);
@@ -632,6 +632,11 @@ public void test_query_timePrecision() {
assertEquals(TEST_12_30_40_987654321.query(Query.TIME_PRECISION), NANOS);
}
+ @Test(groups={"tck"})
+ public void test_query_offset() {
+ assertEquals(TEST_12_30_40_987654321.query(Query.OFFSET), null);
+ }
+
@Test(expectedExceptions=NullPointerException.class, groups={"tck"})
public void test_query_null() {
TEST_12_30_40_987654321.query(null);
@@ -64,10 +64,12 @@
import javax.time.calendrical.DateTime.MinusAdjuster;
import javax.time.calendrical.DateTime.PlusAdjuster;
import javax.time.calendrical.DateTime.WithAdjuster;
+import javax.time.calendrical.DateTimeAccessor.Query;
import javax.time.calendrical.DateTimeAccessor;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.JulianDayField;
import javax.time.calendrical.MockFieldNoValue;
+import javax.time.chrono.ISOChrono;
import javax.time.format.DateTimeFormatter;
import javax.time.format.DateTimeFormatters;
import javax.time.format.DateTimeParseException;
@@ -506,6 +508,26 @@ public void test_get_DateTimeField_null() {
//-----------------------------------------------------------------------
// query(Query)
//-----------------------------------------------------------------------
+ @Test(groups={"tck"})
+ public void test_query_chrono() {
+ assertEquals(TEST_2007_07_15_PONE.query(Query.CHRONO), ISOChrono.INSTANCE);
+ }
+
+ @Test(groups={"tck"})
+ public void test_query_zone() {
+ assertEquals(TEST_2007_07_15_PONE.query(Query.ZONE_ID), null);
+ }
+
+ @Test(groups={"tck"})
+ public void test_query_timePrecision() {
+ assertEquals(TEST_2007_07_15_PONE.query(Query.TIME_PRECISION), null);
+ }
+
+ @Test(groups={"tck"})
+ public void test_query_offset() {
+ assertEquals(TEST_2007_07_15_PONE.query(Query.OFFSET), OFFSET_PONE);
+ }
+
@Test(expectedExceptions=NullPointerException.class, groups={"tck"})
public void test_query_null() {
TEST_2007_07_15_PONE.query(null);
@@ -64,6 +64,7 @@
import static javax.time.calendrical.ChronoField.WEEK_OF_YEAR;
import static javax.time.calendrical.ChronoField.YEAR;
import static javax.time.calendrical.ChronoField.YEAR_OF_ERA;
+import static javax.time.calendrical.ChronoUnit.NANOS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
@@ -78,10 +79,12 @@
import javax.time.calendrical.ChronoUnit;
import javax.time.calendrical.DateTime;
import javax.time.calendrical.DateTime.WithAdjuster;
+import javax.time.calendrical.DateTimeAccessor.Query;
import javax.time.calendrical.DateTimeAccessor;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.JulianDayField;
import javax.time.calendrical.MockFieldNoValue;
+import javax.time.chrono.ISOChrono;
import javax.time.format.DateTimeFormatter;
import javax.time.format.DateTimeFormatters;
import javax.time.format.DateTimeParseException;
@@ -596,6 +599,26 @@ public void test_get_DateTimeField_null() {
//-----------------------------------------------------------------------
// query(Query)
//-----------------------------------------------------------------------
+ @Test(groups={"tck"})
+ public void test_query_chrono() {
+ assertEquals(TEST_2008_6_30_11_30_59_000000500.query(Query.CHRONO), ISOChrono.INSTANCE);
+ }
+
+ @Test(groups={"tck"})
+ public void test_query_zone() {
+ assertEquals(TEST_2008_6_30_11_30_59_000000500.query(Query.ZONE_ID), null);
+ }
+
+ @Test(groups={"tck"})
+ public void test_query_timePrecision() {
+ assertEquals(TEST_2008_6_30_11_30_59_000000500.query(Query.TIME_PRECISION), NANOS);
+ }
+
+ @Test(groups={"tck"})
+ public void test_query_offset() {
+ assertEquals(TEST_2008_6_30_11_30_59_000000500.query(Query.OFFSET), OFFSET_PONE);
+ }
+
@Test(expectedExceptions=NullPointerException.class, groups={"tck"})
public void test_query_null() {
TEST_2008_6_30_11_30_59_000000500.query(null);
@@ -47,6 +47,7 @@
import static javax.time.calendrical.ChronoField.OFFSET_SECONDS;
import static javax.time.calendrical.ChronoField.SECOND_OF_DAY;
import static javax.time.calendrical.ChronoField.SECOND_OF_MINUTE;
+import static javax.time.calendrical.ChronoUnit.NANOS;
import static javax.time.calendrical.ChronoUnit.SECONDS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
@@ -72,6 +73,7 @@
import javax.time.calendrical.DateTime.PlusAdjuster;
import javax.time.calendrical.DateTime.WithAdjuster;
import javax.time.calendrical.DateTimeAccessor;
+import javax.time.calendrical.DateTimeAccessor.Query;
import javax.time.calendrical.DateTimeField;
import javax.time.calendrical.JulianDayField;
import javax.time.calendrical.MockFieldNoValue;
@@ -536,6 +538,26 @@ public void test_get_DateTimeField_null() {
//-----------------------------------------------------------------------
// query(Query)
//-----------------------------------------------------------------------
+ @Test(groups={"tck"})
+ public void test_query_chrono() {
+ assertEquals(TEST_11_30_59_500_PONE.query(Query.CHRONO), null);
+ }
+
+ @Test(groups={"tck"})
+ public void test_query_zone() {
+ assertEquals(TEST_11_30_59_500_PONE.query(Query.ZONE_ID), null);
+ }
+
+ @Test(groups={"tck"})
+ public void test_query_timePrecision() {
+ assertEquals(TEST_11_30_59_500_PONE.query(Query.TIME_PRECISION), NANOS);
+ }
+
+ @Test(groups={"tck"})
+ public void test_query_offset() {
+ assertEquals(TEST_11_30_59_500_PONE.query(Query.OFFSET), OFFSET_PONE);
+ }
+
@Test(expectedExceptions=NullPointerException.class, groups={"tck"})
public void test_query_null() {
TEST_11_30_59_500_PONE.query(null);
@@ -898,6 +898,11 @@ public void test_query_timePrecision() {
assertEquals(TEST_DATE_TIME.query(Query.TIME_PRECISION), NANOS);
}
+ @Test(groups={"tck"})
+ public void test_query_offset() {
+ assertEquals(TEST_DATE_TIME.query(Query.OFFSET), OFFSET_0100);
+ }
+
@Test(expectedExceptions=NullPointerException.class, groups={"tck"})
public void test_query_null() {
TEST_DATE_TIME.query(null);

0 comments on commit 28da1e0

Please sign in to comment.