Repository: metron Updated Branches: refs/heads/master eaa52f623 -> b9125c830
METRON-1003 ParserUtil parses dates incorrect (bjigmp via justinleet) closes apache/metron#623 Project: http://git-wip-us.apache.org/repos/asf/metron/repo Commit: http://git-wip-us.apache.org/repos/asf/metron/commit/b9125c83 Tree: http://git-wip-us.apache.org/repos/asf/metron/tree/b9125c83 Diff: http://git-wip-us.apache.org/repos/asf/metron/diff/b9125c83 Branch: refs/heads/master Commit: b9125c8307b5e8f5b77eeb6f4b96004cf52dc4f9 Parents: eaa52f6 Author: bjigmp <[email protected]> Authored: Fri Jul 14 07:54:18 2017 -0400 Committer: leet <[email protected]> Committed: Fri Jul 14 07:54:18 2017 -0400 ---------------------------------------------------------------------- .../metron/parsers/utils/ParserUtils.java | 13 +++++- .../metron/parsers/utils/ParserUtilsTest.java | 45 ++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metron/blob/b9125c83/metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/utils/ParserUtils.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/utils/ParserUtils.java b/metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/utils/ParserUtils.java index f98f996..09fe088 100644 --- a/metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/utils/ParserUtils.java +++ b/metron-platform/metron-parsers/src/main/java/org/apache/metron/parsers/utils/ParserUtils.java @@ -46,6 +46,17 @@ public class ParserUtils { return tempFile; } + /** + * Converts passed month (human short form), day, time and current year + * to milliseconds since epoch + * + * @param m Month in human short form (3 letters) (MMM) + * @param d Day (dd) + * @param ts Time (HH:mm:ss) + * @param adjust_timezone If True set GMT timezone for input time + * @return Number of milliseconds since epoch + * @exception ParseException If a date parsing error occured + */ public static Long convertToEpoch(String m, String d, String ts, boolean adjust_timezone) throws ParseException { d = d.trim(); @@ -55,7 +66,7 @@ public class ParserUtils { Date date = new SimpleDateFormat("MMM", Locale.ENGLISH).parse(m); Calendar cal = Calendar.getInstance(); cal.setTime(date); - String month = String.valueOf(cal.get(Calendar.MONTH)); + String month = String.valueOf(cal.get(Calendar.MONTH) + 1); int year = Calendar.getInstance().get(Calendar.YEAR); if (month.length() <= 2) { month = "0" + month; http://git-wip-us.apache.org/repos/asf/metron/blob/b9125c83/metron-platform/metron-parsers/src/test/java/org/apache/metron/parsers/utils/ParserUtilsTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-parsers/src/test/java/org/apache/metron/parsers/utils/ParserUtilsTest.java b/metron-platform/metron-parsers/src/test/java/org/apache/metron/parsers/utils/ParserUtilsTest.java new file mode 100644 index 0000000..835dfc5 --- /dev/null +++ b/metron-platform/metron-parsers/src/test/java/org/apache/metron/parsers/utils/ParserUtilsTest.java @@ -0,0 +1,45 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.metron.parsers.utils; + +import junit.framework.TestCase; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +import java.text.ParseException; + +public class ParserUtilsTest extends TestCase { + + public void testConvertToEpoch() throws ParseException { + Boolean adjustTimezone = true; + String[] timeToTest = {"Mar", "2", "05:24:39"}; + int year = Calendar.getInstance().get(Calendar.YEAR); + String timeToTestWithYear = String.valueOf(year) + " " + timeToTest[0] + " " + timeToTest[1] + " " + timeToTest[2]; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM d HH:mm:ss", Locale.ENGLISH); + sdf.setTimeZone(TimeZone.getTimeZone("GMT")); + Date date = sdf.parse(timeToTestWithYear); + Long expectedTs = date.getTime(); + Long ts = ParserUtils.convertToEpoch(timeToTest[0], timeToTest[1], timeToTest[2], adjustTimezone); + assertEquals(expectedTs, ts); + } +}
