Search in sources :

Example 21 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project j2objc by google.

the class TCKChronoPeriod method test_plus.

// -----------------------------------------------------------------------
@Test()
@UseDataProvider("data_of_calendars")
public void test_plus(Chronology chrono) {
    ChronoPeriod period = chrono.period(1, 2, 3);
    ChronoPeriod period2 = chrono.period(2, 3, 4);
    ChronoPeriod result = period.plus(period2);
    assertEquals(result, chrono.period(3, 5, 7));
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 22 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project j2objc by google.

the class TCKChronoPeriod method test_negated.

/* J2ObjC removed: Only "gregorian" and "julian" calendars are supported.
    @Test(expected=DateTimeException.class)
    @UseDataProvider("data_of_calendars")
    public void test_subtractFrom_wrongChrono(Chronology chrono) {
        ChronoPeriod period = chrono.period(1, 2, 3);
        ChronoLocalDate isoDate = LocalDate.of(2000, 1, 1);
        ChronoLocalDate thaiDate = ThaiBuddhistChronology.INSTANCE.date(2000, 1, 1);
        // one of these two will fail
        period.subtractFrom(isoDate);
        period.subtractFrom(thaiDate);
    } */
// -----------------------------------------------------------------------
@Test()
@UseDataProvider("data_of_calendars")
public void test_negated(Chronology chrono) {
    ChronoPeriod period = chrono.period(1, 2, 3);
    assertEquals(period.negated(), chrono.period(-1, -2, -3));
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 23 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project j2objc by google.

the class Period method from.

// -----------------------------------------------------------------------
/**
 * Obtains an instance of {@code Period} from a temporal amount.
 * <p>
 * This obtains a period based on the specified amount.
 * A {@code TemporalAmount} represents an  amount of time, which may be
 * date-based or time-based, which this factory extracts to a {@code Period}.
 * <p>
 * The conversion loops around the set of units from the amount and uses
 * the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS}
 * and {@link ChronoUnit#DAYS DAYS} units to create a period.
 * If any other units are found then an exception is thrown.
 * <p>
 * If the amount is a {@code ChronoPeriod} then it must use the ISO chronology.
 *
 * @param amount  the temporal amount to convert, not null
 * @return the equivalent period, not null
 * @throws DateTimeException if unable to convert to a {@code Period}
 * @throws ArithmeticException if the amount of years, months or days exceeds an int
 */
public static Period from(TemporalAmount amount) {
    if (amount instanceof Period) {
        return (Period) amount;
    }
    if (amount instanceof ChronoPeriod) {
        if (IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology()) == false) {
            throw new DateTimeException("Period requires ISO chronology: " + amount);
        }
    }
    Objects.requireNonNull(amount, "amount");
    int years = 0;
    int months = 0;
    int days = 0;
    for (TemporalUnit unit : amount.getUnits()) {
        long unitAmount = amount.get(unit);
        if (unit == ChronoUnit.YEARS) {
            years = Math.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.MONTHS) {
            months = Math.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.DAYS) {
            days = Math.toIntExact(unitAmount);
        } else {
            throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit);
        }
    }
    return create(years, months, days);
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) TemporalUnit(java.time.temporal.TemporalUnit) ChronoPeriod(java.time.chrono.ChronoPeriod)

Example 24 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project drools by kiegroup.

the class TypeUtil method formatValue.

