Fixes for timestamp parsing.

This commit is contained in:
Mark Nellemann 2021-01-28 09:18:39 +01:00
parent 49b8888032
commit edfd89a11d
4 changed files with 26 additions and 28 deletions

View File

@ -86,18 +86,18 @@ public class SyslogParserRfc3164 extends SyslogParser {
*/
public Instant parseTimestamp(String dateString) {
// We need to add year to parse date correctly
// We need to add current year to parse date correctly
OffsetDateTime odt = OffsetDateTime.now();
// Date: Mmm dd hh:mm:ss
Instant instant = null;
try {
instant = Instant.from(dateTimeFormatter.parse(odt.getYear() + " " + dateString));
return Instant.from(dateTimeFormatter.parse(odt.getYear() + " " + dateString));
} catch(DateTimeParseException e) {
log.error("parseDate()", e);
log.debug("parseTimestamp()", e);
}
return instant;
log.warn("parseTimestamp() - Could not parse timestamp: " + dateString);
return Instant.now();
}
}

View File

@ -21,9 +21,11 @@ import biz.nellemann.syslogd.msg.SyslogMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
@ -106,11 +108,11 @@ public class SyslogParserRfc5424 extends SyslogParser {
*/
List<String> formatStrings = Arrays.asList(
"yyyy-MM-dd'T'HH:mm:ss.SS'Z'",
"yyyy-MM-dd'T'HH:mm:ss.SSXXX",
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX",
"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXX"
//"yyyy-MM-dd'T'HH:mm:ss.SS'X'",
"yyyy-MM-dd'T'HH:mm:ss.SSX",
"yyyy-MM-dd'T'HH:mm:ss.SSSX",
"yyyy-MM-dd'T'HH:mm:ss.SSSSSSX",
"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSX"
);
for(String formatString : formatStrings)
@ -118,17 +120,13 @@ public class SyslogParserRfc5424 extends SyslogParser {
try {
return new SimpleDateFormat(formatString).parse(dateString).toInstant();
}
catch (ParseException e) {}
catch (ParseException e) {
log.debug("parseTimestamp()", e);
}
}
/*
try {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
instant = Instant.from(dateTimeFormatter.parse(dateString));
} catch(DateTimeParseException e) {
log.error("parseTimestamp()", e);
}
return instant;*/
return null;
log.warn("parseTimestamp() - Could not parse timestamp: " + dateString);
return Instant.now();
}
}

View File

@ -67,8 +67,8 @@ class SyslogParserRfc5424Test extends Specification {
Instant inst = syslogParser.parseTimestamp(dateString)
then:
inst.toEpochMilli() == 482188850052
//inst.toString() == "1985-04-12T21:20:50.052Z"
inst.toEpochMilli() == 482196050052
inst.toString() == "1985-04-12T23:20:50.052Z"
}
void "test parseRfc5424Timestamp ex2"() {
@ -80,7 +80,7 @@ class SyslogParserRfc5424Test extends Specification {
then:
inst.toEpochMilli() == 482196050052
//inst.toString() == "1985-04-12T23:20:50.052Z"
inst.toString() == "1985-04-12T23:20:50.052Z"
}
void "test parseRfc5424Timestamp ex3"() {
@ -91,8 +91,8 @@ class SyslogParserRfc5424Test extends Specification {
Instant inst = syslogParser.parseTimestamp(dateString)
then:
inst.toEpochMilli() == 1065903255003
//inst.toString() == "2003-10-11T20:14:15.003Z"
inst.toEpochMilli() == 1065910455003
inst.toString() == "2003-10-11T22:14:15.003Z"
}
void "test parseRfc5424Timestamp ex4"() {
@ -104,7 +104,7 @@ class SyslogParserRfc5424Test extends Specification {
then:
inst.toEpochMilli() == 1061727255003
//inst.toString() == "2003-08-24T12:14:15.003Z"
inst.toString() == "2003-08-24T12:14:15.003Z"
}
void "test parseRfc5424Timestamp ex5"() {
@ -116,7 +116,7 @@ class SyslogParserRfc5424Test extends Specification {
then:
inst.toEpochMilli() == 1061727255003
//inst.toString() == "2003-08-24T12:14:15.003Z"
inst.toString() == "2003-08-24T12:14:15.003Z"
}
}

View File

@ -1,4 +1,4 @@
org.slf4j.simpleLogger.logFile=System.out
org.slf4j.simpleLogger.logFile=System.err
org.slf4j.simpleLogger.showDateTime=false
org.slf4j.simpleLogger.showShortLogName=true
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS