(Finally) Fixing incorrect millis <-> nanos conversion (with Timestamp) in EdmDateTimeOffset
This commit is contained in:
parent
811f0bfcc7
commit
fe8df96fe9
|
@ -18,6 +18,9 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.olingo.client.core.it.v4;
|
package org.apache.olingo.client.core.it.v4;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
@ -30,9 +33,6 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
|
||||||
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
|
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
|
||||||
import org.apache.olingo.commons.api.edm.FullQualifiedName;
|
import org.apache.olingo.commons.api.edm.FullQualifiedName;
|
||||||
import org.apache.olingo.commons.api.format.ODataPubFormat;
|
import org.apache.olingo.commons.api.format.ODataPubFormat;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class EntityUpdateTestITCase extends AbstractTestITCase {
|
public class EntityUpdateTestITCase extends AbstractTestITCase {
|
||||||
|
|
|
@ -158,7 +158,7 @@ public class PrimitiveValueTest extends AbstractTest {
|
||||||
|
|
||||||
final ODataValue parsed = getClient().getObjectFactory().newPrimitiveValueBuilder().
|
final ODataValue parsed = getClient().getObjectFactory().newPrimitiveValueBuilder().
|
||||||
setType(EdmPrimitiveTypeKind.DateTimeOffset).setText(value.asPrimitive().toString()).build();
|
setType(EdmPrimitiveTypeKind.DateTimeOffset).setText(value.asPrimitive().toString()).build();
|
||||||
assertEquals("2013-01-10T03:00:00.022+01:00", parsed.asPrimitive().toString());
|
assertEquals(22, parsed.asPrimitive().toCastValue(Calendar.class).get(Calendar.MILLISECOND));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -103,7 +103,7 @@ public final class EdmDateTimeOffset extends SingletonPrimitiveType {
|
||||||
dateTimeValue.set(Calendar.MILLISECOND, Short.parseShort(milliSeconds));
|
dateTimeValue.set(Calendar.MILLISECOND, Short.parseShort(milliSeconds));
|
||||||
|
|
||||||
if (!decimals.isEmpty()) {
|
if (!decimals.isEmpty()) {
|
||||||
final int fractionalSecs = Integer.parseInt(decimals);
|
final int fractionalSecs = dateTimeValue.get(Calendar.MILLISECOND);
|
||||||
// if fractional are just milliseconds, convert to nanoseconds
|
// if fractional are just milliseconds, convert to nanoseconds
|
||||||
timestamp.setNanos(fractionalSecs < 1000 ? fractionalSecs * 1000000 : fractionalSecs);
|
timestamp.setNanos(fractionalSecs < 1000 ? fractionalSecs * 1000000 : fractionalSecs);
|
||||||
}
|
}
|
||||||
|
@ -296,10 +296,19 @@ public final class EdmDateTimeOffset extends SingletonPrimitiveType {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
String fractionals = NANO_FORMAT.get().format(fractionalSeconds);
|
|
||||||
// Keep output similar to Calendar's, if possible
|
// Keep output similar to Calendar's, if possible
|
||||||
if ("000000".equals(fractionals.substring(3))) {
|
String fractionals = NANO_FORMAT.get().format(fractionalSeconds);
|
||||||
fractionals = fractionals.substring(0, 3);
|
boolean canStart = false;
|
||||||
|
int firstZeroAfterNonZeroIdx = -1;
|
||||||
|
for (int i = 0; i < fractionals.length() && firstZeroAfterNonZeroIdx == -1; i++) {
|
||||||
|
if ('0' != fractionals.charAt(i)) {
|
||||||
|
canStart = true;
|
||||||
|
} else if (canStart && '0' == fractionals.charAt(i)) {
|
||||||
|
firstZeroAfterNonZeroIdx = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (firstZeroAfterNonZeroIdx != -1 && 0 == Integer.valueOf(fractionals.substring(firstZeroAfterNonZeroIdx))) {
|
||||||
|
fractionals = fractionals.substring(0, firstZeroAfterNonZeroIdx);
|
||||||
}
|
}
|
||||||
result.append('.').append(fractionals);
|
result.append('.').append(fractionals);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.olingo.commons.core.edm.primitivetype;
|
package org.apache.olingo.commons.core.edm.primitivetype;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
@ -104,6 +105,8 @@ public class EdmDateTimeOffsetTest extends PrimitiveTypeBaseTest {
|
||||||
dateTime.add(Calendar.MILLISECOND, 7);
|
dateTime.add(Calendar.MILLISECOND, 7);
|
||||||
assertEquals(dateTime, instance.valueOfString("2012-02-29T01:02:03.007+11:00", null, null, 3, null, null,
|
assertEquals(dateTime, instance.valueOfString("2012-02-29T01:02:03.007+11:00", null, null, 3, null, null,
|
||||||
Calendar.class));
|
Calendar.class));
|
||||||
|
assertEquals(530000000, instance.valueOfString("2012-02-29T01:02:03.53+11:00", null, null, 9, null, null,
|
||||||
|
Timestamp.class).getNanos());
|
||||||
|
|
||||||
assertEquals(Long.valueOf(120000L), instance.valueOfString("1970-01-01T00:02", null, null, null, null, null,
|
assertEquals(Long.valueOf(120000L), instance.valueOfString("1970-01-01T00:02", null, null, null, null, null,
|
||||||
Long.class));
|
Long.class));
|
||||||
|
|
Loading…
Reference in New Issue