Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Janitor #62

Merged
merged 10 commits into from
Nov 22, 2011
Next Next commit
Improved time handling in Tools class
* Added unit tests for Tools#getUTCTimestamp() and Tools#getUTCTimestampWithMilliseconds()
* Refactored  Tools#getUTCTimestamp() and Tools#getUTCTimestampWithMilliseconds()
  • Loading branch information
joschi committed Nov 20, 2011
commit 865553e343ebd06d75ee83fa4fc0a25bbfa50c42
19 changes: 8 additions & 11 deletions src/main/java/org/graylog2/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;

Expand All @@ -49,8 +49,7 @@ private Tools() { }
/**
* Get the own PID of this process.
*
* @return PID
* @throws Exception
* @return PID of the running process
*/
public static String getPID() {
return ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
Expand Down Expand Up @@ -191,21 +190,19 @@ public static String decompressGzip(byte[] compressedData) throws IOException {
* @return The current UTC UNIX timestamp.
*/
public static int getUTCTimestamp() {
return (int) (System.currentTimeMillis()/1000);
return (int) (Calendar.getInstance().getTimeInMillis()/1000);
}

/**
* Get the current UNIX epoch with milliseconds of the system
*
* @return The current UTC UNIX timestamp with milliseconds.
*/
public static double getUTCTimestampWithMilliseconds() {
// Use JodaTime to easy get the milliseconds and construct a float. (This looks dumb but is the easiest and safest way)
long now = System.currentTimeMillis();
DateTime jt = new DateTime(now);
String unixTime = String.valueOf(now/1000);
String millis = String.valueOf(jt.getMillisOfSecond());
Double milliSecondTime = new Double(unixTime + "." + millis);
return milliSecondTime.doubleValue();

long now = Calendar.getInstance().getTimeInMillis();

return Double.parseDouble(String.format("%d.%d", now/1000, now%1000));
}

public static String getLocalHostname() {
Expand Down
37 changes: 13 additions & 24 deletions src/test/java/org/graylog2/ToolsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,35 @@

package org.graylog2;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
*
* @author lennart
*/
public class ToolsTest {

public ToolsTest() {
@Test
public void testGetPID() {
String result = Tools.getPID();
assertTrue(Integer.parseInt(result) > 0);
}

@BeforeClass
public static void setUpClass() throws Exception {
}
@Test
public void testGetUTCTimestamp() {

@AfterClass
public static void tearDownClass() throws Exception {
assertTrue(Tools.getUTCTimestamp() > 0);
}

@Before
public void setUp() {
}
@Test
public void testGetUTCTimestampWithMilliseconds() {

@After
public void tearDown() {
assertTrue(Tools.getUTCTimestampWithMilliseconds() > 0.0d);
}

/**
* Test of getPID method, of class Tools.
*/
@Test
public void testGetPID() throws Exception {
String result = Tools.getPID();
assertTrue(Integer.parseInt(result) > 0);
}

/**
* Test of syslogLevelToReadable method, of class Tools.
Expand Down