Skip to content

Commit

Permalink
update trailing character cleanup regex in FlexibleDateTimeParser (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdahlke committed May 10, 2024
1 parent 65add6d commit af83fd0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/java/emissary/util/FlexibleDateTimeParser.java
Expand Up @@ -51,7 +51,7 @@ public class FlexibleDateTimeParser {
private static final Pattern REPLACE = Pattern.compile("\t+|[ ]+", Pattern.DOTALL);

/* Remove other junk */
private static final Pattern REMOVE = Pattern.compile("<.+?>|=0D$", Pattern.DOTALL);
private static final Pattern REMOVE = Pattern.compile("<.+?>$|=0D$", Pattern.DOTALL);

/* timezone - config var: TIMEZONE */
private static ZoneId timezone = ZoneId.of(DEFAULT_TIMEZONE);
Expand Down Expand Up @@ -225,7 +225,8 @@ private static String cleanDateString(final String date) {
return date;
}

String cleanedDateString = date;
// date strings over 100 characters are more than likely invalid
String cleanedDateString = StringUtils.substring(date, 0, 100);
cleanedDateString = REPLACE.matcher(cleanedDateString).replaceAll(SPACE);
cleanedDateString = REMOVE.matcher(cleanedDateString).replaceAll(EMPTY);
return StringUtils.trimToNull(cleanedDateString);
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/emissary/util/FlexibleDateTimeParserTest.java
Expand Up @@ -2,6 +2,7 @@

import emissary.test.core.junit5.UnitTest;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -420,6 +421,7 @@ void parse_yyyy_DDD() {
@Test
void testCleanDateString() {
test("2016-01-04 18:20<br>", EXPECTED_NO_SECS, "HTML");
test("2016-01-04 18:20<br>br>", EXPECTED_NO_SECS, "HTML");
test("2016-01-04\t\t18:20", EXPECTED_NO_SECS, "TABS");
test("2016-01-04 18:20", EXPECTED_NO_SECS, "SPACES");
test("2016-01-04 18:20=0D", EXPECTED_NO_SECS, "qp'ified ending");
Expand All @@ -433,5 +435,6 @@ void testBad() {
assertNull(FlexibleDateTimeParser.parse("1234", Collections.singletonList(null)));
test("17.Mar.2016", 0L, "UNKNOWN");
test("Mon, 2 Feb 2017 06:20:30 PM +0000", 0L, "UNKNOWN");
test("2016:01:04 18:20:30 GMT+0000<" + RandomStringUtils.randomAlphanumeric(75) + ">", 0L, "UNKNOWN");
}
}

0 comments on commit af83fd0

Please sign in to comment.