Fix date parsing.

Closes #1564.
This commit is contained in:
Antonin Delpeuch 2018-04-22 10:49:22 +02:00
parent 3c4002ee2c
commit 1daa4d1312
2 changed files with 42 additions and 5 deletions

View File

@ -38,12 +38,14 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
@ -188,13 +190,31 @@ public class ParsingUtilities {
* @return LocalDateTime or null if the parse failed
*/
static public OffsetDateTime stringToDate(String s) {
Instant instant = Instant.parse(s);
return OffsetDateTime.ofInstant(instant, ZoneId.of("Z"));
// Accept timestamps with an explicit time zone
try {
return OffsetDateTime.parse(s);
} catch(DateTimeParseException e) {
}
// Also accept timestamps without an explicit zone and
// assume them to be in local time.
try {
LocalDateTime localTime = LocalDateTime.parse(s);
return OffsetDateTime.of(localTime, ZoneId.systemDefault().getRules().getOffset(localTime));
} catch(DateTimeParseException e) {
}
return null;
}
static public LocalDateTime stringToLocalDate(String s) {
Instant instant = Instant.parse(s);
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
// parse the string as a date and express it in local time
OffsetDateTime parsed = stringToDate(s);
if (parsed == null) {
return null;
}
return parsed.toLocalDateTime();
}
static public String instantToString(Instant instant) {

View File

@ -105,7 +105,24 @@ public class ParsingUtilitiesTests extends RefineTest {
OffsetDateTime zdt = ParsingUtilities.stringToDate(historyEntryDate);
String zdtString = ParsingUtilities.dateToString(zdt);
Assert.assertEquals(zdtString, historyEntryDate);
}
@Test
public void stringToDate() {
Assert.assertEquals(2017, ParsingUtilities.stringToDate("2017-04-03T08:09:43.123").getYear());
Assert.assertEquals(2017, ParsingUtilities.stringToDate("2017-04-03T08:09:43").getYear());
Assert.assertEquals(2017, ParsingUtilities.stringToDate("2017-04-03T08:09:43Z").getYear());
Assert.assertEquals(2017, ParsingUtilities.stringToDate("2017-04-03T08:09:43.123Z").getYear());
Assert.assertEquals(2017, ParsingUtilities.stringToDate("2017-04-03T08:09:43+00:00").getYear());
}
@Test
public void stringToLocalDate() {
Assert.assertEquals(2017, ParsingUtilities.stringToLocalDate("2017-04-03T08:09:43.123").getYear());
Assert.assertEquals(2017, ParsingUtilities.stringToLocalDate("2017-04-03T08:09:43").getYear());
Assert.assertEquals(2017, ParsingUtilities.stringToLocalDate("2017-04-03T08:09:43Z").getYear());
Assert.assertEquals(2017, ParsingUtilities.stringToLocalDate("2017-04-03T08:09:43.123Z").getYear());
Assert.assertEquals(2017, ParsingUtilities.stringToLocalDate("2017-04-03T08:09:43+00:00").getYear());
}
@Test