This repository has been archived by the owner on May 12, 2021. It is now read-only.
METRON-1107 add support for handling epoch dates in seconds or milliseconds #692
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ellar/stellar-common/src/main/java/org/apache/metron/stellar/common/utils/EpochUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 considered to be in seconds and will by | ||
* multiplied by 1000. | ||
* If there not 10, then the original value will be returned. | ||
* | ||
* | ||
* </p> | ||
* @param candidate The Long value to consider | ||
* @return A Long value | ||
*/ | ||
public static Long ensureEpochMillis(Long candidate) { | ||
int length = (int)Math.floor(Math.log10(candidate) + 1); | ||
return length == 10 ? candidate * 1000 : candidate; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...r/stellar-common/src/test/java/org/apache/metron/stellar/common/utils/EpochUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Suggest adding some tests for < 10 digit epochs.