Skip to content

Commit

Permalink
minor changes in DateConverter
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Sep 3, 2024
1 parent d1172d0 commit 1a0e765
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

public class DateConverter extends ClassicConverter {

long lastTimestamp = -1;
String timestampStrCache = null;
CachingDateFormatter cachingDateFormatter = null;

public void start() {
Expand All @@ -33,13 +31,9 @@ public void start() {

if (datePattern == null) {
datePattern = CoreConstants.ISO8601_PATTERN;
}

if (datePattern.equals(CoreConstants.ISO8601_STR)) {
} else if (datePattern.equals(CoreConstants.ISO8601_STR)) {
datePattern = CoreConstants.ISO8601_PATTERN;
}

if (datePattern.equals(CoreConstants.STRICT_STR)) {
} else if (datePattern.equals(CoreConstants.STRICT_STR)) {
datePattern = CoreConstants.STRICT_ISO8601_PATTERN;
}

Expand All @@ -60,6 +54,7 @@ public void start() {
}
try {
// if zoneId is null, the CachingDateFormatter will use the ZoneId.systemDefault()
// if locale is null, the CachingDateFormatter will use the Locale.getDefault()
cachingDateFormatter = new CachingDateFormatter(datePattern, zoneId, locale);
} catch (IllegalArgumentException e) {
addWarn("Could not instantiate SimpleDateFormat with pattern " + datePattern, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,16 @@ public void testSequenceNumber() {

@Test
void dateConverterTest() {
dateConverterChecker(List.of("STRICT", "GMT"), "2024-08-14T15:29:25,956");
dateConverterChecker(List.of("ISO8601", "GMT"), "2024-08-14 15:29:25,956");
dateConverterChecker(List.of("ISO8601", "UTC"), "2024-08-14 15:29:25,956");
dateConverterChecker(List.of("yyyy-MM-EE", "UTC", "fr-CH"), "2024-08-mer.");
// 2024-08-14T1Z:29:25,956 GMT
long millis = 1_723_649_365_956L;
dateConverterChecker(millis, List.of("STRICT", "GMT"), "2024-08-14T15:29:25,956");
dateConverterChecker(millis, List.of("ISO8601", "GMT"), "2024-08-14 15:29:25,956");
dateConverterChecker(millis, List.of("ISO8601", "UTC"), "2024-08-14 15:29:25,956");
dateConverterChecker(millis, List.of("yyyy-MM-EE", "UTC", "fr-CH"), "2024-08-mer.");

}

void dateConverterChecker(List<String> options, String expected) {
void dateConverterChecker(long millis, List<String> options, String expected) {
DateConverter dateConverter = new DateConverter();
dateConverter.setOptionList(options) ;
dateConverter.setContext(loggerContext);
Expand All @@ -410,8 +412,6 @@ void dateConverterChecker(List<String> options, String expected) {
assertTrue(dateConverter.isStarted());
LoggingEvent event = makeLoggingEvent(null);

// 2024-08-14T1Z:29:25,956 GMT
long millis = 1_723_649_365_956L; //System.currentTimeMillis();
Instant now = Instant.ofEpochMilli(millis);
event.setInstant(now);
String result = dateConverter.convert(event);
Expand Down

0 comments on commit 1a0e765

Please sign in to comment.