Skip to content

Commit 7a05e91

Browse files
authored
Merge pull request #233 from billonahill/billg/utc_millis
Add support for parsing UTC strings with millis into timestamps
2 parents 5938c7c + a24d58b commit 7a05e91

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

json-serde/src/main/java/org/openx/data/jsonserde/objectinspector/primitive/ParsePrimitiveUtils.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ private ParsePrimitiveUtils() {
2424
throw new InstantiationError("This class must not be instantiated.");
2525
}
2626

27+
// check to see if UTC_WITH_MILLIS_FORMAT should be used.
28+
private static final Pattern UTC_MILLIS_PATTERN = Pattern.compile(".*\\.[0-9]{3}Z$");
29+
2730
// timestamps are expected to be in UTC
2831
public final static DateFormat UTC_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
32+
public final static DateFormat UTC_WITH_MILLIS_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
2933
public final static DateFormat OFFSET_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
30-
public final static DateFormat NON_UTC_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
31-
static DateFormat[] dateFormats = { UTC_FORMAT, OFFSET_FORMAT,NON_UTC_FORMAT};
34+
public final static DateFormat NON_UTC_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
35+
static DateFormat[] dateFormats = { UTC_FORMAT, UTC_WITH_MILLIS_FORMAT, OFFSET_FORMAT,NON_UTC_FORMAT};
3236
static {
3337
TimeZone tz = TimeZone.getTimeZone("UTC");
3438
for( DateFormat df : dateFormats) {
@@ -114,7 +118,11 @@ public static String nonUTCFormat(String s) {
114118
Date parsed = null;
115119
try {
116120
if(s.endsWith("Z")) { // 003Z
117-
parsed = UTC_FORMAT.parse(s);
121+
if (UTC_MILLIS_PATTERN.matcher(s).matches()) {
122+
parsed = UTC_WITH_MILLIS_FORMAT.parse(s);
123+
} else {
124+
parsed = UTC_FORMAT.parse(s);
125+
}
118126
} else if ( hasTZOffset.matcher(s).matches()) {
119127
parsed = OFFSET_FORMAT.parse(s); // +0600 or -06:00
120128
} else {

json-serde/src/test/java/org/openx/data/jsonserde/JsonSerDeTimeStampTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,14 @@ public static Timestamp getDate(String s) throws ParseException {
176176
public void testformatDateFromUTC() throws ParseException {
177177
System.out.println("testFormatDateFromUTC");
178178
String string1 = "2001-07-04T12:08:56Z";
179-
assertEquals("2001-07-04 12:08:56", ParsePrimitiveUtils.nonUTCFormat(string1));
179+
assertEquals("2001-07-04 12:08:56.000", ParsePrimitiveUtils.nonUTCFormat(string1));
180180
}
181181

182+
@Test
183+
public void testParseUTCTimestampWithMillis() throws ParseException {
184+
assertEquals(Timestamp.valueOf("2020-07-30 15:20:05.424"),
185+
ParsePrimitiveUtils.parseTimestamp("2020-07-30T15:20:05.424Z"));
186+
}
182187

183188
@Test
184189
public void testSerializeTimestamp() throws SerDeException, JSONException {

0 commit comments

Comments
 (0)