Skip to content

Commit 7cdbcaf

Browse files
committed
Added handling of invalid XSD dates that GitLab might use (#151).
1 parent 04d7e08 commit 7cdbcaf

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Diff for: src/main/java/org/gitlab4j/api/utils/ISO8601.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
public class ISO8601 {
1717

1818
public static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
19+
public static final String SPACEY_PATTERN = "yyyy-MM-dd HH:mm:ss Z";
1920
public static final String PATTERN_MSEC = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
2021
public static final String OUTPUT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
2122
public static final String OUTPUT_MSEC_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
2223
public static final String UTC_PATTERN = "yyyy-MM-dd HH:mm:ss 'UTC'";
2324

25+
private static final String PATTERN_REGEX = "\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\d[-+]\\d\\d\\d\\d";
26+
private static final String SPACEY_PATTERN_REGEX = "\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d [-+]\\d\\d\\d\\d";
27+
2428
// Set up ThreadLocal storage to save a thread local SimpleDateFormat keyed with the format string
2529
private static final class SafeDateFormatter {
2630

@@ -128,8 +132,20 @@ public static Date toDate(String dateTimeString) throws ParseException {
128132
if (dateTimeString.endsWith("UTC")) {
129133
return (SafeDateFormatter.getDateFormat(UTC_PATTERN).parse(dateTimeString));
130134
} else {
131-
Calendar cal = DatatypeConverter.parseDateTime(dateTimeString);
132-
return (cal.getTime());
135+
try {
136+
Calendar cal = DatatypeConverter.parseDateTime(dateTimeString);
137+
return (cal.getTime());
138+
} catch (Exception e) {
139+
if (dateTimeString.matches(PATTERN_REGEX)) {
140+
// Try using the ISO8601 format
141+
return (SafeDateFormatter.getDateFormat(PATTERN).parse(dateTimeString));
142+
} else if (dateTimeString.matches(SPACEY_PATTERN_REGEX)) {
143+
// Try using the invalid ISO8601 format with spaces, GitLab sometimes uses this
144+
return (SafeDateFormatter.getDateFormat(SPACEY_PATTERN).parse(dateTimeString));
145+
} else {
146+
throw e;
147+
}
148+
}
133149
}
134150
}
135151

0 commit comments

Comments
 (0)