Skip to content

Commit ca666da

Browse files
author
ar0ne
committed
improve date time helper
1 parent bbfb634 commit ca666da

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2018 MovingBlocks
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.terasology.utilities.time;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
21+
import java.util.Arrays;
22+
23+
public class DateTimeHelperTest {
24+
25+
@Test
26+
public void testGetDeltaBetweenTimestamps() {
27+
Assert.assertEquals("00h 00m 01s", DateTimeHelper.getDeltaBetweenTimestamps(0, 1000));
28+
Assert.assertEquals("00h 00m 10s", DateTimeHelper.getDeltaBetweenTimestamps(0, 10 * 1000));
29+
Assert.assertEquals("00h 01m 00s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000));
30+
Assert.assertEquals("00h 11m 00s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000 * 11));
31+
Assert.assertEquals("01h 00m 00s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000 * 60));
32+
Assert.assertEquals("12h 34m 56s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000 * 60 * 12 + 60 * 1000 * 34 + 56 * 1000));
33+
34+
Assert.assertEquals("1 Days 01h 00m 00s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000 * 60 * 25));
35+
Assert.assertEquals("1 Days 01h 20m 40s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000 * 60 * 24 + 60 * 1000 * 60 + 60 * 1000 * 20 + 40 * 1000));
36+
37+
Assert.assertEquals("00h 00m 00s", DateTimeHelper.getDeltaBetweenTimestamps(1000, 1000));
38+
Assert.assertEquals("00h 00m 00s", DateTimeHelper.getDeltaBetweenTimestamps(50000, 50000));
39+
40+
Assert.assertEquals("11h 01m 01s", DateTimeHelper.getDeltaBetweenTimestamps(1000 * 60 * 25, 1000 * 60 * 25 + 60 * 1000 * 60 * 11 + 60 * 1000 + 1000));
41+
}
42+
43+
@Test
44+
public void testGetDeltaBetweenTimestampsWrongData() {
45+
46+
Integer[][] wrongData = {
47+
{-1, 50000},
48+
{123, -1000},
49+
{-667, -1000},
50+
{123, 0},
51+
{12321321, 1231}
52+
};
53+
54+
Arrays.stream(wrongData).forEach(
55+
data -> {
56+
try {
57+
DateTimeHelper.getDeltaBetweenTimestamps(data[0], data[1]);
58+
Assert.fail("Exception should be thrown!");
59+
} catch (IllegalArgumentException ex) {
60+
Assert.assertEquals("Wrong timestamp values: " + data[0] + " or " + data[1], ex.getMessage());
61+
}
62+
}
63+
);
64+
}
65+
66+
}

engine/src/main/java/org/terasology/utilities/time/DateTimeHelper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ private DateTimeHelper() {
3333
* @return delta between timestamps as a string
3434
*/
3535
public static String getDeltaBetweenTimestamps(final long start, final long end) {
36+
if (start < 0 || end < 0 || start > end) {
37+
throw new IllegalArgumentException("Wrong timestamp values: " + start + " or " + end);
38+
}
3639
TimeUnit timeUnit = TimeUnit.SECONDS;
3740

3841
long diffInMilliseconds = end - start;
@@ -49,7 +52,7 @@ public static String getDeltaBetweenTimestamps(final long start, final long end)
4952

5053
if (days > 0) {
5154
builder.append(days);
52-
builder.append(" Days");
55+
builder.append(" Days ");
5356
}
5457

5558
builder.append(fill2(hrs));

0 commit comments

Comments
 (0)