Fixed: issue 1998

This commit is contained in:
Krzysztof 'impune-pl' Prorok 2019-06-04 17:01:25 +02:00
parent 68b0bbcd92
commit ae2f44f9d5

View File

@ -34,6 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.expr.functions.strings;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
@ -57,43 +58,38 @@ public class Diff implements Function {
String unit = ((String) o3).toLowerCase();
OffsetDateTime c1 = (OffsetDateTime)o1;
OffsetDateTime c2 = (OffsetDateTime)o2;
long delta = getNano(c1) - getNano(c2);
try {
if ("nanos".equals(unit)) {
return delta;
return ChronoUnit.NANOS.between(c2, c1);
}
delta /= 1000;
if ("milliseconds".equals(unit)) {
return delta;
return ChronoUnit.MILLIS.between(c2, c1);
}
delta /= 1000000;
if ("seconds".equals(unit)) {
return delta;
return ChronoUnit.SECONDS.between(c2, c1);
}
delta /= 60;
if ("minutes".equals(unit)) {
return delta;
return ChronoUnit.MINUTES.between(c2, c1);
}
delta /= 60;
if ("hours".equals(unit)) {
return delta;
return ChronoUnit.HOURS.between(c2, c1);
}
long days = delta / 24;
if ("days".equals(unit)) {
return days;
return ChronoUnit.DAYS.between(c2, c1);
}
if ("weeks".equals(unit)) {
return days / 7;
return ChronoUnit.WEEKS.between(c2, c1);
}
if ("months".equals(unit)) {
return days / 30;
return ChronoUnit.MONTHS.between(c2, c1);
}
if ("years".equals(unit)) {
return days / 365;
return ChronoUnit.YEARS.between(c2, c1);
}
return new EvalError("Unknown time unit " + unit);
} catch (ArithmeticException arithmeticException) {
return new EvalError("Number of " + unit + " between given dates causes long overflow.");
}
}
}
}
@ -115,8 +111,4 @@ public class Diff implements Function {
public String getReturns() {
return "string for strings, number for dates";
}
private long getNano(OffsetDateTime odt) {
return odt.toEpochSecond() * 1000000000l + odt.toInstant().getNano();
}
}