{
// http://www.w3.org/TR/xpath-functions/#casting-to-datetimes
if ( ! nv.hasDateTime() )
throw new ExprEvalTypeException("Not a date/time type: "+nv) ;
XSDDateTime 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
String x = String.format("%04d-%02d-%02dT00:00:00", xsdDT.getYears(), xsdDT.getMonths(),xsdDT.getDays()) ;
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) ;
String x = String.format("%04d-%02d-%02d", xsdDT.getYears(), xsdDT.getMonths(),xsdDT.getDays()) ;
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 foratting
DecimalFormat nf = new DecimalFormat("00.####") ;
nf.setDecimalSeparatorAlwaysShown(false) ;
String x = nf.format(xsdDT.getSeconds()) ;
x = String.format("%02d:%02d:%s", xsdDT.getHours(), xsdDT.getMinutes(),x) ;
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.getYears()) ;
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.getYears(), xsdDT.getMonths()) ;
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.getMonths()) ;
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.getMonths(), xsdDT.getDays()) ;
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.getDays()) ;
return NodeValue.makeNode(x, xsd) ;
}
throw new ExprEvalTypeException("Can't case to <"+xsd.getURI()+">: "+nv) ;
}