METRON-1107 add support for handling epoch dates in seconds or milliseconds #692
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* 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.stellar.common.utils; | ||
|
||
/** | ||
* Utility functions for working with numbers that represent different | ||
* formats of EPOCH time. | ||
*/ | ||
public class EpochUtils { | ||
|
||
/** | ||
* Ensures returns the passed value as milliseconds from EPOCH if the value is in seconds. | ||
* This is done by looking at the number of digits. | ||
* If there are 10, then the value is concidered to be in seconds and will by | ||
* muliplited by 1000. | ||
* If there not 10, then the original value will be returned. | ||
* | ||
* | ||
* </p> | ||
* @param canidate The Long value to concider | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :%s/x/y/g There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't assuming vi, I was assuming more of a |
||
* @return A Long value | ||
*/ | ||
public static Long ensureEpochMillis(Long canidate) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
int length = (int)Math.floor(Math.log10(canidate) + 1); | ||
return length == 10 ? canidate * 1000 : canidate; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should handle inputs of length 1-9 as well. |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* 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.stellar.common.utils; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class EpochUtilsTest { | ||
|
||
@Test | ||
public void testSecondsToMillis() throws Exception { | ||
Long seconds = 1472131630L; | ||
Long value = EpochUtils.ensureEpochMillis(seconds); | ||
Assert.assertTrue(seconds.longValue() * 1000 == EpochUtils.ensureEpochMillis(seconds).longValue()); | ||
} | ||
|
||
@Test | ||
public void testMillisStaysMillis() throws Exception { | ||
Long millis = 1472131630748L; | ||
Long value = EpochUtils.ensureEpochMillis(millis); | ||
Assert.assertTrue(millis.longValue() == EpochUtils.ensureEpochMillis(millis).longValue()); | ||
} | ||
|
||
@Test | ||
public void testOtherLengthToMillis() throws Exception { | ||
Long meh = 1L; | ||
Assert.assertTrue(meh.longValue() == EpochUtils.ensureEpochMillis(meh).longValue()); | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest adding some tests for < 10 digit epochs. |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,10 +57,12 @@ private Object run(String expr) { | |
* Thu Aug 25 2016 09:27:10 EST | ||
*/ | ||
private long AUG2016 = 1472131630748L; | ||
private long AUG2016_SECONDS = 1472131630L; | ||
|
||
@Before | ||
public void setup() { | ||
variables.put("epoch", AUG2016); | ||
variables.put("epoch_seconds", AUG2016_SECONDS); | ||
calendar = Calendar.getInstance(); | ||
} | ||
|
||
|
@@ -70,6 +72,12 @@ public void testDayOfWeek() { | |
assertEquals(Calendar.THURSDAY, result); | ||
} | ||
|
||
@Test | ||
public void testDayOfWeekSeconds() { | ||
Object result = run("DAY_OF_WEEK(epoch_seconds)"); | ||
assertEquals(Calendar.THURSDAY, result); | ||
} | ||
|
||
/** | ||
* If no argument, then return the current day of week. | ||
*/ | ||
|
@@ -116,6 +124,12 @@ public void testMonth() { | |
assertEquals(Calendar.AUGUST, result); | ||
} | ||
|
||
@Test | ||
public void testMonthSeconds() { | ||
Object result = run("MONTH(epoch_seconds)"); | ||
assertEquals(Calendar.AUGUST, result); | ||
} | ||
|
||
/** | ||
* If no argument, then return the current month. | ||
*/ | ||
|
@@ -139,6 +153,12 @@ public void testYear() { | |
assertEquals(2016, result); | ||
} | ||
|
||
@Test | ||
public void testYearSeconds() { | ||
Object result = run("YEAR(epoch_seconds)"); | ||
assertEquals(2016, result); | ||
} | ||
|
||
/** | ||
* If no argument, then return the current year. | ||
*/ | ||
|
@@ -162,6 +182,12 @@ public void testDayOfMonth() { | |
assertEquals(25, result); | ||
} | ||
|
||
@Test | ||
public void testDayOfMonthSeconds() { | ||
Object result = run("DAY_OF_MONTH(epoch_seconds)"); | ||
assertEquals(25, result); | ||
} | ||
|
||
/** | ||
* If no argument, then return the current day of month. | ||
*/ | ||
|
@@ -185,6 +211,12 @@ public void testWeekOfYear() { | |
assertEquals(35, result); | ||
} | ||
|
||
@Test | ||
public void testWeekOfYearSeconds() { | ||
Object result = run("WEEK_OF_YEAR(epoch_seconds)"); | ||
assertEquals(35, result); | ||
} | ||
|
||
/** | ||
* If no argument, then return the current week of year. | ||
*/ | ||
|
@@ -208,6 +240,12 @@ public void testDayOfYear() { | |
assertEquals(238, result); | ||
} | ||
|
||
@Test | ||
public void testDayOfYearSeconds() { | ||
Object result = run("DAY_OF_YEAR(epoch_seconds)"); | ||
assertEquals(238, result); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest adding tests for an epoch time at or before Sunday, September 9, 2001 1:46:39 AM. |
||
/** | ||
* If no argument, then return the current day of year. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/muliplited/multiplied/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.