From fe8df96fe990f616c08b7b9e9d03b6ccbd9d781c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesco=20Chicchiricc=C3=B2?= Date: Thu, 17 Apr 2014 17:31:18 +0200 Subject: [PATCH] (Finally) Fixing incorrect millis <-> nanos conversion (with Timestamp) in EdmDateTimeOffset --- .../core/it/v4/EntityUpdateTestITCase.java | 6 +++--- .../client/core/v3/PrimitiveValueTest.java | 2 +- .../edm/primitivetype/EdmDateTimeOffset.java | 17 +++++++++++++---- .../primitivetype/EdmDateTimeOffsetTest.java | 3 +++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v4/EntityUpdateTestITCase.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v4/EntityUpdateTestITCase.java index 36a9e65be..30e6607a6 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v4/EntityUpdateTestITCase.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/it/v4/EntityUpdateTestITCase.java @@ -18,6 +18,9 @@ */ 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.util.Calendar; 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.FullQualifiedName; import org.apache.olingo.commons.api.format.ODataPubFormat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - import org.junit.Test; public class EntityUpdateTestITCase extends AbstractTestITCase { diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/v3/PrimitiveValueTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/v3/PrimitiveValueTest.java index 260f9a4ae..db0b7b750 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/v3/PrimitiveValueTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/v3/PrimitiveValueTest.java @@ -158,7 +158,7 @@ public class PrimitiveValueTest extends AbstractTest { final ODataValue parsed = getClient().getObjectFactory().newPrimitiveValueBuilder(). 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 diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDateTimeOffset.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDateTimeOffset.java index 2e7ab9853..44dc72d44 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDateTimeOffset.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDateTimeOffset.java @@ -103,7 +103,7 @@ public final class EdmDateTimeOffset extends SingletonPrimitiveType { dateTimeValue.set(Calendar.MILLISECOND, Short.parseShort(milliSeconds)); if (!decimals.isEmpty()) { - final int fractionalSecs = Integer.parseInt(decimals); + final int fractionalSecs = dateTimeValue.get(Calendar.MILLISECOND); // if fractional are just milliseconds, convert to nanoseconds timestamp.setNanos(fractionalSecs < 1000 ? fractionalSecs * 1000000 : fractionalSecs); } @@ -296,10 +296,19 @@ public final class EdmDateTimeOffset extends SingletonPrimitiveType { throw new IllegalArgumentException(); } + // Keep output similar to Calendar's, if possible String fractionals = NANO_FORMAT.get().format(fractionalSeconds); - // Keep output similar to Calendar's, if possible - if ("000000".equals(fractionals.substring(3))) { - 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); } diff --git a/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDateTimeOffsetTest.java b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDateTimeOffsetTest.java index 2a1e7d580..4e49a62dc 100644 --- a/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDateTimeOffsetTest.java +++ b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/edm/primitivetype/EdmDateTimeOffsetTest.java @@ -18,6 +18,7 @@ */ package org.apache.olingo.commons.core.edm.primitivetype; +import java.sql.Timestamp; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -104,6 +105,8 @@ public class EdmDateTimeOffsetTest extends PrimitiveTypeBaseTest { dateTime.add(Calendar.MILLISECOND, 7); assertEquals(dateTime, instance.valueOfString("2012-02-29T01:02:03.007+11:00", null, null, 3, null, null, 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, Long.class));