use of org.neo4j.exceptions.UnsupportedTemporalUnitException in project neo4j by neo4j.
the class TemporalValue method getTruncatedDateAndTime.
static Pair<LocalDate, LocalTime> getTruncatedDateAndTime(TemporalUnit unit, TemporalValue input, String type) {
if (unit.isTimeBased() && !(input instanceof DateTimeValue || input instanceof LocalDateTimeValue)) {
throw new UnsupportedTemporalUnitException(String.format("Cannot truncate %s to %s with a time based unit.", input, type));
}
LocalDate localDate = input.getDatePart();
LocalTime localTime = input.hasTime() ? input.getLocalTimePart() : LocalTimeValue.DEFAULT_LOCAL_TIME;
LocalTime truncatedTime;
LocalDate truncatedDate;
if (unit.isDateBased()) {
truncatedDate = DateValue.truncateTo(localDate, unit);
truncatedTime = LocalTimeValue.DEFAULT_LOCAL_TIME;
} else {
truncatedDate = localDate;
truncatedTime = localTime.truncatedTo(unit);
}
return Pair.of(truncatedDate, truncatedTime);
}
use of org.neo4j.exceptions.UnsupportedTemporalUnitException 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.UnsupportedTemporalUnitException in project neo4j by neo4j.
the class TemporalValue method until.
@Override
public final long until(Temporal endExclusive, TemporalUnit unit) {
if (!(endExclusive instanceof TemporalValue)) {
throw new InvalidArgumentException("Can only compute durations between TemporalValues.");
}
TemporalValue from = this;
TemporalValue to = (TemporalValue) endExclusive;
if (unit.isTimeBased()) {
from = attachTime(from);
to = attachTime(to);
} else if (from.isSupported(ChronoField.SECOND_OF_DAY) && !to.isSupported(ChronoField.SECOND_OF_DAY)) {
to = attachTime(to);
} else if (to.isSupported(ChronoField.SECOND_OF_DAY) && !from.isSupported(ChronoField.SECOND_OF_DAY)) {
from = attachTime(from);
}
if (from.isSupported(ChronoField.MONTH_OF_YEAR) && !to.isSupported(ChronoField.MONTH_OF_YEAR)) {
to = attachDate(to, from.getDatePart());
} else if (to.isSupported(ChronoField.MONTH_OF_YEAR) && !from.isSupported(ChronoField.MONTH_OF_YEAR)) {
from = attachDate(from, to.getDatePart());
}
if (from.supportsTimeZone() && !to.supportsTimeZone()) {
to = attachTimeZone(to, from.getZoneId(from::getZoneOffset));
} else if (to.supportsTimeZone() && !from.supportsTimeZone()) {
from = attachTimeZone(from, to.getZoneId(to::getZoneOffset));
}
long until;
try {
until = from.temporal().until(to, unit);
} catch (UnsupportedTemporalTypeException e) {
throw new UnsupportedTemporalUnitException(e.getMessage(), e);
}
return until;
}
Aggregations