diff --git a/build.gradle b/build.gradle index 4631dfbf..f54e7a1d 100644 --- a/build.gradle +++ b/build.gradle @@ -44,6 +44,8 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.0' + } test { diff --git a/src/main/java/com/github/khakers/modmailviewer/util/DateFormatters.java b/src/main/java/com/github/khakers/modmailviewer/util/DateFormatters.java index a5fadff1..6ae0285e 100644 --- a/src/main/java/com/github/khakers/modmailviewer/util/DateFormatters.java +++ b/src/main/java/com/github/khakers/modmailviewer/util/DateFormatters.java @@ -8,7 +8,10 @@ public class DateFormatters { //todo incorrect parsing of nanos public static final DateTimeFormatter DATABASE_TIMESTAMP_FORMAT = new DateTimeFormatterBuilder() .parseCaseInsensitive() - .appendPattern("yyyy-MM-dd HH:mm:ss.nnnnnn") + .parseLenient() + .appendPattern("yyyy-MM-dd HH:mm:ss.SSSSSS") + .optionalStart() + .appendPattern("xxx") .toFormatter() .withZone(ZoneId.of("UTC")); diff --git a/src/test/java/com/github/khakers/modmailviewer/util/DateFormattersTest.java b/src/test/java/com/github/khakers/modmailviewer/util/DateFormattersTest.java new file mode 100644 index 00000000..eb9a0a71 --- /dev/null +++ b/src/test/java/com/github/khakers/modmailviewer/util/DateFormattersTest.java @@ -0,0 +1,20 @@ +package com.github.khakers.modmailviewer.util; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.time.Instant; + +class DateFormattersTest { + + @ParameterizedTest + @ValueSource(strings = { "2022-11-08 07:14:55.624724", "2022-11-09 03:51:31.687000+00:00", "2022-11-09 03:43:26.171000+00:00", "2022-11-08 05:30:50.694581" }) + void testTimeParsing(String timestamp) { + var time = DateFormatters.DATABASE_TIMESTAMP_FORMAT.parse(timestamp, Instant::from); + System.out.println("------"); + System.out.println(timestamp); + System.out.println(time.toString()); + System.out.println(DateFormatters.DATABASE_TIMESTAMP_FORMAT.format(time)); + } + +} \ No newline at end of file