Skip to content

Commit

Permalink
Don't throw unnecessary exceptions when parsing timestamps.
Browse files Browse the repository at this point in the history
Change-Id: Ib766c8e197f42331f8620c05280757aa10f3d591
  • Loading branch information
tsuna committed Feb 23, 2012
1 parent 23e05f4 commit fed8fbc
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/tsd/GraphHandler.java
Expand Up @@ -950,18 +950,20 @@ private static long getQueryStringDate(final HttpQuery query,
- parseDuration(date.substring(0, date.length() - 4)));
}
long timestamp;
try {
timestamp = Long.parseLong(date); // Is it already a timestamp?
} catch (NumberFormatException ne) { // Nope, try to parse a date then.
if (date.length() < 5 || date.charAt(4) != '/') { // Already a timestamp?
try {
timestamp = Tags.parseLong(date); // => Looks like it.
} catch (NumberFormatException e) {
throw new BadRequestException("Invalid " + paramname + " time: " + date
+ ". " + e.getMessage());
}
} else { // => Nope, there is a slash, so parse a date then.
try {
final SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");
timestamp = fmt.parse(date).getTime() / 1000;
} catch (ParseException e) {
throw new BadRequestException("Invalid " + paramname + " date: " + date
+ ". " + e.getMessage());
} catch (NumberFormatException e) {
throw new BadRequestException("Invalid " + paramname + " date: " + date
+ ". " + e.getMessage());
}
}
if (timestamp < 0) {
Expand Down

0 comments on commit fed8fbc

Please sign in to comment.