private static NodeValue fixupDateOrDateTime(NodeValue nv) { if ( nv.isDateTime() ) return fixupDateTime(nv); if ( nv.isDate() ) return fixupDate(nv); throw new ARQInternalErrorException("Attempt to fixupDateOrDateTime on "+nv); }
private static NodeValue fixupDate(NodeValue nv) { DateTimeStruct dts = DateTimeStruct.parseDate(nv.asNode().getLiteralLexicalForm()) ; if ( dts.timezone != null ) return null ; dts.timezone = defaultTimezone ; nv = NodeValue.makeDate(dts.toString()) ; if ( !nv.isDate() ) throw new ARQInternalErrorException("Failed to reform an xsd:date") ; return nv ; }
public static NodeValue dtGetYear(NodeValue nv) { if ( nv.isDateTime() || nv.isDate() || nv.isGYear() || nv.isGYearMonth() ) { DateTimeStruct dts = parseAnyDT(nv) ; return NodeValue.makeNode(dts.year, XSDDatatype.XSDinteger) ; } throw new ExprEvalException("Not a year datatype") ; }
/** Strict {@literal F&O} handling of compare date(times). * But that means applying the "local" timezone if there is no TZ. * The data may have come from different timezones to the query. */ private static int compareDateTimeFO(NodeValue nv1, NodeValue nv2) { XMLGregorianCalendar dt1 = nv1.getDateTime() ; XMLGregorianCalendar dt2 = nv2.getDateTime() ; int x = compareXSDDateTime(dt1, dt2) ; if ( x == XSDDateTime.INDETERMINATE ) { NodeValue nv3 = (nv1.isDate()) ? fixupDate(nv1) : fixupDateTime(nv1) ; if ( nv3 != null ) { XMLGregorianCalendar dt3 = nv3.getDateTime() ; x = compareXSDDateTime(dt3, dt2) ; if ( x == XSDDateTime.INDETERMINATE ) throw new ARQInternalErrorException("Still get indeterminate comparison") ; return x ; } nv3 = (nv2.isDate()) ? fixupDate(nv2) : fixupDateTime(nv2) ; if ( nv3 != null ) { XMLGregorianCalendar dt3 = nv3.getDateTime() ; x = compareXSDDateTime(dt1, dt3) ; if ( x == XSDDateTime.INDETERMINATE ) throw new ARQInternalErrorException("Still get indeterminate comparison") ; return x ; } throw new ARQInternalErrorException("Failed to fixup dateTimes") ; } return x ; }
public static NodeValue dtGetDay(NodeValue nv) { if ( nv.isDateTime() || nv.isDate() || nv.isGMonthDay() || nv.isGDay() ) { DateTimeStruct dts = parseAnyDT(nv) ; return NodeValue.makeNode(dts.day, XSDDatatype.XSDinteger) ; } throw new ExprEvalException("Not a month datatype") ; }
public static NodeValue dtDateTime(NodeValue nv1, NodeValue nv2) { if ( ! nv1.isDate() ) throw new ExprEvalException("fn:dateTime: arg1: Not an xsd:date: "+nv1) ; if ( ! nv2.isTime() ) throw new ExprEvalException("fn:dateTime: arg2: Not an xsd:time: "+nv2) ; String lex1 = nv1.asNode().getLiteralLexicalForm() ; String lex2 = nv2.asNode().getLiteralLexicalForm() ; DateTimeStruct dts1 = DateTimeStruct.parseDate(lex1); DateTimeStruct dts2 = DateTimeStruct.parseTime(lex2); if ( dts1.hasTimezone() && dts2.hasTimezone() ) { if ( ! Objects.equals(dts1.timezone, dts2.timezone) ) throw new ExprEvalException("fn:dateTime: Two different timezones: ("+nv1+", "+nv2+")"); } // Merge the date into the time. There are zero or one timezones, or two the same by this point. dts2.year = dts1.year; dts2.month = dts1.month; dts2.day = dts1.day; dts2.timezone = ( dts1.timezone != null ) ? dts1.timezone : dts2.timezone; String lex = dts2.toString(); return NodeValue.makeDateTime(lex); }
public static NodeValue dtGetMonth(NodeValue nv) { if ( nv.isDateTime() || nv.isDate() || nv.isGYearMonth() || nv.isGMonth() || nv.isGMonthDay() ) { DateTimeStruct dts = parseAnyDT(nv) ; return NodeValue.makeNode(dts.month, XSDDatatype.XSDinteger) ; } throw new ExprEvalException("Not a month datatype") ; }
public boolean hasDateTime() { return isDateTime() || isDate() || isTime() || isGYear() || isGYearMonth() || isGMonth() || isGMonthDay() || isGDay() ; } public boolean isDateTime() { return false ; }
@Test public void testDateTime1() { Calendar cal = new GregorianCalendar(); cal.setTimeZone(TimeZone.getTimeZone("GMT")); // Clear/ set all fields (milliseconds included). cal.setTimeInMillis(0); cal.set(2005, 01, 18, 20, 39, 10); // NB Months from 0, not 1 NodeValue v = NodeValue.makeDateTime(cal); assertTrue("Not a dateTime: " + v, v.isDateTime()); assertFalse("A date: " + v, v.isDate()); // DateTimes always have nodes because we used that to parse the thing. }
@Test public void testDate1() { Calendar cal = new GregorianCalendar(); cal.setTimeZone(TimeZone.getTimeZone("GMT")); // Clear/ set all fields (milliseconds included). cal.setTimeInMillis(0); // NB Months from 0, not 1 // For a date, must be time = 00:00:00 cal.set(2005, 01, 18, 0, 0, 0); NodeValue v = NodeValue.makeDate(cal); assertTrue("Not a date: " + v, v.isDate()); assertFalse("A dateTime: " + v, v.isDateTime()); // DateTimes always have nodes because we used that to parse the thing. }
@Test public void testDate5() { boolean b = NodeValue.VerboseWarnings; try { NodeValue.VerboseWarnings = false; // Illegal lexical for a date. NodeValue v = NodeValue.makeNode("2005-02-18T20:39:10Z", XSDDatatype.XSDdate); assertFalse("Datetime!: " + v, v.isDateTime()); assertFalse("Date!: " + v, v.isDate()); } finally { NodeValue.VerboseWarnings = b; } }
@Test public void testDateTime5() { boolean b = NodeValue.VerboseWarnings; try { NodeValue.VerboseWarnings = false; // Illegal lexical for a dateTime. NodeValue v = NodeValue.makeNode("2005-02-18", XSDDatatype.XSDdateTime); assertFalse("Date!: " + v, v.isDate()); assertFalse("Datetime!: " + v, v.isDateTime()); } finally { NodeValue.VerboseWarnings = b; } }
if ( !nv.isDate() ) throw new ExprEvalTypeException("Can't cast to XSD:dateTime: " + nv) ; if ( nv.isDate() ) return nv ; if ( !nv.isDateTime() ) if ( !nv.isDateTime() && !nv.isDate() ) throw new ExprEvalTypeException("Can't cast to XSD:gYear: " + nv) ; String x = String.format("%04d", xsdDT.getYear()) ; if ( !nv.isDateTime() && !nv.isDate() ) throw new ExprEvalTypeException("Can't cast to XSD:gYearMonth: " + nv) ; String x = String.format("%04d-%02d", xsdDT.getYear(), xsdDT.getMonth()) ; if ( !nv.isDateTime() && !nv.isDate() ) throw new ExprEvalTypeException("Can't cast to XSD:gMonth: " + nv) ; String x = String.format("--%02d", xsdDT.getMonth()) ; if ( !nv.isDateTime() && !nv.isDate() ) throw new ExprEvalTypeException("Can't cast to XSD:gMonthDay: " + nv) ; String x = String.format("--%02d-%02d", xsdDT.getMonth(), xsdDT.getDay()) ; if ( !nv.isDateTime() && !nv.isDate() ) throw new ExprEvalTypeException("Can't cast to XSD:gDay: " + nv) ; String x = String.format("---%02d", xsdDT.getDay()) ;
@Test public void testDateTime4() { Calendar cal1 = new GregorianCalendar(); cal1.setTimeZone(TimeZone.getTimeZone("GMT")); // Clear/ set all fields (milliseconds included). cal1.setTimeInMillis(0); cal1.set(2005, 01, 18, 20, 39, 10); // NB Months from 0, not 1 NodeValue v = NodeValue.makeNode("2005-02-18T20:39:10Z", XSDDatatype.XSDdateTime); assertTrue("Not a dateTime: " + v, v.isDateTime()); assertFalse("A date: " + v, v.isDate()); assertTrue("Not a node: " + v, v.hasNode()); Calendar cal2 = v.getDateTime().toGregorianCalendar(); assertEquals("Not equal: " + v, 0, cal1.compareTo(cal2)); }
@Test public void testDate4() { Calendar cal1 = new GregorianCalendar(); cal1.setTimeZone(TimeZone.getTimeZone("GMT")); // Clear/ set all fields (milliseconds included). cal1.setTimeInMillis(0); // Must be ",0,0,0" cal1.set(2005, 01, 18, 0, 0, 0); // NB Months from 0, not 1 NodeValue v = NodeValue.makeNode("2005-02-18Z", XSDDatatype.XSDdate); assertTrue("Not a date: " + v, v.isDate()); assertFalse("A dateTime: " + v, v.isDateTime()); assertTrue("Not a node: " + v, v.hasNode()); Calendar cal2 = v.getDateTime().toGregorianCalendar(); assertEquals("Not equal: " + v, 0, cal1.compareTo(cal2)); }
@Test public void testDateTime2() { NodeValue v = NodeValue.makeNodeDateTime("2005-02-18T20:39:10Z"); assertTrue("Not a dateTime: " + v, v.isDateTime()); assertFalse("A date: " + v, v.isDate()); assertTrue("Not a node: " + v, v.hasNode()); }
@Test public void testDate2() { NodeValue v = NodeValue.makeNodeDate("2005-02-18"); assertTrue("Not a date: " + v, v.isDate()); assertFalse("A dateTime: " + v, v.isDateTime()); assertTrue("Not a node: " + v, v.hasNode()); }
return null; if(!nv1.isDateTime() && !nv1.isDate() && !nv1.isTime()) throw new ExprEvalException("Not a valid date, datetime or time:"+nv1);
private static ValueSpaceClassification classifyValueSpace(NodeValue nv) { if ( nv.isNumber() ) return VSPACE_NUM ; if ( nv.isDateTime() ) return VSPACE_DATETIME ; if ( nv.isString()) return VSPACE_STRING ; if ( nv.isBoolean()) return VSPACE_BOOLEAN ; if ( ! nv.isLiteral() ) return VSPACE_NODE ; if ( ! SystemARQ.ValueExtensions ) return VSPACE_UNKNOWN ; // Datatypes and their value spaces that are an extension of strict SPARQL. if ( nv.isDate() ) return VSPACE_DATE ; if ( nv.isTime() ) return VSPACE_TIME ; if ( nv.isDuration() ) return VSPACE_DURATION ; if ( nv.isGYear() ) return VSPACE_G_YEAR ; if ( nv.isGYearMonth() ) return VSPACE_G_YEARMONTH ; if ( nv.isGMonth() ) return VSPACE_G_MONTH ; if ( nv.isGMonthDay() ) return VSPACE_G_MONTHDAY ; if ( nv.isGDay() ) return VSPACE_G_DAY ; if ( nv.isSortKey() ) return VSPACE_SORTKEY ; // Already a literal by this point. if ( NodeUtils.hasLang(nv.asNode()) ) return VSPACE_LANG ; return VSPACE_UNKNOWN ; }
private static DateTimeStruct parseAnyDT(NodeValue nv) { String lex = nv.asNode().getLiteralLexicalForm() ; if ( nv.isDateTime() ) return DateTimeStruct.parseDateTime(lex) ; if ( nv.isDate() ) return DateTimeStruct.parseDate(lex) ; if ( nv.isGYear() ) return DateTimeStruct.parseGYear(lex) ; if ( nv.isGYearMonth() ) return DateTimeStruct.parseGYearMonth(lex) ; if ( nv.isGMonth() ) return DateTimeStruct.parseGMonth(lex) ; if ( nv.isGMonthDay() ) return DateTimeStruct.parseGMonthDay(lex) ; if ( nv.isGDay() ) return DateTimeStruct.parseGDay(lex) ; if ( nv.isTime() ) return DateTimeStruct.parseTime(lex) ; return null ; }