001: package net.sf.saxon.functions;
002:
003: import net.sf.saxon.expr.Expression;
004: import net.sf.saxon.expr.StaticContext;
005: import net.sf.saxon.expr.XPathContext;
006: import net.sf.saxon.om.Item;
007: import net.sf.saxon.trans.XPathException;
008: import net.sf.saxon.type.Type;
009: import net.sf.saxon.type.TypeHierarchy;
010: import net.sf.saxon.value.DateTimeValue;
011: import net.sf.saxon.value.DateValue;
012: import net.sf.saxon.value.SecondsDurationValue;
013: import net.sf.saxon.value.TimeValue;
014:
015: /**
016: * This class implements the XPath 2.0 functions
017: * current-date(), current-time(), and current-dateTime(), as
018: * well as the function implicit-timezone(). The value that is required
019: * is inferred from the type of result required.
020: */
021:
022: public class CurrentDateTime extends SystemFunction {
023:
024: /**
025: * preEvaluate: this method suppresses compile-time evaluation by doing nothing
026: * (because the value of the expression depends on the runtime context)
027: */
028:
029: public Expression preEvaluate(StaticContext env) {
030: return this ;
031: }
032:
033: /**
034: * Determine the dependencies
035: */
036:
037: public int getIntrinsicDependencies() {
038: // current date/time is part of the context, but it is fixed for a transformation, so
039: // we don't need to manage it as a dependency: expressions using it can be freely
040: // rearranged
041: return 0;
042: }
043:
044: /**
045: * Evaluate in a general context
046: */
047:
048: public Item evaluateItem(XPathContext context)
049: throws XPathException {
050: final DateTimeValue dt = DateTimeValue
051: .getCurrentDateTime(context);
052: final TypeHierarchy th = context.getNamePool()
053: .getTypeHierarchy();
054: final int targetType = getItemType(th).getPrimitiveType();
055: switch (targetType) {
056: case Type.DATE_TIME:
057: return dt;
058: case Type.DATE:
059: return (DateValue) dt.convert(Type.DATE, context);
060: case Type.TIME:
061: return (TimeValue) dt.convert(Type.TIME, context);
062: case Type.DAY_TIME_DURATION:
063: case Type.DURATION:
064: return dt.getComponent(Component.TIMEZONE);
065: default:
066: throw new IllegalArgumentException(
067: "Wrong target type for current date/time");
068: }
069: }
070:
071: /**
072: * Get the implicit timezone
073: */
074:
075: public static SecondsDurationValue getImplicitTimezone(
076: XPathContext context) throws XPathException {
077: DateTimeValue dt = DateTimeValue.getCurrentDateTime(context);
078: return (SecondsDurationValue) dt
079: .getComponent(Component.TIMEZONE);
080: }
081:
082: }
083:
084: //
085: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
086: // you may not use this file except in compliance with the License. You may obtain a copy of the
087: // License at http://www.mozilla.org/MPL/
088: //
089: // Software distributed under the License is distributed on an "AS IS" basis,
090: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
091: // See the License for the specific language governing rights and limitations under the License.
092: //
093: // The Original Code is: all this file.
094: //
095: // The Initial Developer of the Original Code is Michael H. Kay.
096: //
097: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
098: //
099: // Contributor(s): none.
100: //
|