Skip to content

Commit 13081d4

Browse files
committed
[feature] Add xs:dateTimeStamp implementation.
1 parent 12ae764 commit 13081d4

File tree

6 files changed

+127
-9
lines changed

6 files changed

+127
-9
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FunCurrentDateTime.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.exist.xquery.Profiler;
3333
import org.exist.xquery.XPathException;
3434
import org.exist.xquery.XQueryContext;
35-
import org.exist.xquery.value.DateTimeValue;
35+
import org.exist.xquery.value.DateTimeStampValue;
3636
import org.exist.xquery.value.FunctionReturnSequenceType;
3737
import org.exist.xquery.value.Item;
3838
import org.exist.xquery.value.Sequence;
@@ -48,12 +48,11 @@ public class FunCurrentDateTime extends Function {
4848
public final static FunctionSignature fnCurrentDateTime =
4949
new FunctionSignature(
5050
new QName("current-dateTime", Function.BUILTIN_FUNCTION_NS),
51-
"Returns the xs:dateTime (with timezone) that is current at some time " +
51+
"Returns the xs:dateTimeStamp (with timezone) that is current at some time " +
5252
"during the evaluation of a query or transformation in which " +
5353
"fn:current-dateTime() is executed.",
5454
null,
55-
//should be xs:dateTimeStamp, need to add support for DATE_TIME_STAMP
56-
new FunctionReturnSequenceType(Type.DATE_TIME,
55+
new FunctionReturnSequenceType(Type.DATE_TIME_STAMP,
5756
Cardinality.EXACTLY_ONE, "the date-time current " +
5857
"within query execution time span"));
5958

@@ -95,7 +94,7 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc
9594
{context.getProfiler().message(this, Profiler.START_SEQUENCES,
9695
"CONTEXT ITEM", contextItem.toSequence());}
9796
}
98-
Sequence result = new DateTimeValue(context.getCalendar());
97+
Sequence result = new DateTimeStampValue(context.getCalendar());
9998
if (isCalledAs("current-dateTime")) {
10099
// do nothing, result already in right form
101100
} else if (isCalledAs("current-date")) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.exist.xquery.value;
2+
3+
import org.exist.xquery.ErrorCodes;
4+
import org.exist.xquery.XPathException;
5+
6+
import javax.xml.XMLConstants;
7+
8+
import javax.xml.datatype.DatatypeConstants;
9+
import javax.xml.datatype.XMLGregorianCalendar;
10+
import javax.xml.namespace.QName;
11+
12+
public class DateTimeStampValue extends DateTimeValue {
13+
14+
private static final QName XML_SCHEMA_TYPE = new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "dateTimeStamp");
15+
16+
public DateTimeStampValue(XMLGregorianCalendar calendar) throws XPathException {
17+
super(calendar);
18+
checkValidTimezone();
19+
}
20+
21+
public DateTimeStampValue(String dateTime) throws XPathException {
22+
super(dateTime);
23+
checkValidTimezone();
24+
}
25+
26+
private void checkValidTimezone() throws XPathException {
27+
if(calendar.getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
28+
throw new XPathException(ErrorCodes.ERROR, "Unable to create xs:dateTimeStamp, timezone missing.");
29+
}
30+
}
31+
32+
@Override
33+
public AtomicValue convertTo(int requiredType) throws XPathException {
34+
switch (requiredType) {
35+
case Type.DATE_TIME_STAMP:
36+
return this;
37+
case Type.DATE_TIME:
38+
return new DateTimeValue(calendar);
39+
default: return
40+
super.convertTo(requiredType);
41+
}
42+
}
43+
44+
@Override
45+
public int getType() {
46+
return Type.DATE_TIME_STAMP;
47+
}
48+
49+
@Override
50+
protected QName getXMLSchemaType() {
51+
return XML_SCHEMA_TYPE;
52+
}
53+
}

