Skip to content

Commit

Permalink
fixed conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
rcongiu committed Apr 3, 2017
2 parents 06a73b9 + bcba800 commit 7e380ab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package org.openx.data.jsonserde.objectinspector.primitive;

import java.sql.Timestamp;
import org.apache.hadoop.hive.serde2.io.TimestampWritable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
*
Expand All @@ -18,6 +20,9 @@ private ParsePrimitiveUtils() {
throw new InstantiationError("This class must not be instantiated.");
}

private static DateFormat UTC_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
private static DateFormat NON_UTC_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public static boolean isHex(String s) {
return s.startsWith("0x") || s.startsWith("0X");
}
Expand Down Expand Up @@ -59,7 +64,7 @@ public static Timestamp parseTimestamp(String s) {

Timestamp value;
if (s.indexOf(':') > 0) {
value = Timestamp.valueOf(s);
value = Timestamp.valueOf(nonUTCFormat(s));
} else if (s.indexOf('.') >= 0) {
// it's a float
value = new Timestamp(
Expand All @@ -77,4 +82,15 @@ public static Timestamp parseTimestamp(String s) {
return value;
}

public static String nonUTCFormat(String s) {
if(s.endsWith("Z")) {
try {
return NON_UTC_FORMAT.format(UTC_FORMAT.parse(s));
} catch (ParseException e) {
e.printStackTrace();
}
}
return s;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@

import static org.junit.Assert.assertEquals;
import org.openx.data.jsonserde.objectinspector.primitive.JavaStringTimestampObjectInspector;
import org.openx.data.jsonserde.objectinspector.primitive.ParsePrimitiveUtils;


/**
* User: guyrt
*/
public class JsonSerDeTimeStampTest {

static JsonSerDe instance;
Expand Down Expand Up @@ -68,6 +67,20 @@ public void testTimestampDeSerialize() throws Exception {
assertEquals(Timestamp.valueOf("2013-03-27 23:18:40.0"), jstOi.getPrimitiveJavaObject(result.get("five")));
}

@Test
public void testUTCTimestampDeSerialize() throws Exception {
// Test that timestamp object can be deserialized
Writable w = new Text("{\"one\":true,\"five\":\"2013-03-27T23:18:40Z\"}");

JSONObject result = (JSONObject) instance.deserialize(w);

StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector();

JavaStringTimestampObjectInspector jstOi = (JavaStringTimestampObjectInspector)
soi.getStructFieldRef("five").getFieldObjectInspector();
assertEquals(Timestamp.valueOf("2013-03-27 23:18:40.0"), jstOi.getPrimitiveJavaObject(result.get("five")));
}

@Test
public void testTimestampDeSerializeWithNanoseconds() throws Exception {
// Test that timestamp object can be deserialized
Expand Down Expand Up @@ -137,4 +150,10 @@ public static Timestamp getDate(String s) throws ParseException {
return ts;

}

@Test
public void testformatDateFromUTC() throws ParseException {
String string1 = "2001-07-04T12:08:56Z";
assertEquals("2001-07-04 12:08:56", ParsePrimitiveUtils.nonUTCFormat(string1));
}
}

0 comments on commit 7e380ab

Please sign in to comment.