Search in sources :

Example 16 with TemporalUnit

use of java.time.temporal.TemporalUnit in project neo4j by neo4j.

the class TemporalValue method updateFieldMapWithConflictingSubseconds.

static <TEMP extends Temporal, VALUE> VALUE updateFieldMapWithConflictingSubseconds(MapValue fields, TemporalUnit unit, TEMP temporal, BiFunction<MapValue, TEMP, VALUE> mapFunction) {
    boolean conflictingMilliSeconds = unit == ChronoUnit.MILLIS && (fields.containsKey("microsecond") || fields.containsKey("nanosecond"));
    boolean conflictingMicroSeconds = unit == ChronoUnit.MICROS && fields.containsKey("nanosecond");
    if (conflictingMilliSeconds) {
        AnyValue millis = Values.intValue(temporal.get(ChronoField.MILLI_OF_SECOND));
        AnyValue micros = fields.get("microsecond");
        AnyValue nanos = fields.get("nanosecond");
        int newNanos = validNano(millis, micros, nanos);
        TEMP newTemporal = (TEMP) temporal.with(ChronoField.NANO_OF_SECOND, newNanos);
        MapValue filtered = fields.filter((k, ignore) -> !k.equals("microsecond") && !k.equals("nanosecond"));
        return mapFunction.apply(filtered, newTemporal);
    } else if (conflictingMicroSeconds) {
        AnyValue micros = Values.intValue(temporal.get(ChronoField.MICRO_OF_SECOND));
        AnyValue nanos = fields.get("nanosecond");
        int newNanos = validNano(null, micros, nanos);
        TEMP newTemporal = (TEMP) temporal.with(ChronoField.NANO_OF_SECOND, newNanos);
        MapValue filtered = fields.filter((k, ignore) -> !k.equals("nanosecond"));
        return mapFunction.apply(filtered, newTemporal);
    } else {
        return mapFunction.apply(fields, temporal);
    }
}
Also used : ChronoField(java.time.temporal.ChronoField) AnyValue(org.neo4j.values.AnyValue) DateTimeValue.parseZoneName(org.neo4j.values.storable.DateTimeValue.parseZoneName) ZonedDateTime(java.time.ZonedDateTime) TemporalUnit(java.time.temporal.TemporalUnit) BiFunction(java.util.function.BiFunction) LocalDateTime(java.time.LocalDateTime) TemporalQuery(java.time.temporal.TemporalQuery) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ValueRange(java.time.temporal.ValueRange) Matcher(java.util.regex.Matcher) TemporalParseException(org.neo4j.exceptions.TemporalParseException) Map(java.util.Map) LocalTime(java.time.LocalTime) TemporalAmount(java.time.temporal.TemporalAmount) MapValue(org.neo4j.values.virtual.MapValue) ZoneOffset(java.time.ZoneOffset) StructureBuilder(org.neo4j.values.StructureBuilder) IntegralValue.safeCastIntegral(org.neo4j.values.storable.IntegralValue.safeCastIntegral) LocalDateTimeValue.localDateTime(org.neo4j.values.storable.LocalDateTimeValue.localDateTime) HashFunction(org.neo4j.hashing.HashFunction) DateTimeException(java.time.DateTimeException) OffsetTime(java.time.OffsetTime) EnumMap(java.util.EnumMap) UnsupportedTemporalUnitException(org.neo4j.exceptions.UnsupportedTemporalUnitException) TemporalField(java.time.temporal.TemporalField) Set(java.util.Set) DateTimeValue.datetime(org.neo4j.values.storable.DateTimeValue.datetime) TimeValue.time(org.neo4j.values.storable.TimeValue.time) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException) ZoneId(java.time.ZoneId) InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) NO_NUMBER(org.neo4j.values.storable.NumberType.NO_NUMBER) Objects(java.util.Objects) ChronoZonedDateTime(java.time.chrono.ChronoZonedDateTime) ChronoUnit(java.time.temporal.ChronoUnit) IsoFields(java.time.temporal.IsoFields) LocalDate(java.time.LocalDate) Pattern(java.util.regex.Pattern) Pair(org.neo4j.internal.helpers.collection.Pair) Temporal(java.time.temporal.Temporal) TemporalAdjuster(java.time.temporal.TemporalAdjuster) ArithmeticException(org.neo4j.exceptions.ArithmeticException) AnyValue(org.neo4j.values.AnyValue) MapValue(org.neo4j.values.virtual.MapValue)

Example 17 with TemporalUnit

use of java.time.temporal.TemporalUnit in project perun by CESNET.

the class Utils method prepareGracePeriodDate.

/**
 * Prepares grace period date by values from given matcher.
 * @param matcher matcher
 * @return pair of field(ChronoUnit.YEARS, ChronoUnit.MONTHS, ChronoUnit.DAYS) and amount
 * @throws InternalErrorException when given matcher contains invalid data
 * @throws IllegalArgumentException when matcher does not match gracePeriod format
 */
