Skip to content

Commit

Permalink
Explicitly identify the value that can't be converted to a date (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel authored Oct 1, 2019
1 parent 84c3bdf commit 329514c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/javarosa/xpath/expr/XPathFuncExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ public static Object toDate(Object input, boolean preserveTime) {
}

if (n.isInfinite() || n > Integer.MAX_VALUE || n < Integer.MIN_VALUE) {
throw new XPathTypeMismatchException("converting out-of-range value to date");
throw new XPathTypeMismatchException("The value \"" + n + "\" is out of range for representing a date.");
}

long timeMillis = (long) (n * DateUtils.DAY_IN_MS);
Expand All @@ -833,7 +833,7 @@ public static Object toDate(Object input, boolean preserveTime) {
}

if (n.isInfinite() || n > Integer.MAX_VALUE || n < Integer.MIN_VALUE) {
throw new XPathTypeMismatchException("converting out-of-range value to date");
throw new XPathTypeMismatchException("The value \"" + n + "\" is out of range for representing a date.");
}

return DateUtils.dateAdd(DateUtils.getDate(1970, 1, 1), n.intValue());
Expand All @@ -847,7 +847,7 @@ public static Object toDate(Object input, boolean preserveTime) {

Date d = DateUtils.parseDateTime(s);
if (d == null) {
throw new XPathTypeMismatchException("converting to date");
throw new XPathTypeMismatchException("The value \"" + s + "\" can't be converted to a date.");
} else {
return d;
}
Expand All @@ -858,7 +858,7 @@ public static Object toDate(Object input, boolean preserveTime) {
return DateUtils.roundDate((Date) input);
}
} else {
throw new XPathTypeMismatchException("converting to date");
throw new XPathTypeMismatchException("The value \"" + input.toString() + "\" can't be converted to a date.");
}
}

Expand All @@ -873,7 +873,7 @@ public static Object toDecimalDateTime(Object o, boolean keepDate) {
}

if (n.isInfinite() || n > Integer.MAX_VALUE || n < Integer.MIN_VALUE) {
throw new XPathTypeMismatchException("converting out-of-range value to date");
throw new XPathTypeMismatchException("The value \"" + n + "\" is out of range for representing a date.");
}

if (keepDate) {
Expand All @@ -890,7 +890,7 @@ public static Object toDecimalDateTime(Object o, boolean keepDate) {

Date d = DateUtils.parseDateTime(s);
if (d == null) {
throw new XPathTypeMismatchException("converting to date");
throw new XPathTypeMismatchException("The value \"" + s + "\" can't be converted to a date.");
} else {
if (keepDate) {
long milli = d.getTime();
Expand All @@ -910,7 +910,7 @@ public static Object toDecimalDateTime(Object o, boolean keepDate) {
return DateUtils.decimalTimeOfLocalDay(d);
}
} else {
throw new XPathTypeMismatchException("converting to date");
throw new XPathTypeMismatchException("The value \"" + o.toString() + "\" can't be converted to a date.");
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/javarosa/xpath/test/XPathEvalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ should be tested (particularly DST changes), but it's just too hard and
testEval("date-time('2000-01-01T10:20:30.000')", DateUtils.getDateTimeFromString("2000-01-01T10:20:30.000"));
testEval("decimal-date-time('2000-01-01T10:20:30.000')", 10957.430902777778);
testEval("decimal-time('2000-01-01T10:20:30.000+03:00')", .30590277777810115);
testEval("decimal-date-time('-1000')", new XPathTypeMismatchException());
testEval("decimal-date-time('-01-2019')", new XPathTypeMismatchException());
}

@Test
Expand Down

0 comments on commit 329514c

Please sign in to comment.