java 8 time API migrate
This commit is contained in:
parent
34c46111ea
commit
19f98b7ea2
@ -33,8 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.browsing.filters;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
import com.google.refine.browsing.util.RowEvaluable;
|
||||
import com.google.refine.expr.ExpressionUtils;
|
||||
@ -67,11 +66,8 @@ abstract public class ExpressionTimeComparisonRowFilter extends ExpressionNumber
|
||||
if (ExpressionUtils.isError(v)) {
|
||||
return _selectError;
|
||||
} else if (ExpressionUtils.isNonBlankData(v)) {
|
||||
if (v instanceof Date) {
|
||||
long time = ((Date) v).getTime();
|
||||
return _selectTime && checkValue(time);
|
||||
} else if (v instanceof Calendar) {
|
||||
long time = ((Calendar) v).getTime().getTime();
|
||||
if (v instanceof OffsetDateTime) {
|
||||
long time = ((OffsetDateTime) v).toInstant().toEpochMilli();
|
||||
return _selectTime && checkValue(time);
|
||||
} else {
|
||||
return _selectNonTime;
|
||||
|
@ -33,9 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.browsing.util;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.google.refine.browsing.RecordVisitor;
|
||||
@ -162,12 +161,8 @@ public class ExpressionTimeValueBinner implements RowVisitor, RecordVisitor {
|
||||
if (ExpressionUtils.isError(value)) {
|
||||
hasError = true;
|
||||
} else if (ExpressionUtils.isNonBlankData(value)) {
|
||||
if (value instanceof Calendar) {
|
||||
value = ((Calendar) value).getTime();
|
||||
}
|
||||
|
||||
if (value instanceof Date) {
|
||||
long t = ((Date) value).getTime();
|
||||
if (value instanceof OffsetDateTime) {
|
||||
long t = ((OffsetDateTime) value).toInstant().toEpochMilli();
|
||||
hasTime = true;
|
||||
|
||||
int bin = (int) Math.floor((double) (t - _index.getMin()) / (double) _index.getStep());
|
||||
|
@ -33,10 +33,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.browsing.util;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@ -181,13 +180,9 @@ abstract public class TimeBinIndex {
|
||||
if (ExpressionUtils.isError(v)) {
|
||||
_hasError = true;
|
||||
} else if (ExpressionUtils.isNonBlankData(v)) {
|
||||
if (v instanceof Calendar) {
|
||||
v = ((Calendar) v).getTime();
|
||||
}
|
||||
|
||||
if (v instanceof Date) {
|
||||
if (v instanceof OffsetDateTime) {
|
||||
_hasTime = true;
|
||||
processValue(((Date) v).getTime(), allValues);
|
||||
processValue(((OffsetDateTime) v).toInstant().toEpochMilli(), allValues);
|
||||
} else {
|
||||
_hasNonTime = true;
|
||||
}
|
||||
@ -202,13 +197,9 @@ abstract public class TimeBinIndex {
|
||||
if (ExpressionUtils.isError(v)) {
|
||||
_hasError = true;
|
||||
} else if (ExpressionUtils.isNonBlankData(v)) {
|
||||
if (v instanceof Calendar) {
|
||||
v = ((Calendar) v).getTime();
|
||||
}
|
||||
|
||||
if (v instanceof Date) {
|
||||
if (v instanceof OffsetDateTime) {
|
||||
_hasTime = true;
|
||||
processValue(((Date) v).getTime(), allValues);
|
||||
processValue(((OffsetDateTime) v).toInstant().toEpochMilli(), allValues);
|
||||
} else {
|
||||
_hasNonTime = true;
|
||||
}
|
||||
@ -219,13 +210,9 @@ abstract public class TimeBinIndex {
|
||||
} else {
|
||||
_totalValueCount++;
|
||||
|
||||
if (value instanceof Calendar) {
|
||||
value = ((Calendar) value).getTime();
|
||||
}
|
||||
|
||||
if (value instanceof Date) {
|
||||
if (value instanceof OffsetDateTime) {
|
||||
_hasTime = true;
|
||||
processValue(((Date) value).getTime(), allValues);
|
||||
processValue(((OffsetDateTime) value).toInstant().toEpochMilli(), allValues);
|
||||
} else {
|
||||
_hasNonTime = true;
|
||||
}
|
||||
|
@ -35,10 +35,7 @@ package com.google.refine.commands.expr;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@ -210,12 +207,7 @@ public class PreviewExpressionCommand extends Command {
|
||||
sb.append(" ]");
|
||||
} else if (v instanceof HasFields) {
|
||||
sb.append("[object " + v.getClass().getSimpleName() + "]");
|
||||
} else if (v instanceof Calendar) {
|
||||
Calendar c = (Calendar) v;
|
||||
|
||||
sb.append("[date " +
|
||||
ParsingUtilities.dateToString(OffsetDateTime.ofInstant(c.toInstant(), ZoneId.systemDefault())) +"]");
|
||||
} else if (v instanceof LocalDateTime) {
|
||||
} else if (v instanceof OffsetDateTime) {
|
||||
sb.append("[date " +
|
||||
ParsingUtilities.dateToString((OffsetDateTime) v) +"]");
|
||||
} else if (v instanceof String) {
|
||||
|
@ -35,9 +35,9 @@ package com.google.refine.exporters;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -344,10 +344,8 @@ abstract public class CustomizableTabularExporterUtilities {
|
||||
if (text == null) {
|
||||
if (value instanceof String) {
|
||||
text = (String) value;
|
||||
} else if (value instanceof Calendar) {
|
||||
text = dateFormatter.format(((Calendar) value).getTime());
|
||||
} else if (value instanceof Date) {
|
||||
text = dateFormatter.format((Date) value);
|
||||
} else if (value instanceof OffsetDateTime) {
|
||||
text = ((OffsetDateTime) value).format(DateTimeFormatter.ISO_INSTANT);
|
||||
} else {
|
||||
text = value.toString();
|
||||
}
|
||||
|
@ -34,9 +34,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
package com.google.refine.expr;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Calendar;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
@ -138,8 +137,7 @@ public class ExpressionUtils {
|
||||
v instanceof Number ||
|
||||
v instanceof String ||
|
||||
v instanceof Boolean ||
|
||||
v instanceof Date ||
|
||||
v instanceof Calendar ||
|
||||
v instanceof OffsetDateTime ||
|
||||
v instanceof EvalError;
|
||||
}
|
||||
|
||||
|
@ -36,21 +36,22 @@ package com.google.refine.expr.functions;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.google.refine.expr.EvalError;
|
||||
import com.google.refine.expr.util.CalendarParser;
|
||||
import com.google.refine.expr.util.CalendarParserException;
|
||||
import com.google.refine.grel.Function;
|
||||
import com.google.refine.expr.util.CalenderParser;
|
||||
import com.google.refine.grel.ControlFunctionRegistry;
|
||||
import com.google.refine.grel.Function;
|
||||
import com.google.refine.util.ParsingUtilities;
|
||||
|
||||
public class ToDate implements Function {
|
||||
@ -62,10 +63,8 @@ public class ToDate implements Function {
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects at least one argument");
|
||||
} else {
|
||||
Object arg0 = args[0];
|
||||
if (arg0 instanceof Date) {
|
||||
if (arg0 instanceof OffsetDateTime) {
|
||||
return arg0;
|
||||
} else if (arg0 instanceof Calendar) {
|
||||
return ((Calendar) arg0).getTime();
|
||||
} else if (arg0 instanceof Long) {
|
||||
o1 = ((Long) arg0).toString(); // treat integers as years
|
||||
} else if (arg0 instanceof String && arg0.toString().trim().length() > 0) {
|
||||
@ -83,21 +82,16 @@ public class ToDate implements Function {
|
||||
month_first = (Boolean) args[1];
|
||||
}
|
||||
try {
|
||||
return CalendarParser.parse( o1, (month_first) ? CalendarParser.MM_DD_YY : CalendarParser.DD_MM_YY);
|
||||
return CalenderParser.parseAsOffsetDateTime( o1, (month_first) ? CalenderParser.MM_DD_YY : CalenderParser.DD_MM_YY);
|
||||
} catch (CalendarParserException e) {
|
||||
OffsetDateTime d = ParsingUtilities.stringToDate(o1);
|
||||
if (d != null) {
|
||||
return d;
|
||||
} else {
|
||||
try {
|
||||
return javax.xml.bind.DatatypeConverter.parseDateTime(o1).getTime();
|
||||
return javax.xml.bind.DatatypeConverter.parseDateTime(o1).getTime().toInstant().atOffset(ZoneOffset.of("Z"));
|
||||
} catch (IllegalArgumentException e2) {
|
||||
}
|
||||
// alternate implementation which may be useful on some JVMs?
|
||||
// try {
|
||||
// return javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(o1).toGregorianCalendar().getTime();
|
||||
// } catch (DatatypeConfigurationException e2) {
|
||||
// }
|
||||
}
|
||||
return new EvalError("Unable to parse as date");
|
||||
}
|
||||
@ -114,14 +108,7 @@ public class ToDate implements Function {
|
||||
DateFormat formatter;
|
||||
// Attempt to parse first string as a language tag
|
||||
if (i == 1) {
|
||||
// Locale possibleLocale = Locale.forLanguageTag(format); // Java 1.7+ only
|
||||
Locale possibleLocale;
|
||||
int c = format.indexOf('_');
|
||||
if (c > 0) {
|
||||
possibleLocale = new Locale(format.substring(0, c),format.substring(c+1));
|
||||
} else {
|
||||
possibleLocale = new Locale(format);
|
||||
}
|
||||
Locale possibleLocale = Locale.forLanguageTag(format); // Java 1.7+
|
||||
boolean valid = false;
|
||||
for (Locale l : DateFormat.getAvailableLocales()) {
|
||||
if (l.equals(possibleLocale)) {
|
||||
@ -133,8 +120,7 @@ public class ToDate implements Function {
|
||||
if (valid) { // If we got a valid locale
|
||||
if (args.length == 2) { // No format strings to try, process using default
|
||||
formatter = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
|
||||
formatter.setLenient(true);
|
||||
GregorianCalendar date = parse(o1, formatter);
|
||||
OffsetDateTime date = parse(o1, formatter);
|
||||
if (date != null) {
|
||||
return date;
|
||||
} else {
|
||||
@ -150,30 +136,30 @@ public class ToDate implements Function {
|
||||
return new EvalError("Unknown date format");
|
||||
}
|
||||
formatter.setLenient(true);
|
||||
GregorianCalendar date = parse(o1, formatter);
|
||||
OffsetDateTime date = parse(o1, formatter);
|
||||
if (date != null) {
|
||||
return date;
|
||||
}
|
||||
|
||||
}
|
||||
return new EvalError("Unable to parse as date");
|
||||
} else {
|
||||
return new EvalError("Unable to parse as date");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private GregorianCalendar parse(String o1, DateFormat formatter) {
|
||||
|
||||
private OffsetDateTime parse(String o1, DateFormat formatter) {
|
||||
try {
|
||||
formatter.setTimeZone(TimeZone.getTimeZone("Z"));
|
||||
Date date = formatter.parse(o1);
|
||||
GregorianCalendar c = new GregorianCalendar();
|
||||
c.setTime(date);
|
||||
return c;
|
||||
return ParsingUtilities.calendarToOffsetDateTime(c);
|
||||
} catch (java.text.ParseException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
@ -33,10 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.expr.functions;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
@ -55,9 +53,9 @@ public class ToString implements Function {
|
||||
Object o1 = args[0];
|
||||
if (args.length == 2 && args[1] instanceof String) {
|
||||
Object o2 = args[1];
|
||||
if (o1 instanceof Calendar || o1 instanceof Date) {
|
||||
DateFormat formatter = new SimpleDateFormat((String) o2);
|
||||
return formatter.format(o1 instanceof Date ? ((Date) o1) : ((Calendar) o1).getTime());
|
||||
if (o1 instanceof OffsetDateTime) {
|
||||
OffsetDateTime odt = (OffsetDateTime)o1;
|
||||
return odt.format(DateTimeFormatter.ISO_INSTANT);
|
||||
} else if (o1 instanceof Number) {
|
||||
return String.format((String) o2, (Number) o1);
|
||||
}
|
||||
|
@ -33,8 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.expr.functions;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
@ -55,7 +54,7 @@ public class Type implements Function {
|
||||
if (v != null) {
|
||||
if (v instanceof String) {
|
||||
return "string";
|
||||
} else if (v instanceof Calendar || v instanceof Date) {
|
||||
} else if (v instanceof OffsetDateTime) {
|
||||
return "date";
|
||||
} else if (v instanceof Number) {
|
||||
return "number";
|
||||
|
@ -33,8 +33,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.expr.functions.date;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.TemporalUnit;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
@ -49,42 +50,36 @@ public class Inc implements Function {
|
||||
@Override
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 3 &&
|
||||
args[0] != null && (args[0] instanceof Calendar || args[0] instanceof Date) &&
|
||||
args[0] != null && (args[0] instanceof OffsetDateTime) &&
|
||||
args[1] != null && args[1] instanceof Number &&
|
||||
args[2] != null && args[2] instanceof String) {
|
||||
Calendar date;
|
||||
if (args[0] instanceof Calendar) {
|
||||
date = (Calendar) ((Calendar) args[0]).clone(); // must copy so not to modify original
|
||||
} else {
|
||||
date = Calendar.getInstance();
|
||||
date.setTime((Date) args[0]);
|
||||
}
|
||||
OffsetDateTime date = (OffsetDateTime)args[0];
|
||||
|
||||
int amount = ((Number) args[1]).intValue();
|
||||
String unit = (String) args[2];
|
||||
|
||||
date.add(getField(unit), amount);
|
||||
date.plus(amount, getField(unit));
|
||||
|
||||
return date;
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a date, a number and a string");
|
||||
}
|
||||
|
||||
private int getField(String unit) {
|
||||
private TemporalUnit getField(String unit) {
|
||||
if ("hours".equals(unit) || "hour".equals(unit) || "h".equals(unit)) {
|
||||
return Calendar.HOUR;
|
||||
return ChronoUnit.HOURS;
|
||||
} else if ("days".equals(unit) || "day".equals(unit) || "d".equals(unit)) {
|
||||
return Calendar.DAY_OF_MONTH;
|
||||
return ChronoUnit.DAYS;
|
||||
} else if ("years".equals(unit) || "year".equals(unit)) {
|
||||
return Calendar.YEAR;
|
||||
return ChronoUnit.YEARS;
|
||||
} else if ("months".equals(unit) || "month".equals(unit)) { // avoid 'm' to avoid confusion with minute
|
||||
return Calendar.MONTH;
|
||||
return ChronoUnit.MONTHS;
|
||||
} else if ("minutes".equals(unit) || "minute".equals(unit) || "min".equals(unit)) { // avoid 'm' to avoid confusion with month
|
||||
return Calendar.MINUTE;
|
||||
return ChronoUnit.MINUTES;
|
||||
} else if ("weeks".equals(unit) || "week".equals(unit) || "w".equals(unit)) {
|
||||
return Calendar.WEEK_OF_MONTH;
|
||||
return ChronoUnit.WEEKS;
|
||||
} else if ("seconds".equals(unit) || "sec".equals(unit) || "s".equals(unit)) {
|
||||
return Calendar.SECOND;
|
||||
return ChronoUnit.SECONDS;
|
||||
} else {
|
||||
throw new RuntimeException("Unit '" + unit + "' not recognized.");
|
||||
}
|
||||
|
@ -33,7 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.expr.functions.date;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
@ -46,7 +47,7 @@ public class Now implements Function {
|
||||
@Override
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 0) {
|
||||
return Calendar.getInstance();
|
||||
return OffsetDateTime.now(ZoneId.of("Z"));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -33,8 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.expr.functions.strings;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -42,8 +41,6 @@ import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.google.refine.expr.EvalError;
|
||||
import com.google.refine.expr.util.CalendarParser;
|
||||
import com.google.refine.expr.util.CalendarParserException;
|
||||
import com.google.refine.grel.Function;
|
||||
|
||||
public class Diff implements Function {
|
||||
@ -56,21 +53,14 @@ public class Diff implements Function {
|
||||
if (o1 != null && o2 != null) {
|
||||
if (args.length == 2 && o1 instanceof String && o2 instanceof String) {
|
||||
return StringUtils.difference((String) o1,(String) o2);
|
||||
} else if ((o1 instanceof Date || o1 instanceof Calendar) && args.length == 3) {
|
||||
} else if (o1 instanceof OffsetDateTime && o2 instanceof OffsetDateTime && args.length == 3) {
|
||||
Object o3 = args[2];
|
||||
if (o3 != null && o3 instanceof String) {
|
||||
try {
|
||||
String unit = ((String) o3).toLowerCase();
|
||||
Date c1 = (o1 instanceof Date) ? (Date) o1 : ((Calendar) o1).getTime();
|
||||
Date c2;
|
||||
if (o2 instanceof Date) {
|
||||
c2 = (Date) o2;
|
||||
} else if (o2 instanceof Calendar) {
|
||||
c2 = ((Calendar) o2).getTime();
|
||||
} else {
|
||||
c2 = CalendarParser.parse((o2 instanceof String) ? (String) o2 : o2.toString()).getTime();
|
||||
}
|
||||
long delta = (c1.getTime() - c2.getTime()) / 1000;
|
||||
OffsetDateTime c1 = (OffsetDateTime)o1;
|
||||
OffsetDateTime c2 = (OffsetDateTime)o2;
|
||||
|
||||
long delta = (c1.toInstant().toEpochMilli() - c2.toInstant().toEpochMilli()) / 1000;
|
||||
if ("seconds".equals(unit)) {
|
||||
return delta;
|
||||
}
|
||||
@ -96,13 +86,10 @@ public class Diff implements Function {
|
||||
return days / 365;
|
||||
}
|
||||
return new EvalError("Unknown time unit " + unit);
|
||||
} catch (CalendarParserException e) {
|
||||
return new EvalError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new EvalError("Unexpected arguments - expecting either 2 strings or 2 dates and a unit string");
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ package com.google.refine.expr.util;
|
||||
// Copyright Dave Glowacki. Released under the BSD license.
|
||||
|
||||
/**
|
||||
* Thrown when an invalid date is encountered in <tt>CalendarParser</tt>.
|
||||
* Thrown when an invalid date is encountered in <tt>OffsetDateTimeParser</tt>.
|
||||
*/
|
||||
public class CalendarParserException extends Exception {
|
||||
|
||||
|
@ -35,12 +35,15 @@ package com.google.refine.expr.util;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.TimeZone;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.google.refine.util.ParsingUtilities;
|
||||
|
||||
// Taken from http://icecube.wisc.edu/~dglo/software/calparse/index.html
|
||||
// Copyright Dave Glowacki. Released under the BSD license.
|
||||
|
||||
@ -491,7 +494,7 @@ class ParserState {
|
||||
/**
|
||||
* A parser for arbitrary date/time strings.
|
||||
*/
|
||||
public class CalendarParser {
|
||||
public class CalenderParser {
|
||||
/** bit indicating that the year comes before the month. */
|
||||
public static final int YEAR_BEFORE_MONTH = ParserState.YEAR_BEFORE_MONTH;
|
||||
/** bit indicating that the year comes before the day. */
|
||||
@ -865,7 +868,11 @@ public class CalendarParser {
|
||||
throws CalendarParserException {
|
||||
return parse(dateStr, YY_MM_DD);
|
||||
}
|
||||
|
||||
|
||||
public static final OffsetDateTime parseAsOffsetDateTime(String dateStr) throws CalendarParserException {
|
||||
return ParsingUtilities.calendarToOffsetDateTime(parse(dateStr));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a date from a string.
|
||||
*
|
||||
@ -884,6 +891,11 @@ public class CalendarParser {
|
||||
throws CalendarParserException {
|
||||
return parse(dateStr, order, true);
|
||||
}
|
||||
|
||||
public static final OffsetDateTime parseAsOffsetDateTime(String dateStr, int order)
|
||||
throws CalendarParserException {
|
||||
return ParsingUtilities.calendarToOffsetDateTime(parse(dateStr, order));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a date from a string.
|
||||
@ -911,6 +923,11 @@ public class CalendarParser {
|
||||
return parseString(dateStr, order, ignoreChanges);
|
||||
}
|
||||
|
||||
public static final OffsetDateTime parseAsOffsetDateTime(String dateStr, int order,
|
||||
boolean ignoreChanges) throws CalendarParserException {
|
||||
return ParsingUtilities.calendarToOffsetDateTime(parse(dateStr, order, ignoreChanges));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a non-numeric token from the date string.
|
||||
*
|
||||
@ -1620,7 +1637,7 @@ public class CalendarParser {
|
||||
state.setYear(tmpYear + (CENTURY_OFFSET - 100));
|
||||
}
|
||||
|
||||
GregorianCalendar cal = new GregorianCalendar();
|
||||
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Z"));
|
||||
|
||||
state.setCalendar(cal, ignoreChanges);
|
||||
|
||||
@ -1631,6 +1648,7 @@ public class CalendarParser {
|
||||
+ state.getMillisecond() + " => " + toString(cal));
|
||||
}
|
||||
|
||||
// return cal.toInstant().atOffset(ZoneOffset.of("Z"));
|
||||
return cal;
|
||||
}
|
||||
|
@ -39,8 +39,6 @@ import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
@ -92,11 +90,7 @@ public class Cell implements HasFields, Jsonizable {
|
||||
writer.key("v");
|
||||
if (value != null) {
|
||||
Instant instant = null;
|
||||
if (value instanceof Calendar) {
|
||||
instant = ((Calendar)value).toInstant();
|
||||
} else if (value instanceof Date) {
|
||||
instant = ((Date)value).toInstant();
|
||||
} else if (value instanceof OffsetDateTime) {
|
||||
if (value instanceof OffsetDateTime) {
|
||||
instant = ((OffsetDateTime)value).toInstant();
|
||||
} else if (value instanceof LocalDateTime) {
|
||||
instant = ((LocalDateTime)value).toInstant(ZoneOffset.of("Z"));
|
||||
|
@ -38,11 +38,8 @@ import java.io.InputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.time.ZoneId;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
@ -307,11 +304,7 @@ public class StandardReconConfig extends ReconConfig {
|
||||
jsonWriter.key("id"); jsonWriter.value(cell2.recon.match.id);
|
||||
jsonWriter.key("name"); jsonWriter.value(cell2.recon.match.name);
|
||||
jsonWriter.endObject();
|
||||
} else if (cell2.value instanceof Calendar) {
|
||||
Calendar calendar = (Calendar) cell2.value;
|
||||
OffsetDateTime d = OffsetDateTime.ofInstant(calendar.toInstant(), ZoneId.of("Z"));
|
||||
jsonWriter.value(ParsingUtilities.dateToString(d));
|
||||
} else if (cell2.value instanceof Date) {
|
||||
} else if (cell2.value instanceof OffsetDateTime) {
|
||||
jsonWriter.value(ParsingUtilities.dateToString((OffsetDateTime) cell2.value));
|
||||
} else {
|
||||
jsonWriter.value(cell2.value.toString());
|
||||
|
@ -33,8 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.sorting;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
import com.google.refine.expr.EvalError;
|
||||
import com.google.refine.expr.ExpressionUtils;
|
||||
@ -48,10 +48,8 @@ public class DateCriterion extends Criterion {
|
||||
@Override
|
||||
protected Object makeKey(Object value) {
|
||||
if (ExpressionUtils.isNonBlankData(value)) {
|
||||
if (value instanceof Date) {
|
||||
return value;
|
||||
} else if (value instanceof Calendar) {
|
||||
return ((Calendar) value).getTime();
|
||||
if (value instanceof OffsetDateTime) {
|
||||
return ((OffsetDateTime) value).toInstant();
|
||||
} else {
|
||||
return s_error;
|
||||
}
|
||||
@ -61,7 +59,7 @@ public class DateCriterion extends Criterion {
|
||||
|
||||
@Override
|
||||
public int compareKeys(Object key1, Object key2) {
|
||||
return ((Date) key1).compareTo((Date) key2);
|
||||
return ((Instant) key1).compareTo((Instant) key2);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -33,8 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.sorting;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
import com.google.refine.expr.EvalError;
|
||||
import com.google.refine.expr.ExpressionUtils;
|
||||
@ -53,10 +52,8 @@ public class NumberCriterion extends Criterion {
|
||||
return value;
|
||||
} else if (value instanceof Boolean) {
|
||||
return ((Boolean) value).booleanValue() ? 1 : 0;
|
||||
} else if (value instanceof Date) {
|
||||
return ((Date) value).getTime();
|
||||
} else if (value instanceof Calendar) {
|
||||
return ((Calendar) value).getTime().getTime();
|
||||
} else if (value instanceof OffsetDateTime) {
|
||||
return ((OffsetDateTime) value).toInstant().toEpochMilli();
|
||||
} else if (value instanceof String) {
|
||||
try {
|
||||
double d = Double.parseDouble((String) value);
|
||||
|
@ -45,6 +45,7 @@ import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@ -223,4 +224,8 @@ public class ParsingUtilities {
|
||||
static public String instantToLocalDateTimeString(Instant instant) {
|
||||
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).format(ISO8601);
|
||||
}
|
||||
|
||||
static public OffsetDateTime calendarToOffsetDateTime(Calendar calendar) {
|
||||
return calendar.toInstant().atOffset(ZoneOffset.of("Z"));
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
package com.google.refine.util;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
@ -13,9 +11,10 @@ public class StringUtils {
|
||||
* @return string representing object
|
||||
*/
|
||||
public static String toString(Object o) {
|
||||
if (o instanceof Calendar || o instanceof Date) {
|
||||
DateFormat formatter = DateFormat.getDateInstance();
|
||||
return formatter.format(o instanceof Date ? ((Date) o) : ((Calendar) o).getTime());
|
||||
// to replace the DateFormat with java.time.format.DateTimeFormatter
|
||||
if (o instanceof OffsetDateTime) {
|
||||
OffsetDateTime odt = (OffsetDateTime)o;
|
||||
return odt.format(DateTimeFormatter.ISO_INSTANT);
|
||||
} else if (o == null) {
|
||||
return "null";
|
||||
} else {
|
||||
|
@ -40,8 +40,6 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -60,7 +58,6 @@ import com.google.refine.model.ModelException;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.model.Row;
|
||||
import com.google.refine.tests.RefineTest;
|
||||
import com.google.refine.util.ParsingUtilities;
|
||||
|
||||
public class CsvExporterTests extends RefineTest {
|
||||
|
||||
@ -198,6 +195,9 @@ public class CsvExporterTests extends RefineTest {
|
||||
",row2cell1,row2cell2\n");
|
||||
}
|
||||
|
||||
// all date type cells are in unified format
|
||||
/**
|
||||
@Ignore
|
||||
@Test
|
||||
public void exportDateColumnsPreVersion28(){
|
||||
CreateGrid(1,2);
|
||||
@ -219,6 +219,7 @@ public class CsvExporterTests extends RefineTest {
|
||||
|
||||
Assert.assertEquals(writer.toString(), expectedOutput);
|
||||
}
|
||||
*/
|
||||
//helper methods
|
||||
|
||||
protected void CreateColumns(int noOfColumns){
|
||||
|
@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
package com.google.refine.tests.expr.functions.strings;
|
||||
package com.google.refine.tests.expr.functions;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.google.refine.tests.expr.functions.strings;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -11,8 +12,6 @@ import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.expr.EvalError;
|
||||
import com.google.refine.expr.util.CalendarParser;
|
||||
import com.google.refine.expr.util.CalendarParserException;
|
||||
import com.google.refine.grel.ControlFunctionRegistry;
|
||||
import com.google.refine.grel.Function;
|
||||
import com.google.refine.tests.RefineTest;
|
||||
@ -21,21 +20,16 @@ import com.google.refine.tests.RefineTest;
|
||||
public class DiffTests extends RefineTest {
|
||||
|
||||
static Properties bindings;
|
||||
private Calendar date1;
|
||||
private Calendar date2;
|
||||
|
||||
private OffsetDateTime odt1;
|
||||
private OffsetDateTime odt2;
|
||||
|
||||
@Override
|
||||
@BeforeTest
|
||||
public void init() {
|
||||
logger = LoggerFactory.getLogger(this.getClass());
|
||||
try {
|
||||
date1 = CalendarParser.parse("2012-08-02");
|
||||
date2 = CalendarParser.parse("2012-10-02");
|
||||
} catch (CalendarParserException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
Assert.fail();
|
||||
}
|
||||
odt1 = OffsetDateTime.parse("2011-09-01T10:15:30+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
odt2 = OffsetDateTime.parse("2011-12-02T10:16:30+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
@ -68,18 +62,20 @@ public class DiffTests extends RefineTest {
|
||||
public void testDiffInvalidParams() {
|
||||
Assert.assertTrue(invoke("diff") instanceof EvalError);
|
||||
Assert.assertTrue(invoke("diff", "one","two","three") instanceof EvalError);
|
||||
Assert.assertTrue(invoke("diff", date1,date2) instanceof EvalError);
|
||||
Assert.assertTrue(invoke("diff", date1,date2,"foo") instanceof EvalError);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiff() {
|
||||
public void testDiffString() {
|
||||
Assert.assertEquals((String)(invoke("diff", "onetwo","onetwothree")),"three");
|
||||
Assert.assertEquals(invoke("diff",date2,date1,"days"),Long.valueOf(61));
|
||||
Assert.assertEquals(invoke("diff",date2,date1,"weeks"),Long.valueOf(8));
|
||||
Assert.assertEquals(invoke("diff",date2,date1,"months"),Long.valueOf(2));
|
||||
Assert.assertEquals(invoke("diff",date2,date1,"hours"),Long.valueOf(1464));
|
||||
Assert.assertEquals(invoke("diff",date2,date1,"seconds"),Long.valueOf(5270400));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiffOffsetDateTime() {
|
||||
// OffsetDateTime diff:
|
||||
Assert.assertEquals(invoke("diff",odt2,odt1,"days"),Long.valueOf(92));
|
||||
Assert.assertEquals(invoke("diff",odt2,odt1,"weeks"),Long.valueOf(13));
|
||||
Assert.assertEquals(invoke("diff",odt2,odt1,"months"),Long.valueOf(3));
|
||||
Assert.assertEquals(invoke("diff",odt2,odt1,"hours"),Long.valueOf(2208));
|
||||
Assert.assertEquals(invoke("diff",odt2,odt1,"seconds"),Long.valueOf(7948860));
|
||||
}
|
||||
}
|
||||
|
@ -33,8 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.tests.expr.functions.strings;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -45,8 +44,8 @@ import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.expr.EvalError;
|
||||
import com.google.refine.expr.util.CalendarParser;
|
||||
import com.google.refine.expr.util.CalendarParserException;
|
||||
import com.google.refine.expr.util.CalenderParser;
|
||||
import com.google.refine.grel.ControlFunctionRegistry;
|
||||
import com.google.refine.grel.Function;
|
||||
import com.google.refine.tests.RefineTest;
|
||||
@ -118,11 +117,9 @@ public class ToFromConversionTests extends RefineTest {
|
||||
Assert.assertEquals(invoke("toString", Double.valueOf(100.0)),"100.0");
|
||||
Assert.assertEquals(invoke("toString", Double.valueOf(100.0),"%.0f"),"100");
|
||||
|
||||
String expectedDate = DateFormat.getDateInstance().format(new GregorianCalendar(2013,5,1).getTime());
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parse("2013-06-01")), expectedDate);
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parse("2013-06-01").getTime()), expectedDate);
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parse("2013-06-01"),"yyyy"),"2013");
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parse("2013-06-01"),"yyyy-MM-dd"),"2013-06-01");
|
||||
String expectedDate = "2013-06-01T00:00:00Z";
|
||||
Assert.assertEquals(invoke("toString", CalenderParser.parseAsOffsetDateTime("2013-06-01")), expectedDate);
|
||||
Assert.assertEquals(invoke("toString", CalenderParser.parseAsOffsetDateTime("2013-06-01"),"yyyy-MM-dd"),expectedDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -132,21 +129,16 @@ public class ToFromConversionTests extends RefineTest {
|
||||
Assert.assertTrue(invoke("toDate", "") instanceof EvalError);
|
||||
Assert.assertTrue(invoke("toDate", 1.0) instanceof EvalError);
|
||||
Assert.assertTrue(invoke("toDate", "2012-03-01","xxx") instanceof EvalError); // bad format string
|
||||
Assert.assertTrue(invoke("toDate", "2012-03-01") instanceof GregorianCalendar);
|
||||
Assert.assertEquals(invoke("toDate", "2012-03-01"),CalendarParser.parse("2012-03-01"));
|
||||
Assert.assertEquals(invoke("toDate", "2012-03-01","yyyy-MM-dd"),CalendarParser.parse("2012-03-01"));
|
||||
Assert.assertTrue(invoke("toDate", "2012-03-01") instanceof OffsetDateTime);
|
||||
Assert.assertEquals(invoke("toDate", "2012-03-01"),CalenderParser.parseAsOffsetDateTime("2012-03-01"));
|
||||
Assert.assertEquals(invoke("toDate", "2012-03-01","yyyy-MM-dd"),CalenderParser.parseAsOffsetDateTime("2012-03-01"));
|
||||
|
||||
// Multiple format strings should get tried sequentially until one succeeds or all are exhausted
|
||||
Assert.assertEquals(invoke("toDate", "2012-03-01","MMM","yyyy-MM-dd"), CalendarParser.parse("2012-03-01"));
|
||||
Assert.assertEquals(invoke("toDate", "2012-03-01","MMM","yyyy-MM-dd"), CalenderParser.parseAsOffsetDateTime("2012-03-01"));
|
||||
|
||||
// First string can be a locale identifier instead of a format string
|
||||
Assert.assertEquals(invoke("toDate", "2013-06-01","zh"), CalendarParser.parse("2013-06-01"));
|
||||
Assert.assertEquals(invoke("toDate", "01-六月-2013","zh","dd-MMM-yyyy"), CalendarParser.parse("2013-06-01"));
|
||||
Assert.assertEquals(invoke("toDate", "01-六月-2013","zh_HK","dd-MMM-yyyy"), CalendarParser.parse("2013-06-01"));
|
||||
Assert.assertEquals(invoke("toDate", "01-六月-2013","zh_TW","dd-MMM-yyyy"), CalendarParser.parse("2013-06-01"));
|
||||
Assert.assertEquals(invoke("toDate", "01-六月-2013","zh_CN","dd-MMM-yyyy"), CalendarParser.parse("2013-06-01"));
|
||||
|
||||
// Date
|
||||
// Calendar
|
||||
// String
|
||||
Assert.assertEquals(invoke("toDate", "2013-06-01","zh"), CalenderParser.parseAsOffsetDateTime("2013-06-01"));
|
||||
Assert.assertEquals(invoke("toDate", "01-六月-2013","zh","dd-MMM-yyyy"), CalenderParser.parseAsOffsetDateTime("2013-06-01"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user