Skip to content

Commit

Permalink
Fixes time formatter & adds test
Browse files Browse the repository at this point in the history
The timestamps can apparently include timezone offsets now, so this adds an optional section for them and fixes fraction of second handling
  • Loading branch information
khakers committed Nov 9, 2022
1 parent e28d606 commit f9ac297
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit f9ac297

Please sign in to comment.