Skip to content

Commit

Permalink
eclipse-rdf4jGH-4104: fix issue with Instant, turn it into datetimest…
Browse files Browse the repository at this point in the history
…amp value

Signed-off-by:Bart Hanssens <[email protected]>
  • Loading branch information
barthanssens committed Sep 2, 2022
1 parent c607df2 commit 64646ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static java.lang.Math.abs;
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
Expand Down Expand Up @@ -522,7 +523,7 @@ static class TemporalAccessorLiteral extends AbstractLiteral {
private static final ChronoField[] FIELDS = {
YEAR, MONTH_OF_YEAR, DAY_OF_MONTH,
HOUR_OF_DAY, MINUTE_OF_HOUR, SECOND_OF_MINUTE, NANO_OF_SECOND,
OFFSET_SECONDS
OFFSET_SECONDS, INSTANT_SECONDS
};

private static final DateTimeFormatter LOCAL_TIME_FORMATTER = new DateTimeFormatterBuilder()
Expand Down Expand Up @@ -584,6 +585,12 @@ static class TemporalAccessorLiteral extends AbstractLiteral {

.toFormatter();

private static final DateTimeFormatter DATETIMESTAMP_FORMATTER = new DateTimeFormatterBuilder()

.appendInstant()

.toFormatter();

private static final DateTimeFormatter DASH_FORMATTER = new DateTimeFormatterBuilder()

.appendLiteral("--")
Expand Down Expand Up @@ -620,13 +627,16 @@ private static Map<Integer, CoreDatatype.XSD> datatypes() {
int time = key(HOUR_OF_DAY, MINUTE_OF_HOUR, SECOND_OF_MINUTE);
int nano = key(NANO_OF_SECOND);
int zone = key(OFFSET_SECONDS);
int instant = key(INSTANT_SECONDS);

Map<Integer, CoreDatatype.XSD> datatypes = new HashMap<>();

datatypes.put(date + time, CoreDatatype.XSD.DATETIME);
datatypes.put(date + time + nano, CoreDatatype.XSD.DATETIME);
datatypes.put((date + time + zone), CoreDatatype.XSD.DATETIME);
datatypes.put((date + time + nano + zone), CoreDatatype.XSD.DATETIME);
datatypes.put((date + time + instant + zone), CoreDatatype.XSD.DATETIME);
datatypes.put((date + time + instant + nano + zone), CoreDatatype.XSD.DATETIME);
datatypes.put((instant + nano), CoreDatatype.XSD.DATETIMESTAMP);

datatypes.put(time, CoreDatatype.XSD.TIME);
datatypes.put(time + nano, CoreDatatype.XSD.TIME);
Expand All @@ -649,6 +659,7 @@ private static Map<CoreDatatype.XSD, DateTimeFormatter> formatters() {

final Map<CoreDatatype.XSD, DateTimeFormatter> formatters = new EnumMap<>(CoreDatatype.XSD.class);

formatters.put(CoreDatatype.XSD.DATETIMESTAMP, DATETIMESTAMP_FORMATTER);
formatters.put(CoreDatatype.XSD.DATETIME, DATETIME_FORMATTER);
formatters.put(CoreDatatype.XSD.TIME, OFFSET_TIME_FORMATTER);
formatters.put(CoreDatatype.XSD.DATE, OFFSET_DATE_FORMATTER);
Expand Down Expand Up @@ -705,18 +716,17 @@ private static int key(Predicate<ChronoField> include, ChronoField... fields) {
private final CoreDatatype.XSD datatype;

TemporalAccessorLiteral(TemporalAccessor value) {

this.value = value;

datatype = DATATYPES.get(key(value));
datatype = DATATYPES.get(key(this.value));

if (datatype == null) {
throw new IllegalArgumentException(String.format(
"value <%s> cannot be represented by an XML Schema date/time datatype", value
"value <%s> cannot be represented by an XML Schema date/time datatype", this.value
));
}

this.label = FORMATTERS.get(datatype).format(value);
this.label = FORMATTERS.get(datatype).format(this.value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.Date;
Expand Down Expand Up @@ -507,6 +508,18 @@ public void testTemporalAccessorLiteralNull() {
.hasMessageContaining("value may not be null");

}

@Test
public void testTemporalAccessorLiteralInstant() {
final Instant value = Instant.ofEpochSecond(1234567890, 12);
Literal literal = literal(value);

assertThat(literal).isNotNull();
assertThat(literal.temporalAccessorValue()).isEqualTo(value);
assertThat(literal.getLabel()).isEqualTo(value.toString());
assertThat(literal.getDatatype()).isEqualTo(XSD.DATETIMESTAMP);
assertThat(literal.getCoreDatatype()).isEqualTo(CoreDatatype.XSD.DATETIMESTAMP);
}

@Test
public void testTriple() {
Expand Down

0 comments on commit 64646ce

Please sign in to comment.