use of org.apache.jena.sparql.util.DateTimeStruct in project jena by apache.
the class XSDFuncOp method dateTimeCast.
/** Cast a NodeValue to a date/time type (xsd dateTime, date, time, g*) according to F&O
* <a href="http://www.w3.org/TR/xpath-functions/#casting-to-datetimes">17.1.5 Casting to date and time types</a>
* Throws an exception on incorrect case.
*
* @throws ExprEvalTypeException
*/
public static NodeValue dateTimeCast(NodeValue nv, XSDDatatype xsd) {
if (nv.isString()) {
String s = nv.getString();
if (!xsd.isValid(s))
throw new ExprEvalTypeException("Invalid lexical form: '" + s + "' for " + xsd.getURI());
return NodeValue.makeNode(s, xsd);
}
if (!nv.hasDateTime())
throw new ExprEvalTypeException("Not a date/time type: " + nv);
XMLGregorianCalendar xsdDT = nv.getDateTime();
if (XSDDatatype.XSDdateTime.equals(xsd)) {
// ==> DateTime
if (nv.isDateTime())
return nv;
if (!nv.isDate())
throw new ExprEvalTypeException("Can't cast to XSD:dateTime: " + nv);
// DateTime with time 00:00:00 ... and timezone, if any
String tzStr = tzStrFromNV(nv);
String x = String.format("%04d-%02d-%02dT00:00:00%s", xsdDT.getYear(), xsdDT.getMonth(), xsdDT.getDay(), tzStr);
return NodeValue.makeNode(x, xsd);
}
if (XSDDatatype.XSDdate.equals(xsd)) {
// ==> Date
if (nv.isDate())
return nv;
if (!nv.isDateTime())
throw new ExprEvalTypeException("Can't cast to XSD:date: " + nv);
// Timezone
String tzStr = tzStrFromNV(nv);
String x = String.format("%04d-%02d-%02d%s", xsdDT.getYear(), xsdDT.getMonth(), xsdDT.getDay(), tzStr);
return NodeValue.makeNode(x, xsd);
}
if (XSDDatatype.XSDtime.equals(xsd)) {
// ==> time
if (nv.isTime())
return nv;
if (!nv.isDateTime())
throw new ExprEvalTypeException("Can't cast to XSD:time: " + nv);
// Careful formatting
DateTimeStruct dts = parseAnyDT(nv);
if (dts.timezone == null)
dts.timezone = "";
String x = String.format("%s:%s:%s%s", dts.hour, dts.minute, dts.second, dts.timezone);
return NodeValue.makeNode(x, xsd);
}
if (XSDDatatype.XSDgYear.equals(xsd)) {
// ==> Year
if (nv.isGYear())
return nv;
if (!nv.isDateTime() && !nv.isDate())
throw new ExprEvalTypeException("Can't cast to XSD:gYear: " + nv);
String x = String.format("%04d", xsdDT.getYear());
return NodeValue.makeNode(x, xsd);
}
if (XSDDatatype.XSDgYearMonth.equals(xsd)) {
// ==> YearMonth
if (nv.isGYearMonth())
return nv;
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());
return NodeValue.makeNode(x, xsd);
}
if (XSDDatatype.XSDgMonth.equals(xsd)) {
// ==> Month
if (nv.isGMonth())
return nv;
if (!nv.isDateTime() && !nv.isDate())
throw new ExprEvalTypeException("Can't cast to XSD:gMonth: " + nv);
String x = String.format("--%02d", xsdDT.getMonth());
return NodeValue.makeNode(x, xsd);
}
if (XSDDatatype.XSDgMonthDay.equals(xsd)) {
// ==> MonthDay
if (nv.isGMonthDay())
return nv;
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());
return NodeValue.makeNode(x, xsd);
}
if (XSDDatatype.XSDgDay.equals(xsd)) {
// Day
if (nv.isGDay())
return nv;
if (!nv.isDateTime() && !nv.isDate())
throw new ExprEvalTypeException("Can't cast to XSD:gDay: " + nv);
String x = String.format("---%02d", xsdDT.getDay());
return NodeValue.makeNode(x, xsd);
}
throw new ExprEvalTypeException("Can't case to <" + xsd.getURI() + ">: " + nv);
}
use of org.apache.jena.sparql.util.DateTimeStruct in project jena by apache.
the class XSDFuncOp method fixupDate.
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;
}
use of org.apache.jena.sparql.util.DateTimeStruct in project jena by apache.
the class TestDateTimeParsing method testGYear_3.
@Test
public void testGYear_3() {
DateTimeStruct dt = DateTimeStruct.parseGYear("2007+08:00");
check(dt, null, "2007", null, null, "+08:00");
}
use of org.apache.jena.sparql.util.DateTimeStruct in project jena by apache.
the class TestDateTimeParsing method testGDay_3.
@Test
public void testGDay_3() {
DateTimeStruct dt = DateTimeStruct.parseGDay("---31-08:00");
check(dt, null, null, null, "31", "-08:00");
}
use of org.apache.jena.sparql.util.DateTimeStruct in project jena by apache.
the class TestDateTimeParsing method testDT_10.
@Test
public void testDT_10() {
DateTimeStruct dt = DateTimeStruct.parseDateTime("2007-08-31T12:34:56");
check(dt, null, "2007", "08", "31", "12", "34", "56", null);
}
Aggregations