use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.
the class TestReducedParser method test_reducedWithLateChronoChangeTwice.
@Test
public void test_reducedWithLateChronoChangeTwice() {
DateTimeFormatter df = new DateTimeFormatterBuilder().appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1)).appendLiteral(" ").appendChronologyId().appendLiteral(" ").appendChronologyId().toFormatter();
int expected = 2044;
String input = "44 ThaiBuddhist ISO";
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = df.parseUnresolved(input, pos);
assertEquals(pos.getIndex(), input.length(), "Input not parsed completely: " + pos);
assertEquals(pos.getErrorIndex(), -1, "Error index should be -1 (no-error)");
int actual = parsed.get(YEAR);
assertEquals(actual, expected, String.format("Wrong date parsed, chrono: %s, input: %s", parsed.query(TemporalQueries.chronology()), input));
}
use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.
the class TestReducedParser method test_parseLenient_baseDate.
@Test(dataProvider = "ParseLenientSensitive")
public void test_parseLenient_baseDate(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos, Pair strict, Pair lenient) {
ParsePosition ppos = new ParsePosition(pos);
setStrict(false);
TemporalAccessor parsed = getFormatterBaseDate(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), lenient.parseLen, "error case parse position");
assertEquals(parsed, lenient.parseVal, "unexpected parse result");
} else {
assertEquals(ppos.getIndex(), lenient.parseLen, "parse position");
assertParsed(parsed, YEAR, lenient.parseVal != null ? (long) lenient.parseVal : null);
}
}
use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.
the class TestReducedParser method test_parseStrict.
//-----------------------------------------------------------------------
// Parsing tests for strict mode
//-----------------------------------------------------------------------
@Test(dataProvider = "ParseLenientSensitive")
public void test_parseStrict(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos, Pair strict, Pair lenient) {
ParsePosition ppos = new ParsePosition(pos);
setStrict(true);
TemporalAccessor parsed = getFormatter0(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), strict.parseLen, "error case parse position");
assertEquals(parsed, strict.parseVal, "unexpected parse result");
} else {
assertEquals(ppos.getIndex(), strict.parseLen, "parse position");
assertParsed(parsed, YEAR, strict.parseVal != null ? (long) strict.parseVal : null);
}
}
use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.
the class TestFractionPrinterParser method test_reverseParse_preceededByNonDigit.
// @Test(dataProvider="Nanos")
// public void test_reverseParse_followedByNonDigit_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception {
// FractionPrinterParser pp = new FractionPrinterParser(NANO_OF_SECOND, minWidth, maxWidth, false);
// int newPos = pp.parse(parseContext, result + " ", (result.startsWith(".") ? 1 : 0));
// assertEquals(newPos, result.length());
// int expectedValue = fixParsedValue(maxWidth, value);
// assertParsed(parseContext, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue);
// }
@Test(dataProvider = "Nanos")
public void test_reverseParse_preceededByNonDigit(int minWidth, int maxWidth, int value, String result) throws Exception {
ParsePosition pos = new ParsePosition(1);
int expectedValue = fixParsedValue(maxWidth, value);
TemporalAccessor parsed = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseUnresolved(" " + result, pos);
assertEquals(pos.getIndex(), result.length() + 1);
assertParsed(parsed, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue);
}
use of java.time.temporal.TemporalAccessor in project lucene-solr by apache.
the class TemporalEvaluator method evaluate.
@Override
public Object evaluate(Tuple tuple) throws IOException {
Instant instant = null;
TemporalAccessor date = null;
//First evaluate the parameter
StreamEvaluator streamEvaluator = subEvaluators.get(0);
Object tupleValue = streamEvaluator.evaluate(tuple);
if (tupleValue == null)
return null;
if (field == null) {
field = streamEvaluator.toExpression(constructingFactory).toString();
}
Map tupleContext = streamContext.getTupleContext();
// Check to see if the date has already been created for this field
date = (LocalDateTime) tupleContext.get(field);
if (date == null) {
if (tupleValue instanceof String) {
instant = getInstant((String) tupleValue);
} else if (tupleValue instanceof Long) {
instant = Instant.ofEpochMilli((Long) tupleValue);
} else if (tupleValue instanceof Instant) {
instant = (Instant) tupleValue;
} else if (tupleValue instanceof Date) {
instant = ((Date) tupleValue).toInstant();
} else if (tupleValue instanceof TemporalAccessor) {
date = ((TemporalAccessor) tupleValue);
// Cache the date in the TupleContext
tupleContext.put(field, date);
}
}
if (instant != null) {
if (TemporalEvaluatorEpoch.FUNCTION_NAME.equals(getFunction()))
return instant.toEpochMilli();
date = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
// Cache the date in the TupleContext
tupleContext.put(field, date);
}
if (date != null) {
try {
return evaluateDate(date);
} catch (UnsupportedTemporalTypeException utte) {
throw new IOException(String.format(Locale.ROOT, "It is not possible to call '%s' function on %s", getFunction(), date.getClass().getName()));
}
}
throw new IOException(String.format(Locale.ROOT, "Invalid parameter %s - The parameter must be a string formatted ISO_INSTANT or of type Long,Instant,Date,LocalDateTime or TemporalAccessor.", String.valueOf(tupleValue)));
}
Aggregations