Skip to content

Commit

Permalink
Merge pull request #1518 from tech-sketch/feature/1517_modify_gettime…
Browse files Browse the repository at this point in the history
…instant

[1517][cygnus-common][CommonUtils] getTimeInstant can accepts the ISO8601 with offset
  • Loading branch information
fgalan authored Sep 21, 2018
2 parents d492237 + 3d7c0c0 commit 887d615
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[cygnus-ngsi][KafkaSink] Use lower_case option to build topics (#1468)
[cygnus-ngsi] [NGSIDynamoDBSink] [bug] Southeast regions are bad coded (#1448)
[cygnus-common][CommonUtils][debug] Modify that CommonUtils#getTimeInstant can accepts the ISO8601 format with offset like 2018-01-02T03:04:05+09:00 (#1517)

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Properties;
import java.util.TimeZone;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.DatatypeConverter;
import kafka.common.OffsetOutOfRangeException;
Expand Down Expand Up @@ -77,6 +78,12 @@ public final class CommonUtils {
"yyyy-MM-dd HH:mm:ss").withOffsetParsed().withZoneUTC();
private static final DateTimeFormatter FORMATTER4 = DateTimeFormat.forPattern(
"yyyy-MM-dd HH:mm:ss.SSS").withOffsetParsed().withZoneUTC();
private static final DateTimeFormatter FORMATTER5 = DateTimeFormat.forPattern(
"yyyy-MM-dd'T'HH:mm:ssZ").withOffsetParsed();
private static final DateTimeFormatter FORMATTER6 = DateTimeFormat.forPattern(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ").withOffsetParsed();
private static final Pattern FORMATTER6_PATTERN = Pattern.compile("^(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})\\.(\\d+)([+-][\\d:]+)$");

private static final Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9_]*$");

/**
Expand Down Expand Up @@ -308,7 +315,30 @@ public static Long getTimeInstant(String metadata) {
dateTime = FORMATTER4.parseDateTime(mdValueTruncated);
} catch (Exception e6) {
LOGGER.debug(e6.getMessage());
return null;

try {
// ISO 8601 with offset (without milliseconds)
dateTime = FORMATTER5.parseDateTime(mdValue);
} catch (Exception e7) {
LOGGER.debug(e7.getMessage());

try {
// ISO 8601 with offset (with milliseconds)
Matcher matcher = FORMATTER6_PATTERN.matcher(mdValue);
if (matcher.matches()) {
String mdValueTruncated = matcher.group(1) + "."
+ matcher.group(2).substring(0, 3)
+ matcher.group(3);
dateTime = FORMATTER6.parseDateTime(mdValueTruncated);
} else {
LOGGER.debug("ISO8601 format does not match");
return null;
} // if
} catch (Exception e8) {
LOGGER.debug(e8.getMessage());
return null;
} // try catch
} // try catch
} // try catch
} // try catch
} // try catch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,71 @@ public void testGetTimeInstantSQLTimestampWithMicroseconds() {
throw e;
} // try catch
} // testGetTimeInstantSQLTimestampWithMicroseconds

/**
* [CommonUtils.getTimeInstant] -------- When getting a time instant, it is properly obtained when passing
* a valid ISO 8601 timestamp with offset without miliseconds.
*/
@Test
public void testGetTimeInstantISO8601TimestampWithOffsetWithoutMiliseconds() {
System.out.println(getTestTraceHead("[CommonUtils.getTimeInstant]")
+ "-------- When getting a time instant, it is properly obtained when passing a valid "
+ "ISO 8601 timestamp with offset without miliseconds");
String offsets[] = {"+09:00", "+0900", "+09", "-09:00", "-0900", "-09"};
for (String offset : offsets) {
JSONObject metadataJson = new JSONObject();
metadataJson.put("name", "TimeInstant");
metadataJson.put("type", "ISO8601");
metadataJson.put("value", "2017-01-01T00:00:01" + offset);
JSONArray metadatasJson = new JSONArray();
metadatasJson.add(metadataJson);
String metadatasStr = metadatasJson.toJSONString();
Long timeInstant = CommonUtils.getTimeInstant(metadatasStr);

try {
assertTrue(timeInstant != null);
System.out.println(getTestTraceHead("[CommonUtils.getTimeInstant]") + "- OK - Time instant obtained for '"
+ metadatasJson.toJSONString() + "' is '" + timeInstant + "'");
} catch (AssertionError e) {
System.out.println(getTestTraceHead("[CommonUtils.getTimeInstant]")
+ "- FAIL - Time instant obtained is 'null'");
throw e;
} // try catch
} // for
} // testGetTimeInstantISO8601TimestampWithOffsetWithoutMiliseconds

/**
* [CommonUtils.getTimeInstant] -------- When getting a time instant, it is properly obtained when passing
* a valid ISO 8601 timestamp with offset with miliseconds.
*/
@Test
public void testGetTimeInstantISO8601TimestampWithOffsetWithMiliseconds() {
System.out.println(getTestTraceHead("[CommonUtils.getTimeInstant]")
+ "-------- When getting a time instant, it is properly obtained when passing a valid "
+ "ISO 8601 timestamp with offset and miliseconds");
String offsets[] = {"+11:00", "+1100", "+11", "-11:00", "-1100", "-11"};
for (String offset : offsets) {
JSONObject metadataJson = new JSONObject();
metadataJson.put("name", "TimeInstant");
metadataJson.put("type", "ISO8601");
metadataJson.put("value", "2017-01-01T00:00:01.123456" + offset);
JSONArray metadatasJson = new JSONArray();
metadatasJson.add(metadataJson);
String metadatasStr = metadatasJson.toJSONString();
Long timeInstant = CommonUtils.getTimeInstant(metadatasStr);

try {
assertTrue(timeInstant != null);
System.out.println(getTestTraceHead("[CommonUtils.getTimeInstant]") + "- OK - Time instant obtained for '"
+ metadatasJson.toJSONString() + "' is '" + timeInstant + "'");
} catch (AssertionError e) {
System.out.println(getTestTraceHead("[CommonUtils.getTimeInstant]")
+ "- FAIL - Time instant obtained is 'null'");
throw e;
} // try catch
}
} // testGetTimeInstantISO8601TimestampWithOffsetWithMiliseconds


/**
* [CommonUtils.getMilliseconds] -------- Milliseconds are obtained when passing a valid timestamp.
Expand Down

0 comments on commit 887d615

Please sign in to comment.