@Test public void invokeParamTemporalWrongTemporal() { // reminder: 1st parameter accordingly to FEEL Spec Table 58 "date is a date or date time [...] creates a date time from the given date (ignoring any time component)" [that means ignoring any TZ from `date` parameter, too] FunctionTestUtil.assertResultError( dateTimeFunction.invoke( LocalDate.of(2017, 6, 12), LocalDateTime.of(2017, 6, 12, 0, 0)), InvalidParametersEvent.class); FunctionTestUtil.assertResultError( dateTimeFunction.invoke( LocalDateTime.of(2017, 6, 12, 0, 0), LocalDateTime.of(2017, 6, 12, 0, 0)), InvalidParametersEvent.class); }
@Test public void invokeParamStringNotDateOrTime() { FunctionTestUtil.assertResultError(dateTimeFunction.invoke("test"), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(dateTimeFunction.invoke("2017-09-test"), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(dateTimeFunction.invoke("2017-09-T89"), InvalidParametersEvent.class); }
@Test public void invokeParamStringDate() { FunctionTestUtil.assertResult(dateTimeFunction.invoke("2017-09-07"), LocalDateTime.of(2017, 9, 7, 0, 0, 0)); }
@Test public void invokeParamTemporalNulls() { FunctionTestUtil.assertResultError(dateTimeFunction.invoke((Temporal) null, null), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(dateTimeFunction.invoke(null, LocalTime.of(10, 6, 20)), InvalidParametersEvent.class); FunctionTestUtil.assertResultError(dateTimeFunction.invoke(LocalDate.of(2017, 6, 12), null), InvalidParametersEvent.class); }
return BuiltInFunctions.getFunction( TimeFunction.class ).invoke( value ).cata( justNull(), Function.identity() ); } else if ( feelType.equals( org.kie.dmn.feel.lang.types.BuiltInType.DATE_TIME ) ) { return BuiltInFunctions.getFunction( DateAndTimeFunction.class ).invoke( value ).cata( justNull(), Function.identity() ); } else if ( feelType.equals( org.kie.dmn.feel.lang.types.BuiltInType.DURATION ) ) { return BuiltInFunctions.getFunction( DurationFunction.class ).invoke( value ).cata( justNull(), Function.identity() );
@Test public void invokeParamTemporalLocalTime() { FunctionTestUtil.assertResult( dateTimeFunction.invoke( LocalDate.of(2017, 6, 12), LocalTime.of(10, 6, 20)), LocalDateTime.of(2017, 6, 12, 10, 6, 20)); }
@Test public void invokeParamTemporalOffsetTime() { FunctionTestUtil.assertResult( dateTimeFunction.invoke( LocalDate.of(2017, 6, 12), OffsetTime.of(10, 6, 20, 0, ZoneOffset.UTC)), ZonedDateTime.of(2017, 6, 12, 10, 6, 20, 0, ZoneOffset.UTC)); } }
@Test public void invokeParamStringNull() { FunctionTestUtil.assertResultError(dateTimeFunction.invoke(null), InvalidParametersEvent.class); }
@Test public void testComposite1() { final FEELFnResult<TemporalAccessor> p1 = dateTimeFunction.invoke("2017-08-10T10:20:00+02:00"); final FEELFnResult<TemporalAccessor> p2 = timeFunction.invoke("23:59:01"); FunctionTestUtil.assertResult(p1, ZonedDateTime.of(2017, 8, 10, 10, 20, 0, 0, ZoneId.of("+02:00"))); FunctionTestUtil.assertResult(p2, LocalTime.of(23, 59, 1)); final FEELFnResult<TemporalAccessor> result = dateTimeFunction.invoke(p1.getOrElse(null), p2.getOrElse(null)); FunctionTestUtil.assertResult(result, LocalDateTime.of(2017, 8, 10, 23, 59, 1)); }
public FEELFnResult<TemporalAccessor> invoke(@ParameterName("from") String val) { if (val == null) { return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "cannot be null")); } if (!BEGIN_YEAR.matcher(val).find()) { // please notice the regex strictly requires the beginning, so we can use find. return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "year not compliant with XML Schema Part 2 Datatypes")); } try { return FEELFnResult.ofResult(LocalDate.from(FEEL_DATE.parse(val))); } catch (DateTimeException e) { // try to parse it as a date time and extract the date component // NOTE: this is an extension to the standard return BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(val) .cata(overrideLeft -> FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "date-parsing exception", e)), this::invoke ); } }
public FEELFnResult<TemporalAccessor> invoke(@ParameterName( "year" ) Number year, @ParameterName( "month" ) Number month, @ParameterName( "day" ) Number day, @ParameterName( "hour" ) Number hour, @ParameterName( "minute" ) Number minute, @ParameterName( "second" ) Number second ) { return invoke( year, month, day, hour, minute, second, (Number) null ); }
@Test public void testComposite4() { final FEELFnResult<TemporalAccessor> p1 = dateFunction.invoke("2017-01-01"); final FEELFnResult<TemporalAccessor> p2 = timeFunction.invoke("23:59:01@Europe/Paris"); FunctionTestUtil.assertResult(p1, LocalDate.of(2017, 1, 1)); final TemporalAccessor p2TA = p2.getOrElse(null); assertNotNull(p2TA); assertEquals(LocalTime.of(23, 59, 1), p2TA.query(TemporalQueries.localTime())); assertEquals(ZoneId.of("Europe/Paris"), p2TA.query(TemporalQueries.zone())); final FEELFnResult<TemporalAccessor> result = dateTimeFunction.invoke(p1.getOrElse(null), p2.getOrElse(null)); FunctionTestUtil.assertResult(result, ZonedDateTime.of(2017, 1, 1, 23, 59, 1, 0, ZoneId.of("Europe/Paris"))); }
@Test public void testComposite5() { final FEELFnResult<TemporalAccessor> p1 = dateTimeFunction.invoke("2017-08-10T10:20:00@Europe/Paris"); FunctionTestUtil.assertResult(p1, ZonedDateTime.of(2017, 8, 10, 10, 20, 0, 0, ZoneId.of("Europe/Paris"))); final TemporalAccessor timeOnDateTime = timeFunction.invoke(p1.getOrElse(null)).getOrElse(null); assertNotNull(timeOnDateTime); assertEquals(LocalTime.of(10, 20, 0), timeOnDateTime.query(TemporalQueries.localTime())); assertEquals(ZoneId.of("Europe/Paris"), timeOnDateTime.query(TemporalQueries.zone())); FunctionTestUtil.assertResult(stringFunction.invoke(timeOnDateTime), "10:20:00@Europe/Paris"); } }
public FEELFnResult<TemporalAccessor> invoke(@ParameterName("from") String val) { if (val == null) { return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "cannot be null")); } try { TemporalAccessor parsed = FEEL_TIME.parse(val); if (parsed.query(TemporalQueries.offset()) != null) { // it is an offset-zoned time, so I can know for certain an OffsetTime OffsetTime asOffSetTime = parsed.query(OffsetTime::from); return FEELFnResult.ofResult(asOffSetTime); } else if (parsed.query(TemporalQueries.zone()) == null) { // if it does not contain any zone information at all, then I know for certain is a local time. LocalTime asLocalTime = parsed.query(LocalTime::from); return FEELFnResult.ofResult(asLocalTime); } return FEELFnResult.ofResult(parsed); } catch (DateTimeException e) { // try to parse it as a date time and extract the date component // NOTE: this is an extension to the standard return BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(val) .cata(overrideLeft -> FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "time-parsing exception", e)), this::invoke ); } }
public FEELFnResult<TemporalAccessor> invoke(@ParameterName("from") TemporalAccessor date) { if ( date == null ) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "cannot be null")); } try { // If the temporal accessor type doesn't support time, try to parse it as a date with UTC midnight. if (!date.isSupported(ChronoField.HOUR_OF_DAY)) { return BuiltInFunctions.getFunction( DateAndTimeFunction.class ).invoke( date, OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC) ) .cata( overrideLeft -> FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "time-parsing exception")), this::invoke ); } else if( date.query( TemporalQueries.offset() ) == null ) { return FEELFnResult.ofResult( LocalTime.from( date ) ); } else { ZoneId zone = date.query(TemporalQueries.zoneId()); if (!(zone instanceof ZoneOffset)) { // TZ is a ZoneRegion, so do NOT normalize (although the result will be unreversible, but will keep what was supplied originally). // Unfortunately java.time.Parsed is a package-private class, hence will need to re-parse in order to have it instantiated. return invoke(date.query(TemporalQueries.localTime()) + "@" + zone); } else { return FEELFnResult.ofResult(OffsetTime.from(date)); } } } catch (DateTimeException e) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "time-parsing exception", e)); } }
public FEELFnResult<TemporalAccessor> invoke(@ParameterName("from") TemporalAccessor date) { if (date == null) { return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "cannot be null")); } try { // If the temporal accessor type doesn't support time, try to parse it as a date with UTC midnight. if (!date.isSupported(ChronoField.HOUR_OF_DAY)) { return BuiltInFunctions.getFunction( DateAndTimeFunction.class ).invoke( date, OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC) ) .cata( overrideLeft -> FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "time-parsing exception")), this::invoke ); } else if( date.query( TemporalQueries.offset() ) == null ) { return FEELFnResult.ofResult(LocalTime.from(date)); } else { ZoneId zone = date.query(TemporalQueries.zoneId()); if (!(zone instanceof ZoneOffset)) { // TZ is a ZoneRegion, so do NOT normalize (although the result will be unreversible, but will keep what was supplied originally). // Unfortunately java.time.Parsed is a package-private class, hence will need to re-parse in order to have it instantiated. return invoke(date.query(TemporalQueries.localTime()) + "@" + zone); } else { return FEELFnResult.ofResult(OffsetTime.from(date)); } } } catch (DateTimeException e) { return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "time-parsing exception", e)); } } }