Skip to content

Commit b59f458

Browse files
Ignore [] brackets while capturing date with UNIFIED_DATE_TIMESTAMP (#444)
* Ignore [] brackets while capturing date with UNIFIED_DATE_TIMESTAMP * Add test case for parsing GC log line with brackets
1 parent 325cfa6 commit b59f458

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

api/src/main/java/com/microsoft/gctoolkit/time/DateTimeStamp.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private static double ageFromString(String doubleFormat) {
6565
private static final String TIME = INTEGER + DECIMAL_POINT + "\\d{3}";
6666

6767
// Unified Tokens
68-
private static final String DATE_TAG = "\\[" + DATE + "]";
68+
private static final String DATE_TAG = "\\[(" + DATE + ")]";
6969
private static final String UPTIME_TAG = "\\[(" + TIME + ")s]";
7070

7171
// Pre-unified tokens
@@ -81,15 +81,20 @@ private static double ageFromString(String doubleFormat) {
8181

8282
public static DateTimeStamp fromGCLogLine(String line) {
8383
Matcher matcher;
84-
int captureGroup = 2;
84+
int dateCaptureGroup;
85+
int ageCaptureGroup;
8586
if ( line.startsWith("[")) {
8687
matcher = UNIFIED_DATE_TIMESTAMP.matcher(line);
87-
captureGroup = 3;
88-
} else
88+
dateCaptureGroup = 2;
89+
ageCaptureGroup = 4;
90+
} else {
8991
matcher = PREUNIFIED_DATE_TIMESTAMP.matcher(line);
92+
dateCaptureGroup = 1;
93+
ageCaptureGroup = 2;
94+
}
9095

9196
if ( matcher.find())
92-
return new DateTimeStamp(dateFromString(matcher.group(1)), ageFromString(matcher.group(captureGroup)));
97+
return new DateTimeStamp(dateFromString(matcher.group(dateCaptureGroup)), ageFromString(matcher.group(ageCaptureGroup)));
9398
else
9499
return EMPTY_DATE;
95100
}

api/src/test/java/com/microsoft/gctoolkit/time/DateTimeStampTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,12 @@ void malformedDateTimeStampThrowsException() {
483483
assertThrows(IllegalStateException.class, () -> onlyDate.after(onlyTime));
484484
}
485485

486+
@Test
487+
void shouldParseGCLogLineWithBrackets() {
488+
final String dateTimeString = "2025-05-08T11:07:55.681+0530";
489+
final String gcLogLine = "[" + dateTimeString + "][gc,phases ] GC(4) Other: 0.2ms";
490+
final DateTimeStamp dateTimeStamp = DateTimeStamp.fromGCLogLine(gcLogLine);
491+
final ZonedDateTime expected = ZonedDateTime.from(formatter.parse(dateTimeString));
492+
assertTrue(expected.isEqual(dateTimeStamp.getDateTime()));
493+
}
486494
}

0 commit comments

Comments
 (0)