diff --git a/app/src/main/java/info/nightscout/utils/T.java b/app/src/main/java/info/nightscout/utils/T.java index ce8541dd8c..99f4e573c3 100644 --- a/app/src/main/java/info/nightscout/utils/T.java +++ b/app/src/main/java/info/nightscout/utils/T.java @@ -7,6 +7,12 @@ package info.nightscout.utils; public class T { 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) { T t = new T(); t.time = msec; @@ -56,4 +62,12 @@ public class T { public long days() { 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); + } } diff --git a/app/src/test/java/info/nightscout/utils/TTest.java b/app/src/test/java/info/nightscout/utils/TTest.java index ca7bb7cdaf..764754623e 100644 --- a/app/src/test/java/info/nightscout/utils/TTest.java +++ b/app/src/test/java/info/nightscout/utils/TTest.java @@ -9,11 +9,11 @@ import org.powermock.modules.junit4.PowerMockRunner; * Created by mike on 26.03.2018. */ -@RunWith(PowerMockRunner.class) +//@RunWith(PowerMockRunner.class) public class TTest { @Test - public void doTests() { + public void toUnits() { Assert.assertEquals(1, T.msecs(1000).secs()); Assert.assertEquals(1, T.secs(60).mins()); Assert.assertEquals(1, T.mins(60).hours()); @@ -21,4 +21,31 @@ public class TTest { Assert.assertEquals(24, T.days(1).hours()); 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); + } }