Skip to content

Commit

Permalink
fixed timezone issue in unit tests, fixes #48
Browse files Browse the repository at this point in the history
  • Loading branch information
rcongiu committed Feb 12, 2014
1 parent 73b5388 commit 72bbf27
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ match hive table declaration.
More detailed explanation on my blog:
http://www.congiu.com/articles/json_serde

### Notes

#### Timestamp support
note that timestamp support will use the systems default timezone
to convert timestamps.


### CONTRIBUTING

I am using gitflow for the release cycle.
Expand Down
32 changes: 28 additions & 4 deletions src/test/java/org/openx/data/jsonserde/JsonSerDeTimeStampTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
import org.openx.data.jsonserde.json.JSONObject;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;

import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -55,7 +60,7 @@ public void testTimestampDeSerialize() throws Exception {
Writable w = new Text("{\"one\":true,\"five\":\"2013-03-27 23:18:40\"}");

JSONObject result = (JSONObject) instance.deserialize(w);
assertEquals(result.get("five"), Timestamp.valueOf("2013-03-27 23:18:40.0"));
assertEquals(Timestamp.valueOf("2013-03-27 23:18:40.0"), result.get("five"));
}

@Test
Expand All @@ -64,16 +69,17 @@ public void testTimestampDeSerializeWithNanoseconds() throws Exception {
Writable w = new Text("{\"one\":true,\"five\":\"2013-03-27 23:18:40.123456\"}");

JSONObject result = (JSONObject) instance.deserialize(w);
assertEquals(result.get("five"), Timestamp.valueOf("2013-03-27 23:18:40.123456"));
assertEquals( Timestamp.valueOf("2013-03-27 23:18:40.123456"), result.get("five"));
}

@Test
public void testTimestampDeSerializeNumericTimestamp() throws Exception {
// Test that timestamp object can be deserialized
Writable w = new Text("{\"one\":true,\"five\":1367801925}");


JSONObject result = (JSONObject) instance.deserialize(w);
assertEquals(result.get("five"), Timestamp.valueOf("2013-05-05 17:58:45.0") );
assertEquals(getDate("2013-05-05 17:58:45.0" ), result.get("five") );
}

@Test
Expand All @@ -82,7 +88,25 @@ public void testTimestampDeSerializeNumericTimestampWithNanoseconds() throws Exc
Writable w = new Text("{\"one\":true,\"five\":1367801925.123}");
//
JSONObject result = (JSONObject) instance.deserialize(w);
assertEquals(result.get("five"), Timestamp.valueOf("2013-05-05 17:58:45.123"));

assertEquals(getDate("2013-05-05 17:58:45.123"), result.get("five") );
}

/**
* for tests, if time zone not specified, make sure that it's in the correct
* timezone
*/
static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz");
public static Timestamp getDate(String s) throws ParseException {

s = s + " PDT";

Date dt = sdf.parse(s);
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
Timestamp ts = new Timestamp(cal.getTimeInMillis());
return ts;

}


Expand Down

0 comments on commit 72bbf27

Please sign in to comment.