Add T.now, plus, minus methods.

This commit is contained in:
Johannes Mockenhaupt 2018-05-13 12:44:47 +02:00
parent d0492462a4
commit d80fecc390
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
2 changed files with 43 additions and 2 deletions

View file

@ -7,6 +7,12 @@ package info.nightscout.utils;
public class T { public class T {
private long time; // in msec private long time; // in msec
public static T now() {
T t = new T();
t.time = System.currentTimeMillis();
return t;
}
public static T msecs(long msec) { public static T msecs(long msec) {
T t = new T(); T t = new T();
t.time = msec; t.time = msec;
@ -56,4 +62,12 @@ public class T {
public long days() { public long days() {
return time / 24 / 60 / 60 / 1000L; return time / 24 / 60 / 60 / 1000L;
} }
public T plus(T plus) {
return T.msecs(time + plus.time);
}
public T minus(T minus) {
return T.msecs(time - minus.time);
}
} }

View file

@ -9,11 +9,11 @@ import org.powermock.modules.junit4.PowerMockRunner;
* Created by mike on 26.03.2018. * Created by mike on 26.03.2018.
*/ */
@RunWith(PowerMockRunner.class) //@RunWith(PowerMockRunner.class)
public class TTest { public class TTest {
@Test @Test
public void doTests() { public void toUnits() {
Assert.assertEquals(1, T.msecs(1000).secs()); Assert.assertEquals(1, T.msecs(1000).secs());
Assert.assertEquals(1, T.secs(60).mins()); Assert.assertEquals(1, T.secs(60).mins());
Assert.assertEquals(1, T.mins(60).hours()); Assert.assertEquals(1, T.mins(60).hours());
@ -21,4 +21,31 @@ public class TTest {
Assert.assertEquals(24, T.days(1).hours()); Assert.assertEquals(24, T.days(1).hours());
Assert.assertEquals(60000, T.mins(1).msecs()); Assert.assertEquals(60000, T.mins(1).msecs());
} }
@Test
public void now() {
Assert.assertTrue(Math.abs(T.now().msecs() - System.currentTimeMillis()) < 5000);
}
@Test
public void additions() {
long nowMsecs = System.currentTimeMillis();
T now = T.msecs(nowMsecs);
Assert.assertEquals(now.plus(T.secs(5)).msecs(), nowMsecs + 5 * 1000);
Assert.assertEquals(now.plus(T.mins(5)).msecs(), nowMsecs + 5 * 60 * 1000);
Assert.assertEquals(now.plus(T.hours(5)).msecs(), nowMsecs + 5 * 60 * 60 * 1000);
Assert.assertEquals(now.plus(T.days(5)).msecs(), nowMsecs + 5 * 24 * 60 * 60 * 1000);
}
@Test
public void subtractions() {
long nowMsecs = System.currentTimeMillis();
T now = T.msecs(nowMsecs);
Assert.assertEquals(now.minus(T.secs(5)).msecs(), nowMsecs - 5 * 1000);
Assert.assertEquals(now.minus(T.mins(5)).msecs(), nowMsecs - 5 * 60 * 1000);
Assert.assertEquals(now.minus(T.hours(5)).msecs(), nowMsecs - 5 * 60 * 60 * 1000);
Assert.assertEquals(now.minus(T.days(5)).msecs(), nowMsecs - 5 * 24 * 60 * 60 * 1000);
}
} }