(Finally) Fixing incorrect millis <-> nanos conversion (with Timestamp) in EdmDateTimeOffset

This commit is contained in:
Francesco Chicchiriccò 2014-04-17 17:31:18 +02:00
parent 811f0bfcc7
commit fe8df96fe9
4 changed files with 20 additions and 8 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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);
}

View File

@ -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));