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;
}
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;
}
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);
}
};
}
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));
}
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;
}
Aggregations