From 1d2e34b6a643317cc7f216ab15cdd1ac227317df Mon Sep 17 00:00:00 2001 From: Guille Date: Wed, 10 Jul 2019 12:03:04 +0200 Subject: [PATCH 1/4] Add helper to parse incoming times with unsupported offset definitions --- .../briefcase/reused/Iso8601Helpers.java | 14 ++++++++ .../briefcase/reused/Iso8601HelpersTest.java | 34 ++++++++++++------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/org/opendatakit/briefcase/reused/Iso8601Helpers.java b/src/org/opendatakit/briefcase/reused/Iso8601Helpers.java index 3b8802e74..cace3fa5f 100644 --- a/src/org/opendatakit/briefcase/reused/Iso8601Helpers.java +++ b/src/org/opendatakit/briefcase/reused/Iso8601Helpers.java @@ -17,6 +17,7 @@ package org.opendatakit.briefcase.reused; import java.time.OffsetDateTime; +import java.time.OffsetTime; public class Iso8601Helpers { private static String normalizeDateTime(String value) { @@ -28,7 +29,20 @@ private static String normalizeDateTime(String value) { return String.format("%s:%s", value.substring(0, 26), value.substring(26)); } + private static String normalizeTime(String value) { + char charAtMinus3 = value.charAt(value.length() - 3); + if (value.endsWith("Z") || charAtMinus3 == ':') + return value; + if (charAtMinus3 == '+' || charAtMinus3 == '-') + return value + ":00"; + return String.format("%s:%s", value.substring(0, 26), value.substring(26)); + } + public static OffsetDateTime parseDateTime(String value) { return OffsetDateTime.parse(normalizeDateTime(value)); } + + public static OffsetTime parseTime(String value) { + return OffsetTime.parse(normalizeTime(value)); + } } diff --git a/test/java/org/opendatakit/briefcase/reused/Iso8601HelpersTest.java b/test/java/org/opendatakit/briefcase/reused/Iso8601HelpersTest.java index ce539b4c3..bec5c3f0c 100644 --- a/test/java/org/opendatakit/briefcase/reused/Iso8601HelpersTest.java +++ b/test/java/org/opendatakit/briefcase/reused/Iso8601HelpersTest.java @@ -19,8 +19,12 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import static org.opendatakit.briefcase.reused.Iso8601Helpers.parseDateTime; +import static org.opendatakit.briefcase.reused.Iso8601HelpersTest.TestType.DATETIME; +import static org.opendatakit.briefcase.reused.Iso8601HelpersTest.TestType.TIME; import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.time.temporal.Temporal; import java.util.Arrays; import java.util.Collection; import org.junit.Test; @@ -29,31 +33,37 @@ @RunWith(value = Parameterized.class) public class Iso8601HelpersTest { + enum TestType {DATETIME, TIME} + @Parameterized.Parameter(value = 0) public String testCase; @Parameterized.Parameter(value = 1) - public String input; + public TestType testType; @Parameterized.Parameter(value = 2) - public OffsetDateTime expectedOutput; + public String input; + + @Parameterized.Parameter(value = 3) + public Temporal expectedOutput; @Parameterized.Parameters(name = "{0}") public static Collection data() { return Arrays.asList(new Object[][]{ - {"Correct format - Offset Z", "2010-01-01T00:00:00.000Z", OffsetDateTime.parse("2010-01-01T00:00:00.000Z")}, - {"Correct format - Offset +00:00", "2010-01-01T00:00:00.000+00:00", OffsetDateTime.parse("2010-01-01T00:00:00.000+00:00")}, - {"Correct format - Offset -00:00", "2010-01-01T00:00:00.000-00:00", OffsetDateTime.parse("2010-01-01T00:00:00.000-00:00")}, - {"Correct format - Offset +03:00", "2010-01-01T00:00:00.000+03:00", OffsetDateTime.parse("2010-01-01T00:00:00.000+03:00")}, - {"Correct format - Offset -03:00", "2010-01-01T00:00:00.000-03:00", OffsetDateTime.parse("2010-01-01T00:00:00.000-03:00")}, - {"Wrong format - Offset +0030", "2010-01-01T00:00:00.000+0030", OffsetDateTime.parse("2010-01-01T00:00:00.000+00:30")}, - {"Wrong format - Offset -0030", "2010-01-01T00:00:00.000-0030", OffsetDateTime.parse("2010-01-01T00:00:00.000-00:30")}, - {"Wrong format - Offset +01", "2010-01-01T00:00:00.000+01", OffsetDateTime.parse("2010-01-01T00:00:00.000+01:00")}, + {"Correct datetime format - Offset Z", DATETIME, "2010-01-01T00:00:00.000Z", OffsetDateTime.parse("2010-01-01T00:00:00.000Z")}, + {"Correct datetime format - Offset +00:00", DATETIME, "2010-01-01T00:00:00.000+00:00", OffsetDateTime.parse("2010-01-01T00:00:00.000+00:00")}, + {"Correct datetime format - Offset -00:00", DATETIME, "2010-01-01T00:00:00.000-00:00", OffsetDateTime.parse("2010-01-01T00:00:00.000-00:00")}, + {"Correct datetime format - Offset +03:00", DATETIME, "2010-01-01T00:00:00.000+03:00", OffsetDateTime.parse("2010-01-01T00:00:00.000+03:00")}, + {"Correct datetime format - Offset -03:00", DATETIME, "2010-01-01T00:00:00.000-03:00", OffsetDateTime.parse("2010-01-01T00:00:00.000-03:00")}, + {"Wrong datetime format - Offset +0030", DATETIME, "2010-01-01T00:00:00.000+0030", OffsetDateTime.parse("2010-01-01T00:00:00.000+00:30")}, + {"Wrong datetime format - Offset -0030", DATETIME, "2010-01-01T00:00:00.000-0030", OffsetDateTime.parse("2010-01-01T00:00:00.000-00:30")}, + {"Wrong datetime format - Offset +01", DATETIME, "2010-01-01T00:00:00.000+01", OffsetDateTime.parse("2010-01-01T00:00:00.000+01:00")}, + {"Wrong time format - Offset +01", TIME, "08:08:00.000+01", OffsetTime.parse("08:08:00.000+01:00")} }); } @Test - public void normalizes_and_parses_incoming_datetimes_with_minor_defects_in_their_offsets() { - assertThat(parseDateTime(input), is(expectedOutput)); + public void normalizes_and_parses_incoming_datetimes_and_times_with_minor_defects_in_their_offsets() { + assertThat(testType == DATETIME ? parseDateTime(input) : Iso8601Helpers.parseTime(input), is(expectedOutput)); } } From c2578862a70213019dcfdf76af090901f3c2996c Mon Sep 17 00:00:00 2001 From: Guille Date: Wed, 10 Jul 2019 12:03:50 +0200 Subject: [PATCH 2/4] Remove duplication by reusing code --- .../briefcase/reused/Iso8601Helpers.java | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/org/opendatakit/briefcase/reused/Iso8601Helpers.java b/src/org/opendatakit/briefcase/reused/Iso8601Helpers.java index cace3fa5f..8ccb9e363 100644 --- a/src/org/opendatakit/briefcase/reused/Iso8601Helpers.java +++ b/src/org/opendatakit/briefcase/reused/Iso8601Helpers.java @@ -20,16 +20,7 @@ import java.time.OffsetTime; public class Iso8601Helpers { - private static String normalizeDateTime(String value) { - char charAtMinus3 = value.charAt(value.length() - 3); - if (value.endsWith("Z") || charAtMinus3 == ':') - return value; - if (charAtMinus3 == '+' || charAtMinus3 == '-') - return value + ":00"; - return String.format("%s:%s", value.substring(0, 26), value.substring(26)); - } - - private static String normalizeTime(String value) { + private static String normalizeOffset(String value) { char charAtMinus3 = value.charAt(value.length() - 3); if (value.endsWith("Z") || charAtMinus3 == ':') return value; @@ -39,10 +30,10 @@ private static String normalizeTime(String value) { } public static OffsetDateTime parseDateTime(String value) { - return OffsetDateTime.parse(normalizeDateTime(value)); + return OffsetDateTime.parse(normalizeOffset(value)); } public static OffsetTime parseTime(String value) { - return OffsetTime.parse(normalizeTime(value)); + return OffsetTime.parse(normalizeOffset(value)); } } From 19cb204e5fb1b6820b82bfcddac227a71637ff89 Mon Sep 17 00:00:00 2001 From: Guille Date: Wed, 10 Jul 2019 12:05:02 +0200 Subject: [PATCH 3/4] Use the new parsing method to normalize incoming times with offsets --- src/org/opendatakit/briefcase/export/CsvFieldMappers.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/org/opendatakit/briefcase/export/CsvFieldMappers.java b/src/org/opendatakit/briefcase/export/CsvFieldMappers.java index 845e42f15..16afef3cb 100644 --- a/src/org/opendatakit/briefcase/export/CsvFieldMappers.java +++ b/src/org/opendatakit/briefcase/export/CsvFieldMappers.java @@ -24,6 +24,8 @@ import static org.javarosa.core.model.DataType.GEOPOINT; import static org.javarosa.core.model.DataType.NULL; import static org.javarosa.core.model.DataType.TIME; +import static org.opendatakit.briefcase.reused.Iso8601Helpers.parseDateTime; +import static org.opendatakit.briefcase.reused.Iso8601Helpers.parseTime; import static org.opendatakit.briefcase.reused.UncheckedFiles.copy; import static org.opendatakit.briefcase.reused.UncheckedFiles.createDirectories; import static org.opendatakit.briefcase.reused.UncheckedFiles.exists; @@ -39,7 +41,6 @@ import java.nio.file.Paths; import java.time.LocalDate; import java.time.OffsetDateTime; -import java.time.OffsetTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; @@ -54,7 +55,6 @@ import java.util.stream.IntStream; import java.util.stream.Stream; import org.javarosa.core.model.DataType; -import org.opendatakit.briefcase.reused.Iso8601Helpers; import org.opendatakit.briefcase.reused.OptionalProduct; import org.opendatakit.briefcase.reused.Pair; @@ -181,7 +181,7 @@ private static Stream> time(XmlElement element) { .appendText(ChronoField.AMPM_OF_DAY, TextStyle.FULL) .toFormatter(); return Stream.of(Pair.of(element.fqn(), element.maybeValue() - .map(value -> OffsetTime.parse(value).format(formatter)) + .map(value -> parseTime(value).format(formatter)) .orElse(""))); } @@ -196,7 +196,7 @@ private static Stream> dateTime(XmlElement element) { * format users expect in their exported CSV files. */ static String iso8601DateTimeToExportCsvFormat(String value) { - return iso8601DateTimeToExportCsvFormat(Iso8601Helpers.parseDateTime(value)); + return iso8601DateTimeToExportCsvFormat(parseDateTime(value)); } /** From e17b8e0e5259a97efd405e3ced5f958a0c383b8f Mon Sep 17 00:00:00 2001 From: Guille Date: Wed, 10 Jul 2019 12:09:31 +0200 Subject: [PATCH 4/4] Remove annoying checkstyle rule that I have to constantly work around --- config/checkstyle/checkstyle.xml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index 76a455f53..36e6e1f15 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -70,16 +70,6 @@ - - - - - - - -