use of com.google.refine.expr.util.CalendarParserException in project OpenRefine by OpenRefine.
the class ToDate method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 0) {
// missing value, can this happen?
return null;
}
Object arg0 = args[0];
String o1;
if (arg0 instanceof Date) {
return arg0;
} else if (arg0 instanceof Calendar) {
return ((Calendar) arg0).getTime();
} else if (arg0 instanceof Long) {
// treat integers as years
o1 = ((Long) arg0).toString();
} else if (arg0 instanceof String) {
o1 = (String) arg0;
} else {
// ignore cell values that aren't strings
return new EvalError("Not a String - cannot parse to date");
}
// "o, boolean month_first (optional)"
if (args.length == 1 || (args.length == 2 && args[1] instanceof Boolean)) {
boolean month_first = true;
if (args.length == 2) {
month_first = (Boolean) args[1];
}
try {
return CalendarParser.parse(o1, (month_first) ? CalendarParser.MM_DD_YY : CalendarParser.DD_MM_YY);
} catch (CalendarParserException e) {
Date d = ParsingUtilities.stringToDate(o1);
if (d != null) {
return d;
} else {
try {
return javax.xml.bind.DatatypeConverter.parseDateTime(o1).getTime();
} 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("Cannot parse to date");
}
}
// "o, format1, format2 (optional), ..."
Locale locale = Locale.getDefault();
if (args.length >= 2) {
for (int i = 1; i < args.length; i++) {
if (!(args[i] instanceof String)) {
// skip formats that aren't strings
continue;
}
String format = StringUtils.trim((String) args[i]);
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);
}
boolean valid = false;
for (Locale l : DateFormat.getAvailableLocales()) {
if (l.equals(possibleLocale)) {
locale = possibleLocale;
valid = true;
break;
}
}
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);
if (date != null) {
return date;
} else {
return new EvalError("Unable to parse as date");
}
}
// Don't try to process locale string as a format string if it was valid
continue;
}
}
try {
formatter = new SimpleDateFormat(format, locale);
} catch (IllegalArgumentException e) {
return new EvalError("Unknown date format");
}
formatter.setLenient(true);
GregorianCalendar date = parse(o1, formatter);
if (date != null) {
return date;
}
}
return new EvalError("Unable to parse as date");
}
return null;
}
use of com.google.refine.expr.util.CalendarParserException in project OpenRefine by OpenRefine.
the class Diff method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length >= 2) {
Object o1 = args[0];
Object o2 = args[1];
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) {
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;
if ("seconds".equals(unit)) {
return delta;
}
delta /= 60;
if ("minutes".equals(unit)) {
return delta;
}
delta /= 60;
if ("hours".equals(unit)) {
return delta;
}
long days = delta / 24;
if ("days".equals(unit)) {
return days;
}
if ("weeks".equals(unit)) {
return days / 7;
}
if ("months".equals(unit)) {
return days / 30;
}
if ("years".equals(unit)) {
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");
}
Aggregations