commit 5455acf2a61090aa5810eefff788dbf290719b68
parent 9e3ce9ba9850349e1a4bf222c66343df6173c845
Author: Dan Callaghan <djc@djc.id.au>
Date: Sun, 28 Mar 2010 21:47:01 +1000
also support unparse for datetime
Diffstat:
2 files changed, 37 insertions(+), 3 deletions(-)
diff --git a/src/main/java/au/com/miskinhill/rdftemplate/datatype/DateTimeDataType.java b/src/main/java/au/com/miskinhill/rdftemplate/datatype/DateTimeDataType.java
@@ -20,7 +20,7 @@ public class DateTimeDataType implements RDFDatatype {
instance = new DateTimeDataType();
}
- private final DateTimeFormatter parser = ISODateTimeFormat.dateTimeNoMillis();
+ private final DateTimeFormatter format = ISODateTimeFormat.dateTimeNoMillis().withOffsetParsed();
public DateTimeDataType() {
TypeMapper.getInstance().registerDatatype(this);
@@ -38,7 +38,7 @@ public class DateTimeDataType implements RDFDatatype {
@Override
public String unparse(Object value) {
- throw new UnsupportedOperationException();
+ return ((DateTime) value).toString(format);
}
@Override
@@ -64,7 +64,7 @@ public class DateTimeDataType implements RDFDatatype {
@Override
public DateTime parse(String lexicalForm) throws DatatypeFormatException {
try {
- return parser.parseDateTime(lexicalForm);
+ return format.parseDateTime(lexicalForm);
} catch (IllegalArgumentException e) {
throw new DatatypeFormatException(lexicalForm, this, "Parser barfed");
}
diff --git a/src/test/java/au/com/miskinhill/rdftemplate/datatype/DateTimeDataTypeUnitTest.java b/src/test/java/au/com/miskinhill/rdftemplate/datatype/DateTimeDataTypeUnitTest.java
@@ -0,0 +1,34 @@
+package au.com.miskinhill.rdftemplate.datatype;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.hp.hpl.jena.datatypes.RDFDatatype;
+
+public class DateTimeDataTypeUnitTest {
+
+ private RDFDatatype type;
+
+ @Before
+ public void setUp() {
+ type = new DateTimeDataType();
+ }
+
+ @Test
+ public void shouldParseDate() {
+ assertThat((DateTime) type.parse("2003-05-25T10:11:12+05:00"),
+ equalTo(new DateTime(2003, 5, 25, 10, 11, 12, 0, DateTimeZone.forOffsetHours(5))));
+ }
+
+ @Test
+ public void shouldUnparseDate() {
+ assertThat(type.unparse(new DateTime(2003, 5, 25, 10, 11, 12, 0, DateTimeZone.forOffsetHours(5))),
+ equalTo("2003-05-25T10:11:12+05:00"));
+ }
+
+}