private <T> T toDateObject(String raw, TemporalQuery<T> from) { if (raw.contains(":59:60")) { // ISO_INSTANT doesn't crash on leap seconds TemporalAccessor parse = DateTimeFormatter.ISO_INSTANT.parse(raw); if (parse.query(DateTimeFormatter.parsedLeapSecond())) { // lazy: push back the second instead logger.warn("Exciting! Saw a leap second, shifting the date back 1s, {}", raw); return ISO.parse(raw.replace(":59:60", ":59:59"), from); } } return ISO.parse(raw, from); }
/** * Gets a value using a query. * * @param query the query to use, not null * @return the result, null if not found and optional is true * @throws DateTimeException if the type is not available and the section is not optional */ <R> R getValue(TemporalQuery<R> query) { R result = temporal.query(query); if (result == null && optional == 0) { throw new DateTimeException("Unable to extract value: " + temporal.getClass()); } return result; }
@Test public void basicTest_query() { for (TemporalAccessor sample : samples()) { assertEquals(sample.query(new TemporalQuery<String>() { @Override public String queryFrom(TemporalAccessor temporal) { return "foo"; } }), "foo"); } }
/** * Gets a value using a query. * * @param query the query to use, not null * @return the result, null if not found and optional is true * @throws DateTimeException if the type is not available and the section is not optional */ <R> R getValue(TemporalQuery<R> query) { R result = temporal.query(query); if (result == null && optional == 0) { throw new DateTimeException("Unable to extract " + query + " from temporal " + temporal); } return result; }
/** Constructor. */ public ClassicFormat(DateTimeFormatter formatter, TemporalQuery<?> parseType) { this.formatter = formatter; this.parseType = parseType; }
ZoneIdPrinterParser(TemporalQuery<ZoneId> query, String description) { this.query = query; this.description = description; }
@Test(dataProvider="query") public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { assertEquals(temporal.query(query), expected); }
@Test(dataProvider="query") public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { assertEquals(query.queryFrom(temporal), expected); }
@Test(expectedExceptions=NullPointerException.class) public void test_parse_Query_String_nullRule() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); test.parse("30", (TemporalQuery<?>) null); }
@Test(expectedExceptions=NullPointerException.class) public void test_parseBest_String_nullRules() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); test.parseBest("30", (TemporalQuery<?>[]) null); }
@Test(expectedExceptions=IllegalArgumentException.class) public void test_parseBest_String_zeroRules() throws Exception { DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); test.parseBest("30", new TemporalQuery<?>[0]); }