Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #760 from ggalmazor/issue_759_fix_parsing_times_wi…
Browse files Browse the repository at this point in the history
…th_unsupported_offsets

Issue 759 fix parsing times with unsupported offsets
  • Loading branch information
ggalmazor authored Jul 16, 2019
2 parents 91f5f85 + e17b8e0 commit d92b6c0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
10 changes: 0 additions & 10 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@
<property name="option" value="alone"/>
<property name="tokens" value="CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, LITERAL_WHILE, STATIC_INIT, INSTANCE_INIT"/>
</module>
<module name="WhitespaceAround">
<property name="allowEmptyConstructors" value="true"/>
<property name="allowEmptyMethods" value="true"/>
<property name="allowEmptyTypes" value="true"/>
<property name="allowEmptyLoops" value="true"/>
<message key="ws.notFollowed"
value="WhitespaceAround: ''{0}'' is not followed by whitespace. Empty blocks may only be represented as '{}' when not part of a multi-block statement (4.1.3)"/>
<message key="ws.notPreceded"
value="WhitespaceAround: ''{0}'' is not preceded with whitespace."/>
</module>
<module name="OneStatementPerLine"/>
<module name="MultipleVariableDeclarations"/>
<!-- <module name="ArrayTypeStyle"/> -->
Expand Down
8 changes: 4 additions & 4 deletions src/org/opendatakit/briefcase/export/CsvFieldMappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -181,7 +181,7 @@ private static Stream<Pair<String, String>> 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("")));
}

Expand All @@ -196,7 +196,7 @@ private static Stream<Pair<String, String>> 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));
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/org/opendatakit/briefcase/reused/Iso8601Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package org.opendatakit.briefcase.reused;

import java.time.OffsetDateTime;
import java.time.OffsetTime;

public class Iso8601Helpers {
private static String normalizeDateTime(String value) {
private static String normalizeOffset(String value) {
char charAtMinus3 = value.charAt(value.length() - 3);
if (value.endsWith("Z") || charAtMinus3 == ':')
return value;
Expand All @@ -29,6 +30,10 @@ private static String normalizeDateTime(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(normalizeOffset(value));
}
}
34 changes: 22 additions & 12 deletions test/java/org/opendatakit/briefcase/reused/Iso8601HelpersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Object[]> 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));
}
}

0 comments on commit d92b6c0

Please sign in to comment.