Search in sources :

Example 11 with DateTimeStruct

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);
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) DateTimeStruct(org.apache.jena.sparql.util.DateTimeStruct)

Example 12 with DateTimeStruct

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;
}
Also used : DateTimeStruct(org.apache.jena.sparql.util.DateTimeStruct) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException)

Example 13 with DateTimeStruct

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");
}
Also used : DateTimeStruct(org.apache.jena.sparql.util.DateTimeStruct) Test(org.junit.Test)

Example 14 with DateTimeStruct

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");
}
Also used : DateTimeStruct(org.apache.jena.sparql.util.DateTimeStruct) Test(org.junit.Test)

Example 15 with DateTimeStruct

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);
}
Also used : DateTimeStruct(org.apache.jena.sparql.util.DateTimeStruct) Test(org.junit.Test)

Aggregations

DateTimeStruct (org.apache.jena.sparql.util.DateTimeStruct)28 Test (org.junit.Test)19 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)1 Node (org.apache.jena.graph.Node)1