KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > CurrentDateTime


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.StaticContext;
4 import net.sf.saxon.expr.XPathContext;
5 import net.sf.saxon.om.Item;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.type.Type;
8 import net.sf.saxon.value.DateTimeValue;
9 import net.sf.saxon.value.DateValue;
10 import net.sf.saxon.value.SecondsDurationValue;
11 import net.sf.saxon.value.TimeValue;
12
13 /**
14 * This class implements the XPath 2.0 functions
15  * current-date(), current-time(), and current-dateTime(), as
16  * well as the function implicit-timezone(). The value that is required
17  * is inferred from the type of result required.
18 */

19
20
21 public class CurrentDateTime extends SystemFunction {
22
23     /**
24     * preEvaluate: this method suppresses compile-time evaluation by doing nothing
25     * (because the value of the expression depends on the runtime context)
26     */

27
28     public Expression preEvaluate(StaticContext env) {
29         return this;
30     }
31
32     /**
33     * Determine the dependencies
34     */

35
36     public int getIntrinsicDependencies() {
37         // current date/time is part of the context, but it is fixed for a transformation, so
38
// we don't need to manage it as a dependency: expressions using it can be freely
39
// rearranged
40
return 0;
41     }
42
43     /**
44     * Evaluate in a general context
45     */

46
47     public Item evaluateItem(XPathContext context) throws XPathException {
48         DateTimeValue dt = DateTimeValue.getCurrentDateTime(context);
49         int targetType = getItemType().getPrimitiveType();
50         switch (targetType) {
51             case Type.DATE_TIME:
52                 return dt;
53             case Type.DATE:
54                 return (DateValue)dt.convert(Type.DATE, context);
55             case Type.TIME:
56                 return (TimeValue)dt.convert(Type.TIME, context);
57             case Type.DAY_TIME_DURATION:
58             case Type.DURATION:
59                 return dt.getComponent(Component.TIMEZONE);
60             default:
61                 throw new IllegalArgumentException JavaDoc("Wrong target type for current date/time");
62         }
63     }
64
65     /**
66      * Get the implicit timezone
67      */

68
69     public static SecondsDurationValue getImplicitTimezone(XPathContext context) throws XPathException {
70         DateTimeValue dt = DateTimeValue.getCurrentDateTime(context);
71         return (SecondsDurationValue)dt.getComponent(Component.TIMEZONE);
72     }
73
74 }
75
76
77
78
79 //
80
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
81
// you may not use this file except in compliance with the License. You may obtain a copy of the
82
// License at http://www.mozilla.org/MPL/
83
//
84
// Software distributed under the License is distributed on an "AS IS" basis,
85
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
86
// See the License for the specific language governing rights and limitations under the License.
87
//
88
// The Original Code is: all this file.
89
//
90
// The Initial Developer of the Original Code is Michael H. Kay.
91
//
92
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
93
//
94
// Contributor(s): none.
95
//
96
Popular Tags