Attempts to parse a timestamp from a given string Formats accepted are: Relative: 5m-ago, 1h-ago, etc. See #parseDuration Absolute human readable dates: "yyyy/MM/dd-HH:mm:ss" "yyyy/MM/dd HH:mm:ss" "yyyy/MM/dd-HH:mm" "yyyy/M
(final String datetime,
final String tz)
| 74 | * @throws IllegalArgumentException if the request was malformed |
| 75 | */ |
| 76 | public static final long parseDateTimeString(final String datetime, |
| 77 | final String tz) { |
| 78 | if (datetime == null || datetime.isEmpty()) |
| 79 | return -1; |
| 80 | |
| 81 | if (datetime.matches("^[0-9]+ms$")) { |
| 82 | return Tags.parseLong(datetime.replaceFirst("^([0-9]+)(ms)$", "$1")); |
| 83 | } |
| 84 | |
| 85 | if (datetime.toLowerCase().equals("now")) { |
| 86 | return System.currentTimeMillis(); |
| 87 | } |
| 88 | |
| 89 | if (datetime.toLowerCase().endsWith("-ago")) { |
| 90 | long interval = DateTime.parseDuration( |
| 91 | datetime.substring(0, datetime.length() - 4)); |
| 92 | return System.currentTimeMillis() - interval; |
| 93 | } |
| 94 | |
| 95 | if (datetime.contains("/") || datetime.contains(":")) { |
| 96 | try { |
| 97 | SimpleDateFormat fmt = null; |
| 98 | switch (datetime.length()) { |
| 99 | // these were pulled from cliQuery but don't work as intended since |
| 100 | // they assume a date of 1970/01/01. Can be fixed but may not be worth |
| 101 | // it |
| 102 | // case 5: |
| 103 | // fmt = new SimpleDateFormat("HH:mm"); |
| 104 | // break; |
| 105 | // case 8: |
| 106 | // fmt = new SimpleDateFormat("HH:mm:ss"); |
| 107 | // break; |
| 108 | case 10: |
| 109 | fmt = new SimpleDateFormat("yyyy/MM/dd"); |
| 110 | break; |
| 111 | case 16: |
| 112 | if (datetime.contains("-")) |
| 113 | fmt = new SimpleDateFormat("yyyy/MM/dd-HH:mm"); |
| 114 | else |
| 115 | fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm"); |
| 116 | break; |
| 117 | case 19: |
| 118 | if (datetime.contains("-")) |
| 119 | fmt = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss"); |
| 120 | else |
| 121 | fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); |
| 122 | break; |
| 123 | default: |
| 124 | // todo - deal with internationalization, other time formats |
| 125 | throw new IllegalArgumentException("Invalid absolute date: " |
| 126 | + datetime); |
| 127 | } |
| 128 | if (tz != null && !tz.isEmpty()) |
| 129 | setTimeZone(fmt, tz); |
| 130 | return fmt.parse(datetime).getTime(); |
| 131 | } catch (ParseException e) { |
| 132 | throw new IllegalArgumentException("Invalid date: " + datetime |
| 133 | + ". " + e.getMessage()); |