public static String formatValue(final Object val, final boolean wrapForCodeUsage) {
    if (val instanceof String) {
        return formatString(val.toString(), wrapForCodeUsage);
    } else if (val instanceof LocalDate) {
        return formatDate((LocalDate) val, wrapForCodeUsage);
    } else if (val instanceof LocalTime || val instanceof OffsetTime) {
        return formatTimeString(TimeFunction.FEEL_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
    } else if (val instanceof LocalDateTime || val instanceof OffsetDateTime) {
        return formatDateTimeString(DateAndTimeFunction.FEEL_DATE_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
    } else if (val instanceof ZonedDateTime) {
        TemporalAccessor ta = (TemporalAccessor) val;
        ZoneId zone = ta.query(TemporalQueries.zone());
        if (!(zone instanceof ZoneOffset)) {
            // it is a ZoneRegion
            return formatDateTimeString(DateAndTimeFunction.REGION_DATETIME_FORMATTER.format((TemporalAccessor) val), wrapForCodeUsage);
        } else {
            return formatDateTimeString(DateAndTimeFunction.FEEL_DATE_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
        }
    } else if (val instanceof Duration) {
        return formatDuration((Duration) val, wrapForCodeUsage);
    } else if (val instanceof ChronoPeriod) {
        return formatPeriod((ChronoPeriod) val, wrapForCodeUsage);
    } else if (val instanceof TemporalAccessor) {
        TemporalAccessor ta = (TemporalAccessor) val;
        if (ta.query(TemporalQueries.localDate()) == null && ta.query(TemporalQueries.localTime()) != null && ta.query(TemporalQueries.zoneId()) != null) {
            return formatTimeString(TimeFunction.FEEL_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
        } else {
            return String.valueOf(val);
        }
    } else if (val instanceof List) {
        return formatList((List) val, wrapForCodeUsage);
    } else if (val instanceof Range) {
        return formatRange((Range) val, wrapForCodeUsage);
    } else if (val instanceof Map) {
        return formatContext((Map) val, wrapForCodeUsage);
    } else {
        return String.valueOf(val);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) ChronoPeriod(java.time.chrono.ChronoPeriod) TemporalAccessor(java.time.temporal.TemporalAccessor) LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) Duration(java.time.Duration) Range(org.kie.dmn.feel.runtime.Range) LocalDate(java.time.LocalDate) ZoneOffset(java.time.ZoneOffset) OffsetDateTime(java.time.OffsetDateTime) ZonedDateTime(java.time.ZonedDateTime) OffsetTime(java.time.OffsetTime) List(java.util.List) Map(java.util.Map)

Example 25 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project drools by kiegroup.

the class EvalHelper method compare.

/**
 * Compares left and right operands using the given predicate and returns TRUE/FALSE accordingly
 *
 * @param left
 * @param right
 * @param ctx
 * @param op
 * @return
 */
public static Boolean compare(Object left, Object right, EvaluationContext ctx, BiPredicate<Comparable, Comparable> op) {
    if (left == null || right == null) {
        return null;
    } else if (left instanceof ChronoPeriod && right instanceof ChronoPeriod) {
        // periods have special compare semantics in FEEL as it ignores "days". Only months and years are compared
        Long l = ComparablePeriod.toTotalMonths((ChronoPeriod) left);
        Long r = ComparablePeriod.toTotalMonths((ChronoPeriod) right);
        return op.test(l, r);
    } else if (left instanceof TemporalAccessor && right instanceof TemporalAccessor) {
        // Handle specific cases when both time / datetime
        TemporalAccessor l = (TemporalAccessor) left;
        TemporalAccessor r = (TemporalAccessor) right;
        if (BuiltInType.determineTypeFromInstance(left) == BuiltInType.TIME && BuiltInType.determineTypeFromInstance(right) == BuiltInType.TIME) {
            return op.test(valuet(l), valuet(r));
        } else if (BuiltInType.determineTypeFromInstance(left) == BuiltInType.DATE_TIME && BuiltInType.determineTypeFromInstance(right) == BuiltInType.DATE_TIME) {
            return op.test(valuedt(l, r.query(TemporalQueries.zone())), valuedt(r, l.query(TemporalQueries.zone())));
        }
    // fallback; continue:
    }
    // last fallback:
    if ((left instanceof String && right instanceof String) || (left instanceof Number && right instanceof Number) || (left instanceof Boolean && right instanceof Boolean) || (left instanceof Comparable && left.getClass().isAssignableFrom(right.getClass()))) {
        Comparable l = (Comparable) left;
        Comparable r = (Comparable) right;
        return op.test(l, r);
    }
    return null;
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) TemporalAccessor(java.time.temporal.TemporalAccessor) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

ChronoPeriod (java.time.chrono.ChronoPeriod)55 Test (org.testng.annotations.Test)26 Test (org.junit.Test)21 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)14 ChronoLocalDate (java.time.chrono.ChronoLocalDate)6 MinguoDate (java.time.chrono.MinguoDate)5 List (java.util.List)5 BigDecimal (java.math.BigDecimal)4 Period (java.time.Period)4 ChronoUnit (java.time.temporal.ChronoUnit)4 Temporal (java.time.temporal.Temporal)4 TemporalAccessor (java.time.temporal.TemporalAccessor)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Predicate (java.util.function.Predicate)4 Before (org.junit.Before)4 ComparablePeriod (org.kie.dmn.feel.lang.types.impl.ComparablePeriod)4 Duration (java.time.Duration)3 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)3 TemporalUnit (java.time.temporal.TemporalUnit)3