Merge pull request #1880 from OpenRefine/issue1874
Avoid localizing time for dates with unspecified time.
This commit is contained in:
commit
5421a23fba
@ -37,6 +37,7 @@ import java.text.DateFormat;
|
|||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
@ -166,7 +167,9 @@ public class ToDate implements Function {
|
|||||||
return date;
|
return date;
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
return javax.xml.bind.DatatypeConverter.parseDateTime(o1).getTime().toInstant().atOffset(ZoneOffset.of("Z"));
|
return javax.xml.bind.DatatypeConverter.parseDateTime(o1).getTime().toInstant()
|
||||||
|
.plusSeconds(ZonedDateTime.now().getOffset().getTotalSeconds())
|
||||||
|
.atOffset(ZoneOffset.of("Z"));
|
||||||
} catch (IllegalArgumentException e2) {
|
} catch (IllegalArgumentException e2) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -229,7 +229,8 @@ public class ParsingUtilities {
|
|||||||
if (parsed == null) {
|
if (parsed == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return parsed.toLocalDateTime();
|
return parsed.withOffsetSameInstant(OffsetDateTime.now().getOffset())
|
||||||
|
.toLocalDateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
static public String instantToString(Instant instant) {
|
static public String instantToString(Instant instant) {
|
||||||
|
@ -35,6 +35,7 @@ package com.google.refine.tests.expr.functions.strings;
|
|||||||
|
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
@ -49,6 +50,7 @@ import com.google.refine.expr.util.CalendarParserException;
|
|||||||
import com.google.refine.grel.ControlFunctionRegistry;
|
import com.google.refine.grel.ControlFunctionRegistry;
|
||||||
import com.google.refine.grel.Function;
|
import com.google.refine.grel.Function;
|
||||||
import com.google.refine.tests.RefineTest;
|
import com.google.refine.tests.RefineTest;
|
||||||
|
import com.google.refine.util.ParsingUtilities;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -161,10 +163,10 @@ public class ToFromConversionTests extends RefineTest {
|
|||||||
Assert.assertEquals(invoke("toDate", "01-六月-2013","zh","dd-MMM-yyyy"), CalendarParser.parseAsOffsetDateTime("2013-06-01"));
|
Assert.assertEquals(invoke("toDate", "01-六月-2013","zh","dd-MMM-yyyy"), CalendarParser.parseAsOffsetDateTime("2013-06-01"));
|
||||||
|
|
||||||
//if invalid format/locale strings are passed, ignore them
|
//if invalid format/locale strings are passed, ignore them
|
||||||
Assert.assertEquals(invoke("toDate", "2012-03-01","XXX"), CalendarParser.parseAsOffsetDateTime("2012-03-01"));
|
Assert.assertEquals(invoke("toDate", "2012-03-01","XXX"), invoke("toDate", "2012-03-01"));
|
||||||
|
|
||||||
// If a long, convert to string
|
// If a long, convert to string
|
||||||
Assert.assertEquals(invoke("toDate", (long) 2012), CalendarParser.parseAsOffsetDateTime("2012-01-01"));
|
Assert.assertEquals(invoke("toDate", (long) 2012), invoke("toDate", "2012-01-01"));
|
||||||
|
|
||||||
// If already a date, leave it alone
|
// If already a date, leave it alone
|
||||||
Assert.assertEquals(invoke("toDate", CalendarParser.parseAsOffsetDateTime("2012-03-01")),CalendarParser.parseAsOffsetDateTime("2012-03-01"));
|
Assert.assertEquals(invoke("toDate", CalendarParser.parseAsOffsetDateTime("2012-03-01")),CalendarParser.parseAsOffsetDateTime("2012-03-01"));
|
||||||
|
@ -2,24 +2,46 @@ package com.google.refine.tests.io;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.testng.annotations.BeforeSuite;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
import com.google.refine.tests.util.TestUtils;
|
import com.google.refine.tests.util.TestUtils;
|
||||||
import com.google.refine.util.ParsingUtilities;
|
import com.google.refine.util.ParsingUtilities;
|
||||||
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
import com.google.refine.ProjectMetadata;
|
import com.google.refine.ProjectMetadata;
|
||||||
|
|
||||||
public class ProjectMetadataTests {
|
public class ProjectMetadataTests {
|
||||||
|
|
||||||
|
private String jsonSaveMode = null;
|
||||||
|
private String jsonNonSaveMode = null;
|
||||||
|
|
||||||
|
@BeforeSuite
|
||||||
|
public void setUpJson() throws IOException {
|
||||||
|
InputStream f = ProjectMetadataTests.class.getClassLoader().getResourceAsStream("example_project_metadata.json");
|
||||||
|
jsonNonSaveMode = IOUtils.toString(f);
|
||||||
|
f = ProjectMetadataTests.class.getClassLoader().getResourceAsStream("example_project_metadata_save_mode.json");
|
||||||
|
jsonSaveMode = IOUtils.toString(f);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serializeProjectMetadata() throws IOException {
|
public void serializeProjectMetadata() throws IOException {
|
||||||
InputStream f = ProjectMetadataTests.class.getClassLoader().getResourceAsStream("example_project_metadata.json");
|
ProjectMetadata metadata = ParsingUtilities.mapper.readValue(jsonSaveMode, ProjectMetadata.class);
|
||||||
String json = IOUtils.toString(f);
|
TestUtils.isSerializedTo(metadata, jsonNonSaveMode);
|
||||||
f = ProjectMetadataTests.class.getClassLoader().getResourceAsStream("example_project_metadata_save_mode.json");
|
TestUtils.isSerializedTo(metadata, jsonSaveMode, true);
|
||||||
String fullJson = IOUtils.toString(f);
|
}
|
||||||
|
|
||||||
f = ProjectMetadataTests.class.getClassLoader().getResourceAsStream("example_project_metadata_save_mode.json");
|
@Test
|
||||||
ProjectMetadata metadata = ParsingUtilities.mapper.readValue(f, ProjectMetadata.class);
|
public void serializeProjectMetadataInDifferentTimezone() throws JsonParseException, JsonMappingException, IOException {
|
||||||
TestUtils.isSerializedTo(metadata, json);
|
TimeZone.setDefault(TimeZone.getTimeZone("JST"));
|
||||||
TestUtils.isSerializedTo(metadata, fullJson, true);
|
try {
|
||||||
|
ProjectMetadata metadata = ParsingUtilities.mapper.readValue(jsonSaveMode, ProjectMetadata.class);
|
||||||
|
TestUtils.isSerializedTo(metadata, jsonNonSaveMode);
|
||||||
|
TestUtils.isSerializedTo(metadata, jsonSaveMode, true);
|
||||||
|
} finally {
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ import java.time.OffsetDateTime;
|
|||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.apache.commons.lang3.text.StrSubstitutor;
|
import org.apache.commons.lang3.text.StrSubstitutor;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -89,6 +90,23 @@ public class ParsingUtilitiesTests extends RefineTest {
|
|||||||
Assert.assertEquals(2017, ParsingUtilities.stringToLocalDate("2017-04-03T08:09:43+00:00").getYear());
|
Assert.assertEquals(2017, ParsingUtilities.stringToLocalDate("2017-04-03T08:09:43+00:00").getYear());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converting between string and local time must be reversible, no matter the timezone.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void stringToLocalDateNonUTC() {
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("JST"));
|
||||||
|
try {
|
||||||
|
Assert.assertEquals(ParsingUtilities.stringToLocalDate("2001-08-12T00:00:00Z").getHour(), 9);
|
||||||
|
Assert.assertEquals(ParsingUtilities.localDateToString(
|
||||||
|
ParsingUtilities.stringToLocalDate("2001-08-12T00:00:00Z")),
|
||||||
|
"2001-08-12T00:00:00Z");
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parseProjectModifiedBeforeJDK8() {
|
public void parseProjectModifiedBeforeJDK8() {
|
||||||
String modified = "2014-01-15T21:46:25Z";
|
String modified = "2014-01-15T21:46:25Z";
|
||||||
|
Loading…
Reference in New Issue
Block a user