Search in sources :

Example 1 with TemporalParseException

use of org.neo4j.exceptions.TemporalParseException in project neo4j by neo4j.

the class TemporalValue method parse.

static <VALUE> VALUE parse(Class<VALUE> type, Pattern pattern, BiFunction<Matcher, Supplier<ZoneId>, VALUE> parser, CharSequence text, Supplier<ZoneId> defaultZone) {
    Matcher matcher = pattern.matcher(text);
    VALUE result = matcher.matches() ? parser.apply(matcher, defaultZone) : null;
    if (result == null) {
        throw new TemporalParseException("Text cannot be parsed to a " + valueName(type), text.toString(), 0);
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) TemporalParseException(org.neo4j.exceptions.TemporalParseException)

Example 2 with TemporalParseException

use of org.neo4j.exceptions.TemporalParseException in project neo4j by neo4j.

the class TemporalValue method parse.

static <VALUE> VALUE parse(Class<VALUE> type, Pattern pattern, BiFunction<Matcher, Supplier<ZoneId>, VALUE> parser, TextValue text, Supplier<ZoneId> defaultZone) {
    Matcher matcher = text.matcher(pattern);
    VALUE result = matcher != null && matcher.matches() ? parser.apply(matcher, defaultZone) : null;
    if (result == null) {
        throw new TemporalParseException("Text cannot be parsed to a " + valueName(type), text.stringValue(), 0);
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) TemporalParseException(org.neo4j.exceptions.TemporalParseException)

Example 3 with TemporalParseException

use of org.neo4j.exceptions.TemporalParseException in project neo4j by neo4j.

the class DateTimeValue method builder.

static DateTimeBuilder<DateTimeValue> builder(Supplier<ZoneId> defaultZone) {
    return new DateTimeBuilder<>(defaultZone) {

        @Override
        protected boolean supportsTimeZone() {
            return true;
        }

        @Override
        protected boolean supportsEpoch() {
            return true;
        }

        private final ZonedDateTime defaultZonedDateTime = ZonedDateTime.of(TemporalFields.year.defaultValue, TemporalFields.month.defaultValue, TemporalFields.day.defaultValue, TemporalFields.hour.defaultValue, TemporalFields.minute.defaultValue, TemporalFields.second.defaultValue, TemporalFields.nanosecond.defaultValue, timezone());

        @Override
        public DateTimeValue buildInternal() {
            boolean selectingDate = fields.containsKey(TemporalFields.date);
            boolean selectingTime = fields.containsKey(TemporalFields.time);
            boolean selectingDateTime = fields.containsKey(TemporalFields.datetime);
            boolean selectingEpoch = fields.containsKey(TemporalFields.epochSeconds) || fields.containsKey(TemporalFields.epochMillis);
            boolean selectingTimeZone;
            ZonedDateTime result;
            if (selectingDateTime) {
                AnyValue dtField = fields.get(TemporalFields.datetime);
                if (!(dtField instanceof TemporalValue)) {
                    throw new InvalidArgumentException(String.format("Cannot construct date time from: %s", dtField));
                }
                TemporalValue dt = (TemporalValue) dtField;
                LocalTime timePart = dt.getTimePart(defaultZone).toLocalTime();
                ZoneId zoneId = dt.getZoneId(defaultZone);
                result = ZonedDateTime.of(dt.getDatePart(), timePart, zoneId);
                selectingTimeZone = dt.supportsTimeZone();
            } else if (selectingEpoch) {
                if (fields.containsKey(TemporalFields.epochSeconds)) {
                    AnyValue epochField = fields.get(TemporalFields.epochSeconds);
                    if (!(epochField instanceof IntegralValue)) {
                        throw new InvalidArgumentException(String.format("Cannot construct date time from: %s", epochField));
                    }
                    IntegralValue epochSeconds = (IntegralValue) epochField;
                    result = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochSeconds.longValue() * 1000), timezone()));
                } else {
                    AnyValue epochField = fields.get(TemporalFields.epochMillis);
                    if (!(epochField instanceof IntegralValue)) {
                        throw new InvalidArgumentException(String.format("Cannot construct date time from: %s", epochField));
                    }
                    IntegralValue epochMillis = (IntegralValue) epochField;
                    result = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMillis.longValue()), timezone()));
                }
                selectingTimeZone = false;
            } else if (selectingTime || selectingDate) {
                LocalTime time;
                ZoneId zoneId;
                if (selectingTime) {
                    AnyValue timeField = fields.get(TemporalFields.time);
                    if (!(timeField instanceof TemporalValue)) {
                        throw new InvalidArgumentException(String.format("Cannot construct time from: %s", timeField));
                    }
                    TemporalValue t = (TemporalValue) timeField;
                    time = t.getTimePart(defaultZone).toLocalTime();
                    zoneId = t.getZoneId(defaultZone);
                    selectingTimeZone = t.supportsTimeZone();
                } else {
                    time = LocalTimeValue.DEFAULT_LOCAL_TIME;
                    zoneId = timezone();
                    selectingTimeZone = false;
                }
                LocalDate date;
                if (selectingDate) {
                    AnyValue dateField = fields.get(TemporalFields.date);
                    if (!(dateField instanceof TemporalValue)) {
                        throw new InvalidArgumentException(String.format("Cannot construct date from: %s", dateField));
                    }
                    TemporalValue t = (TemporalValue) dateField;
                    date = t.getDatePart();
                } else {
                    date = DateValue.DEFAULT_CALENDER_DATE;
                }
                result = ZonedDateTime.of(date, time, zoneId);
            } else {
                result = defaultZonedDateTime;
                selectingTimeZone = false;
            }
            if (fields.containsKey(TemporalFields.week) && !selectingDate && !selectingDateTime && !selectingEpoch) {
                // Be sure to be in the start of the week based year (which can be later than 1st Jan)
                result = result.with(IsoFields.WEEK_BASED_YEAR, safeCastIntegral(TemporalFields.year.name(), fields.get(TemporalFields.year), TemporalFields.year.defaultValue)).with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1).with(ChronoField.DAY_OF_WEEK, 1);
            }
            result = assignAllFields(result);
            if (timezone != null) {
                if (((selectingTime || selectingDateTime) && selectingTimeZone) || selectingEpoch) {
                    try {
                        result = result.withZoneSameInstant(timezone());
                    } catch (DateTimeParseException e) {
                        throw new TemporalParseException(e.getMessage(), e.getParsedString(), e.getErrorIndex(), e);
                    }
                } else {
                    result = result.withZoneSameLocal(timezone());
                }
            }
            return datetime(result);
        }

        @Override
        protected DateTimeValue selectDateTime(AnyValue datetime) {
            if (datetime instanceof DateTimeValue) {
                DateTimeValue value = (DateTimeValue) datetime;
                ZoneId zone = optionalTimezone();
                return zone == null ? value : new DateTimeValue(ZonedDateTime.of(value.temporal().toLocalDateTime(), zone));
            }
            if (datetime instanceof LocalDateTimeValue) {
                return new DateTimeValue(ZonedDateTime.of(((LocalDateTimeValue) datetime).temporal(), timezone()));
            }
            throw new UnsupportedTemporalUnitException("Cannot select datetime from: " + datetime);
        }
    };
}
Also used : LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) UnsupportedTemporalUnitException(org.neo4j.exceptions.UnsupportedTemporalUnitException) LocalDate(java.time.LocalDate) TemporalParseException(org.neo4j.exceptions.TemporalParseException) DateTimeParseException(java.time.format.DateTimeParseException) InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) ZonedDateTime(java.time.ZonedDateTime) AnyValue(org.neo4j.values.AnyValue)