public static Pair<Integer, TemporalUnit> prepareGracePeriodDate(Matcher matcher) {
    if (!matcher.matches()) {
        throw new IllegalArgumentException("Wrong format of gracePeriod.");
    }
    String countString = matcher.group(1);
    int amount = Integer.valueOf(countString);
    TemporalUnit field;
    String dmyString = matcher.group(2);
    switch(dmyString) {
        case "d":
            field = ChronoUnit.DAYS;
            break;
        case "m":
            field = ChronoUnit.MONTHS;
            break;
        case "y":
            field = ChronoUnit.YEARS;
            break;
        default:
            throw new InternalErrorException("Wrong format of gracePeriod.");
    }
    return new Pair<>(amount, field);
}
Also used : TemporalUnit(java.time.temporal.TemporalUnit) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IllegalArgumentException(cz.metacentrum.perun.core.api.exceptions.IllegalArgumentException) Pair(cz.metacentrum.perun.core.api.Pair)

Example 18 with TemporalUnit

use of java.time.temporal.TemporalUnit in project j2objc by google.

the class TCKDuration method factory_from_TemporalAmount_DaysNanos.

@Test
public void factory_from_TemporalAmount_DaysNanos() {
    TemporalAmount amount = new TemporalAmount() {

        @Override
        public long get(TemporalUnit unit) {
            if (unit == DAYS) {
                return 23;
            } else {
                return 45;
            }
        }

        @Override
        public List<TemporalUnit> getUnits() {
            List<TemporalUnit> list = new ArrayList<>();
            list.add(DAYS);
            list.add(NANOS);
            return list;
        }

        @Override
        public Temporal addTo(Temporal temporal) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Temporal subtractFrom(Temporal temporal) {
            throw new UnsupportedOperationException();
        }
    };
    Duration t = Duration.from(amount);
    assertEquals(t.getSeconds(), 23 * 86400);
    assertEquals(t.getNano(), 45);
}
Also used : Temporal(java.time.temporal.Temporal) TemporalUnit(java.time.temporal.TemporalUnit) TemporalAmount(java.time.temporal.TemporalAmount) ArrayList(java.util.ArrayList) Duration(java.time.Duration) Test(org.junit.Test)

Example 19 with TemporalUnit

use of java.time.temporal.TemporalUnit in project j2objc by google.

the class TCKDuration method factory_from_TemporalAmount_Minutes_tooBig.

@Test(expected = ArithmeticException.class)
public void factory_from_TemporalAmount_Minutes_tooBig() {
    TemporalAmount amount = new TemporalAmount() {

        @Override
        public long get(TemporalUnit unit) {
            return (Long.MAX_VALUE / 60) + 2;
        }

        @Override
        public List<TemporalUnit> getUnits() {
            return Collections.<TemporalUnit>singletonList(MINUTES);
        }

        @Override
        public Temporal addTo(Temporal temporal) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Temporal subtractFrom(Temporal temporal) {
            throw new UnsupportedOperationException();
        }
    };
    Duration.from(amount);
}
Also used : Temporal(java.time.temporal.Temporal) TemporalUnit(java.time.temporal.TemporalUnit) TemporalAmount(java.time.temporal.TemporalAmount) Test(org.junit.Test)

Example 20 with TemporalUnit

use of java.time.temporal.TemporalUnit in project j2objc by google.

the class TCKDuration method test_duration_getUnits.

// -----------------------------------------------------------------------
@Test()
public void test_duration_getUnits() {
    Duration duration = Duration.ofSeconds(5000, 1000);
    List<TemporalUnit> units = duration.getUnits();
    assertEquals("Period.getUnits length", units.size(), 2);
    assertTrue("Period.getUnits contains ChronoUnit.SECONDS", units.contains(ChronoUnit.SECONDS));
    assertTrue("contains ChronoUnit.NANOS", units.contains(ChronoUnit.NANOS));
}
Also used : TemporalUnit(java.time.temporal.TemporalUnit) Duration(java.time.Duration) Test(org.junit.Test)

Aggregations

TemporalUnit (java.time.temporal.TemporalUnit)60 LocalDate (java.time.LocalDate)18 Test (org.junit.Test)18 Test (org.testng.annotations.Test)17 Temporal (java.time.temporal.Temporal)16 TemporalAmount (java.time.temporal.TemporalAmount)15 Chronology (java.time.chrono.Chronology)12 IsoChronology (java.time.chrono.IsoChronology)12 ArrayList (java.util.ArrayList)12 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)6 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)6 DateTimeException (java.time.DateTimeException)6 HijrahChronology (java.time.chrono.HijrahChronology)6 JapaneseChronology (java.time.chrono.JapaneseChronology)6 MinguoChronology (java.time.chrono.MinguoChronology)6 ThaiBuddhistChronology (java.time.chrono.ThaiBuddhistChronology)6 Matcher (java.util.regex.Matcher)6 Pattern (java.util.regex.Pattern)6 Duration (java.time.Duration)5 ChronoLocalDate (java.time.chrono.ChronoLocalDate)4