exist-core/src/main/java/org/exist/xquery/value/DateTimeValue.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public AtomicValue convertTo(int requiredType) throws XPathException {
115115
case Type.ATOMIC:
116116
case Type.ITEM:
117117
return this;
118+
case Type.DATE_TIME_STAMP:
119+
return new DateTimeStampValue(calendar);
118120
case Type.DATE:
119121
return new DateValue(calendar);
120122
case Type.TIME:

exist-core/src/main/java/org/exist/xquery/value/Type.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public class Type {
9191
public final static int GDAY = 58;
9292
public final static int GYEARMONTH = 59;
9393
public final static int GMONTHDAY = 71;
94+
public final static int DATE_TIME_STAMP = 72;
9495
public final static int TOKEN = 60;
9596
public final static int NORMALIZED_STRING = 61;
9697
public final static int LANGUAGE = 62;
@@ -158,7 +159,7 @@ public class Type {
158159
defineSubType(ATOMIC, UNTYPED_ATOMIC);
159160

160161
// DATE_TIME sub-types
161-
//defineSubType(DATE_TIME, DATE_TIME_STAMP);
162+
defineSubType(DATE_TIME, DATE_TIME_STAMP);
162163

163164
// DURATION sub-types
164165
defineSubType(DURATION, DAY_TIME_DURATION);
@@ -285,8 +286,7 @@ public class Type {
285286
defineBuiltInType(HEX_BINARY, "xs:hexBinary");
286287
defineBuiltInType(NOTATION, "xs:NOTATION");
287288

288-
//TODO add handling for xs:dateTimeStamp
289-
//defineBuiltInType(DATE_TIME_STAMP, "xs:dateTimeStamp");
289+
defineBuiltInType(DATE_TIME_STAMP, "xs:dateTimeStamp");
290290
defineBuiltInType(DATE_TIME, "xs:dateTime");
291291
defineBuiltInType(DATE, "xs:date");
292292
defineBuiltInType(TIME, "xs:time");
@@ -355,7 +355,7 @@ public class Type {
355355
DAY_TIME_DURATION
356356
});
357357
definePrimitiveType(DATE_TIME, new int[] {
358-
//DATE_TIME_STAMP
358+
DATE_TIME_STAMP
359359
});
360360
definePrimitiveType(TIME);
361361
definePrimitiveType(DATE);

exist-core/src/main/java/org/exist/xquery/value/UntypedAtomicValue.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ TODO replace UntypedAtomicValue with something that can allow lazily reading tex
106106

107107
case Type.DATE_TIME:
108108
return new DateTimeValue(value);
109+
case Type.DATE_TIME_STAMP:
110+
return new DateTimeStampValue(value);
109111
case Type.TIME:
110112
return new TimeValue(value);
111113
case Type.DATE:
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* eXist-db Open Source Native XML Database
3+
* Copyright (C) 2001 The eXist-db Authors
4+
*
5+
6+
* http://www.exist-db.org
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
package org.exist.xquery.value;
23+
24+
import org.exist.xquery.XPathException;
25+
import org.junit.Test;
26+
27+
import static org.junit.Assert.assertEquals;
28+
29+
public class DateTimeStampTest extends AbstractTimeRelatedTestCase {
30+
31+
32+
@Test(expected = XPathException.class)
33+
public void crate1() throws XPathException {
34+
new DateTimeStampValue("2005-10-11T10:00:00");
35+
}
36+
37+
@Test
38+
public void convert1() throws XPathException {
39+
DateTimeValue dateTimeValue = new DateTimeValue("2005-10-11T10:00:00Z");
40+
AtomicValue value = dateTimeValue.convertTo(Type.DATE_TIME_STAMP);
41+
assertEquals(DateTimeStampValue.class, value.getClass());
42+
}
43+
44+
@Test(expected = XPathException.class)
45+
public void convert2() throws XPathException {
46+
DateTimeValue dateTimeValue = new DateTimeValue("2005-10-11T10:00:00");
47+
AtomicValue value = dateTimeValue.convertTo(Type.DATE_TIME_STAMP);
48+
assertEquals(DateTimeStampValue.class, value.getClass());
49+
}
50+
51+
@Test()
52+
public void getTimezone1() throws XPathException {
53+
DateTimeStampValue value = new DateTimeStampValue("2005-10-11T10:00:00+10:00");
54+
assertEquals(10 * 60, value.calendar.getTimezone());
55+
}
56+
57+
58+
59+
60+
61+
62+
}

0 commit comments

Comments
 (0)