Example 4 with TemporalParseException

use of org.neo4j.exceptions.TemporalParseException in project neo4j by neo4j.

the class DateTimeValue method parse.

private static DateTimeValue parse(Matcher matcher, Supplier<ZoneId> defaultZone) {
    LocalDateTime local = LocalDateTime.of(parseDate(matcher), optTime(matcher));
    String zoneName = matcher.group("zoneName");
    ZoneOffset offset = parseOffset(matcher);
    ZoneId zone;
    if (zoneName != null) {
        zone = parseZoneName(zoneName);
        if (offset != null) {
            try {
                if (!zone.getRules().isValidOffset(local, offset)) {
                    throw new InvalidArgumentException("Timezone and offset do not match: " + matcher.group());
                }
            } catch (ZoneRulesException e) {
                throw new TemporalParseException(e.getMessage(), e);
            }
        }
    } else if (offset != null) {
        zone = offset;
    } else {
        zone = defaultZone.get();
    }
    return new DateTimeValue(ZonedDateTime.ofLocal(local, zone, offset));
}
Also used : LocalDateTime(java.time.LocalDateTime) InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) ZoneRulesException(java.time.zone.ZoneRulesException) ZoneId(java.time.ZoneId) TemporalParseException(org.neo4j.exceptions.TemporalParseException) ZoneOffset(java.time.ZoneOffset)

Example 5 with TemporalParseException

use of org.neo4j.exceptions.TemporalParseException in project neo4j by neo4j.

the class TemporalValue method parse.

static <VALUE> VALUE parse(Class<VALUE> type, Pattern pattern, Function<Matcher, VALUE> parser, TextValue text) {
    Matcher matcher = text.matcher(pattern);
    VALUE result = matcher != null && matcher.matches() ? parser.apply(matcher) : null;
    if (result == null) {
        throw new TemporalParseException("Text cannot be parsed to a " + valueName(type), text.stringValue(), 0);
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) TemporalParseException(org.neo4j.exceptions.TemporalParseException)

Aggregations

TemporalParseException (org.neo4j.exceptions.TemporalParseException)6 Matcher (java.util.regex.Matcher)4 ZoneId (java.time.ZoneId)2 InvalidArgumentException (org.neo4j.exceptions.InvalidArgumentException)2 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeParseException (java.time.format.DateTimeParseException)1 ZoneRulesException (java.time.zone.ZoneRulesException)1 UnsupportedTemporalUnitException (org.neo4j.exceptions.UnsupportedTemporalUnitException)1 AnyValue (org.neo4j.values.AnyValue)1