Skip to content

Commit

Permalink
Add support for UTC date format
Browse files Browse the repository at this point in the history
  • Loading branch information
Vidur Patel committed Oct 26, 2015
1 parent af92a46 commit bcba800
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
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;

/**
*
* @author rcongiu
*/
public class ParsePrimitiveUtils {

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 @@ -53,7 +58,7 @@ public static long parseLong(String s) {
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 @@ -65,4 +70,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,6 +31,7 @@

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

/**
* User: guyrt
Expand Down Expand Up @@ -68,6 +69,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 @@ -125,5 +140,9 @@ public static Timestamp getDate(String s) throws ParseException {

}


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

Please sign in to comment.