Search in sources :

Example 81 with TemporalAccessor

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));
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 82 with TemporalAccessor

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);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 83 with TemporalAccessor

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);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 84 with TemporalAccessor

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);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 85 with TemporalAccessor

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)));
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) Instant(java.time.Instant) IOException(java.io.IOException) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) Map(java.util.Map) Date(java.util.Date)

Aggregations

TemporalAccessor (java.time.temporal.TemporalAccessor)235 Test (org.testng.annotations.Test)189 DateTimeFormatter (java.time.format.DateTimeFormatter)103 ParsePosition (java.text.ParsePosition)82 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)61 DateTimeException (java.time.DateTimeException)32 DateTimeParseException (java.time.format.DateTimeParseException)24 LocalDate (java.time.LocalDate)14 TemporalField (java.time.temporal.TemporalField)13 ZonedDateTime (java.time.ZonedDateTime)7 LocalTime (java.time.LocalTime)6 InvalidParametersEvent (org.kie.dmn.feel.runtime.events.InvalidParametersEvent)6 Instant (java.time.Instant)5 ZoneId (java.time.ZoneId)5 ChronoLocalDate (java.time.chrono.ChronoLocalDate)5 Chronology (java.time.chrono.Chronology)5 ResolverStyle (java.time.format.ResolverStyle)5 Test (org.junit.Test)5 LocalDateTime (java.time.LocalDateTime)4 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)4