@Test public void testRfc5424DateParsing() { final String[] examples = { "1985-04-12T23:20:50.52Z", "1985-04-12T19:20:50.52-04:00", "2003-10-11T22:14:15.003Z", "2003-08-24T05:14:15.000003-07:00", "2012-04-13T11:11:11-08:00", "2012-04-13T08:08:08.0001+00:00" }; SyslogParser parser = new SyslogParser(); DateTimeFormatter jodaParser = ISODateTimeFormat.dateTimeParser(); for (String ex : examples) { Assert.assertEquals( "Problem parsing date string: " + ex, jodaParser.parseMillis(ex), parser.parseRfc5424Date(ex)); } }
protected long parseRfc5424Date(String msg) { Long ts = null; int curPos = 0; int msgLen = msg.length(); Preconditions.checkArgument(msgLen > RFC5424_PREFIX_LEN, "Bad format: Not a valid RFC5424 timestamp: %s", msg); String timestampPrefix = msg.substring(curPos, RFC5424_PREFIX_LEN); try { ts = timestampCache.get(timestampPrefix); } catch (ExecutionException ex) { throw new IllegalArgumentException("bad timestamp format", ex); } curPos += RFC5424_PREFIX_LEN; Preconditions.checkArgument(ts != null, "Parsing error: timestamp is null"); if (msg.charAt(curPos) == '.') { boolean foundEnd = false; int endMillisPos = curPos + 1; if (msgLen <= endMillisPos) { throw new IllegalArgumentException("bad timestamp format (no TZ)"); } while (!foundEnd) { char curDigit = msg.charAt(endMillisPos); if (curDigit >= '0' && curDigit <= '9') { endMillisPos++; } else { foundEnd = true; } } if (endMillisPos - (curPos + 1) > 0) { float frac = Float.parseFloat(msg.substring(curPos, endMillisPos)); long milliseconds = (long) (frac * 1000f); ts += milliseconds; } else { throw new IllegalArgumentException( "Bad format: Invalid timestamp (fractional portion): " + msg); } curPos = endMillisPos; } char tzFirst = msg.charAt(curPos); if (tzFirst == 'Z') { } else if (tzFirst == '+' || tzFirst == '-') { Preconditions.checkArgument(msgLen > curPos + 5, "Bad format: Invalid timezone (%s)", msg); int polarity; if (tzFirst == '+') { polarity = +1; } else { polarity = -1; } char[] h = new char[5]; for (int i = 0; i < 5; i++) { h[i] = msg.charAt(curPos + 1 + i); } if (h[0] >= '0' && h[0] <= '9' && h[1] >= '0' && h[1] <= '9' && h[2] == ':' && h[3] >= '0' && h[3] <= '9' && h[4] >= '0' && h[4] <= '9') { int hourOffset = Integer.parseInt(msg.substring(curPos + 1, curPos + 3)); int minOffset = Integer.parseInt(msg.substring(curPos + 4, curPos + 6)); ts -= polarity * ((hourOffset * 60) + minOffset) * 60000; } else { throw new IllegalArgumentException( "Bad format: Invalid timezone: " + msg); } } return ts; }
SyslogParser { protected long parseRfc5424Date(String msg) { Long ts = null; int curPos = 0; int msgLen = msg.length(); Preconditions.checkArgument(msgLen > RFC5424_PREFIX_LEN, "Bad format: Not a valid RFC5424 timestamp: %s", msg); String timestampPrefix = msg.substring(curPos, RFC5424_PREFIX_LEN); try { ts = timestampCache.get(timestampPrefix); } catch (ExecutionException ex) { throw new IllegalArgumentException("bad timestamp format", ex); } curPos += RFC5424_PREFIX_LEN; Preconditions.checkArgument(ts != null, "Parsing error: timestamp is null"); if (msg.charAt(curPos) == '.') { boolean foundEnd = false; int endMillisPos = curPos + 1; if (msgLen <= endMillisPos) { throw new IllegalArgumentException("bad timestamp format (no TZ)"); } while (!foundEnd) { char curDigit = msg.charAt(endMillisPos); if (curDigit >= '0' && curDigit <= '9') { endMillisPos++; } else { foundEnd = true; } } if (endMillisPos - (curPos + 1) > 0) { float frac = Float.parseFloat(msg.substring(curPos, endMillisPos)); long milliseconds = (long) (frac * 1000f); ts += milliseconds; } else { throw new IllegalArgumentException( "Bad format: Invalid timestamp (fractional portion): " + msg); } curPos = endMillisPos; } char tzFirst = msg.charAt(curPos); if (tzFirst == 'Z') { } else if (tzFirst == '+' || tzFirst == '-') { Preconditions.checkArgument(msgLen > curPos + 5, "Bad format: Invalid timezone (%s)", msg); int polarity; if (tzFirst == '+') { polarity = +1; } else { polarity = -1; } char[] h = new char[5]; for (int i = 0; i < 5; i++) { h[i] = msg.charAt(curPos + 1 + i); } if (h[0] >= '0' && h[0] <= '9' && h[1] >= '0' && h[1] <= '9' && h[2] == ':' && h[3] >= '0' && h[3] <= '9' && h[4] >= '0' && h[4] <= '9') { int hourOffset = Integer.parseInt(msg.substring(curPos + 1, curPos + 3)); int minOffset = Integer.parseInt(msg.substring(curPos + 4, curPos + 6)); ts -= polarity * ((hourOffset * 60) + minOffset) * 60000; } else { throw new IllegalArgumentException( "Bad format: Invalid timezone: " + msg); } } return ts; } }
SyslogParser { protected long parseRfc5424Date(String msg) { Long ts = null; int curPos = 0; int msgLen = msg.length(); Preconditions.checkArgument(msgLen > RFC5424_PREFIX_LEN, "Bad format: Not a valid RFC5424 timestamp: %s", msg); String timestampPrefix = msg.substring(curPos, RFC5424_PREFIX_LEN); try { ts = timestampCache.get(timestampPrefix); } catch (ExecutionException ex) { throw new IllegalArgumentException("bad timestamp format", ex); } curPos += RFC5424_PREFIX_LEN; Preconditions.checkArgument(ts != null, "Parsing error: timestamp is null"); if (msg.charAt(curPos) == '.') { boolean foundEnd = false; int endMillisPos = curPos + 1; if (msgLen <= endMillisPos) { throw new IllegalArgumentException("bad timestamp format (no TZ)"); } while (!foundEnd) { char curDigit = msg.charAt(endMillisPos); if (curDigit >= '0' && curDigit <= '9') { endMillisPos++; } else { foundEnd = true; } } if (endMillisPos - (curPos + 1) > 0) { float frac = Float.parseFloat(msg.substring(curPos, endMillisPos)); long milliseconds = (long) (frac * 1000f); ts += milliseconds; } else { throw new IllegalArgumentException( "Bad format: Invalid timestamp (fractional portion): " + msg); } curPos = endMillisPos; } char tzFirst = msg.charAt(curPos); if (tzFirst == 'Z') { } else if (tzFirst == '+' || tzFirst == '-') { Preconditions.checkArgument(msgLen > curPos + 5, "Bad format: Invalid timezone (%s)", msg); int polarity; if (tzFirst == '+') { polarity = +1; } else { polarity = -1; } char[] h = new char[5]; for (int i = 0; i < 5; i++) { h[i] = msg.charAt(curPos + 1 + i); } if (h[0] >= '0' && h[0] <= '9' && h[1] >= '0' && h[1] <= '9' && h[2] == ':' && h[3] >= '0' && h[3] <= '9' && h[4] >= '0' && h[4] <= '9') { int hourOffset = Integer.parseInt(msg.substring(curPos + 1, curPos + 3)); int minOffset = Integer.parseInt(msg.substring(curPos + 4, curPos + 6)); ts -= polarity * ((hourOffset * 60) + minOffset) * 60000; } else { throw new IllegalArgumentException( "Bad format: Invalid timezone: " + msg); } } return ts; } SyslogParser(); }
SyslogParser { protected long parseRfc5424Date(String msg) { Long ts = null; int curPos = 0; int msgLen = msg.length(); Preconditions.checkArgument(msgLen > RFC5424_PREFIX_LEN, "Bad format: Not a valid RFC5424 timestamp: %s", msg); String timestampPrefix = msg.substring(curPos, RFC5424_PREFIX_LEN); try { ts = timestampCache.get(timestampPrefix); } catch (ExecutionException ex) { throw new IllegalArgumentException("bad timestamp format", ex); } curPos += RFC5424_PREFIX_LEN; Preconditions.checkArgument(ts != null, "Parsing error: timestamp is null"); if (msg.charAt(curPos) == '.') { boolean foundEnd = false; int endMillisPos = curPos + 1; if (msgLen <= endMillisPos) { throw new IllegalArgumentException("bad timestamp format (no TZ)"); } while (!foundEnd) { char curDigit = msg.charAt(endMillisPos); if (curDigit >= '0' && curDigit <= '9') { endMillisPos++; } else { foundEnd = true; } } if (endMillisPos - (curPos + 1) > 0) { float frac = Float.parseFloat(msg.substring(curPos, endMillisPos)); long milliseconds = (long) (frac * 1000f); ts += milliseconds; } else { throw new IllegalArgumentException( "Bad format: Invalid timestamp (fractional portion): " + msg); } curPos = endMillisPos; } char tzFirst = msg.charAt(curPos); if (tzFirst == 'Z') { } else if (tzFirst == '+' || tzFirst == '-') { Preconditions.checkArgument(msgLen > curPos + 5, "Bad format: Invalid timezone (%s)", msg); int polarity; if (tzFirst == '+') { polarity = +1; } else { polarity = -1; } char[] h = new char[5]; for (int i = 0; i < 5; i++) { h[i] = msg.charAt(curPos + 1 + i); } if (h[0] >= '0' && h[0] <= '9' && h[1] >= '0' && h[1] <= '9' && h[2] == ':' && h[3] >= '0' && h[3] <= '9' && h[4] >= '0' && h[4] <= '9') { int hourOffset = Integer.parseInt(msg.substring(curPos + 1, curPos + 3)); int minOffset = Integer.parseInt(msg.substring(curPos + 4, curPos + 6)); ts -= polarity * ((hourOffset * 60) + minOffset) * 60000; } else { throw new IllegalArgumentException( "Bad format: Invalid timezone: " + msg); } } return ts; } SyslogParser(); Event parseMessage(String msg, Charset charset); }
SyslogParser { protected long parseRfc5424Date(String msg) { Long ts = null; int curPos = 0; int msgLen = msg.length(); Preconditions.checkArgument(msgLen > RFC5424_PREFIX_LEN, "Bad format: Not a valid RFC5424 timestamp: %s", msg); String timestampPrefix = msg.substring(curPos, RFC5424_PREFIX_LEN); try { ts = timestampCache.get(timestampPrefix); } catch (ExecutionException ex) { throw new IllegalArgumentException("bad timestamp format", ex); } curPos += RFC5424_PREFIX_LEN; Preconditions.checkArgument(ts != null, "Parsing error: timestamp is null"); if (msg.charAt(curPos) == '.') { boolean foundEnd = false; int endMillisPos = curPos + 1; if (msgLen <= endMillisPos) { throw new IllegalArgumentException("bad timestamp format (no TZ)"); } while (!foundEnd) { char curDigit = msg.charAt(endMillisPos); if (curDigit >= '0' && curDigit <= '9') { endMillisPos++; } else { foundEnd = true; } } if (endMillisPos - (curPos + 1) > 0) { float frac = Float.parseFloat(msg.substring(curPos, endMillisPos)); long milliseconds = (long) (frac * 1000f); ts += milliseconds; } else { throw new IllegalArgumentException( "Bad format: Invalid timestamp (fractional portion): " + msg); } curPos = endMillisPos; } char tzFirst = msg.charAt(curPos); if (tzFirst == 'Z') { } else if (tzFirst == '+' || tzFirst == '-') { Preconditions.checkArgument(msgLen > curPos + 5, "Bad format: Invalid timezone (%s)", msg); int polarity; if (tzFirst == '+') { polarity = +1; } else { polarity = -1; } char[] h = new char[5]; for (int i = 0; i < 5; i++) { h[i] = msg.charAt(curPos + 1 + i); } if (h[0] >= '0' && h[0] <= '9' && h[1] >= '0' && h[1] <= '9' && h[2] == ':' && h[3] >= '0' && h[3] <= '9' && h[4] >= '0' && h[4] <= '9') { int hourOffset = Integer.parseInt(msg.substring(curPos + 1, curPos + 3)); int minOffset = Integer.parseInt(msg.substring(curPos + 4, curPos + 6)); ts -= polarity * ((hourOffset * 60) + minOffset) * 60000; } else { throw new IllegalArgumentException( "Bad format: Invalid timezone: " + msg); } } return ts; } SyslogParser(); Event parseMessage(String msg, Charset charset);  }