[OLINGO-796] Add support for java.sql.time

This commit is contained in:
Christian Holzer 2015-12-08 16:01:35 +01:00
parent 5004f4b1ff
commit a47e9f6ad2
3 changed files with 46 additions and 4 deletions

View File

@ -18,6 +18,7 @@
*/
package org.apache.olingo.commons.core.edm.primitivetype;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
@ -140,6 +141,8 @@ public final class EdmDateTimeOffset extends SingletonPrimitiveType {
Timestamp timestamp = new Timestamp(dateTimeValue.getTimeInMillis());
timestamp.setNanos(nanoSeconds);
return returnType.cast(timestamp);
} else if(returnType.isAssignableFrom(Time.class)) {
return returnType.cast(new Time(dateTimeValue.getTimeInMillis()));
} else {
throw new ClassCastException("unsupported return type " + returnType.getSimpleName());
}
@ -202,7 +205,10 @@ public final class EdmDateTimeOffset extends SingletonPrimitiveType {
*/
protected static <T> Calendar createDateTime(final T value) throws EdmPrimitiveTypeException {
Calendar dateTimeValue;
if (value instanceof Date) {
if(value instanceof Time) {
dateTimeValue = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
dateTimeValue.setTimeInMillis(((Time) value).getTime());
} else if (value instanceof Date) {
// Although java.util.Date, as stated in its documentation,
// "is intended to reflect coordinated universal time (UTC)",
// its getName() method uses the default time zone. And so do we.

View File

@ -21,6 +21,7 @@ package org.apache.olingo.commons.core.edm.primitivetype;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
@ -75,7 +76,11 @@ public class EdmDateTimeOffsetTest extends PrimitiveTypeBaseTest {
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(millis, null, null, 3, null, null));
assertEquals("1969-12-31T23:59:59.9Z", instance.valueToString(-100L, null, null, 1, null, null));
assertEquals("1969-12-31T23:59:59.98Z", instance.valueToString(-20L, null, null, 2, null, null));
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(new Time(millis), null, null, 3, null, null));
assertEquals("1969-12-31T23:59:59.9Z", instance.valueToString(new Time(-100L), null, null, 1, null, null));
assertEquals("1969-12-31T23:59:59.98Z", instance.valueToString(new Time(-20L), null, null, 2, null, null));
final Date date = new Date(millis);
final String time = date.toString().substring(11, 19);
assertTrue(instance.valueToString(date, null, null, 3, null, null).contains(time));
@ -128,7 +133,14 @@ public class EdmDateTimeOffsetTest extends PrimitiveTypeBaseTest {
Long.class));
assertEquals(Long.valueOf(120L), instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null,
Long.class));
assertEquals(new Time(120000L), instance.valueOfString("1970-01-01T00:02", null, null, null, null, null,
Time.class));
assertEquals(new Time(12L), instance.valueOfString("1970-01-01T00:00:00.012", null, null, 3, null, null,
Time.class));
assertEquals(new Time(120L), instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null,
Time.class));
expectFacetsErrorInValueOfString(instance, "2012-02-29T23:32:02.9Z", null, null, null, null, null);
expectFacetsErrorInValueOfString(instance, "2012-02-29T23:32:02.9Z", null, null, 0, null, null);
expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02X");

View File

@ -20,6 +20,7 @@ package org.apache.olingo.commons.core.edm.primitivetype;
import static org.junit.Assert.assertEquals;
import java.sql.Time;
import java.util.Calendar;
import java.util.TimeZone;
@ -60,7 +61,30 @@ public class EdmTimeOfDayTest extends PrimitiveTypeBaseTest {
expectTypeErrorInValueToString(instance, 0);
}
@Test
public void toTimeObject() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
dateTime.setTimeZone(TimeZone.getTimeZone("GMT"));
dateTime.set(Calendar.HOUR, 12);
Time timeValue = instance.valueOfString("12:00:00", null, null, null, null, null, Time.class);
assertEquals(dateTime.getTimeInMillis(), timeValue.getTime());
}
@Test
public void fromTimeObject() throws Exception {
Calendar dateTime = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
dateTime.clear();
dateTime.set(Calendar.HOUR, 5);
dateTime.set(Calendar.MINUTE, 59);
dateTime.set(Calendar.SECOND, 23);
Time time = new Time(dateTime.getTimeInMillis());
assertEquals("05:59:23", instance.valueToString(time, null, null, null, null, null));
}
@Test
public void valueOfString() throws Exception {
Calendar dateTime = Calendar.getInstance();