Skip to content

Commit

Permalink
tiny improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Linary committed Jun 28, 2019
1 parent 92271da commit d8cdc9e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/baidu/hugegraph/util/TimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public static long tillNextMillis(long lastTimestamp) {
}

public static String readableTime(long time) {
if (time > 60 * 1000) {
// Remove the milliseconds part
time = time / 1000 * 1000;
}
Duration duration = Duration.ofMillis(time);
return duration.toString()
.substring(2)
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/com/baidu/hugegraph/unit/util/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ public class StringUtilTest {
@Test
public void testSplit() {
Assert.assertEquals(guavaSplit("123", " "),
toList(StringUtil.split("123", " ")));
toStringList(StringUtil.split("123", " ")));
Assert.assertEquals(guavaSplit("1 2 3", " "),
toList(StringUtil.split("1 2 3", " ")));
toStringList(StringUtil.split("1 2 3", " ")));
Assert.assertEquals(guavaSplit("1:2:3", ":"),
toList(StringUtil.split("1:2:3", ":")));
toStringList(StringUtil.split("1:2:3", ":")));
Assert.assertEquals(guavaSplit("1::2:3", ":"),
toList(StringUtil.split("1::2:3", ":")));
toStringList(StringUtil.split("1::2:3", ":")));
Assert.assertEquals(guavaSplit("1::2::3", ":"),
toList(StringUtil.split("1::2::3", ":")));
toStringList(StringUtil.split("1::2::3", ":")));
Assert.assertEquals(guavaSplit("1::2::3", "::"),
toList(StringUtil.split("1::2::3", "::")));
toStringList(StringUtil.split("1::2::3", "::")));
Assert.assertEquals(guavaSplit("1:|2|:3", "|"),
toList(StringUtil.split("1:|2|:3", "|")));
toStringList(StringUtil.split("1:|2|:3", "|")));
Assert.assertEquals(guavaSplit("1\t2\t3", "\t"),
toList(StringUtil.split("1\t2\t3", "\t")));
toStringList(StringUtil.split("1\t2\t3", "\t")));
Assert.assertEquals(guavaSplit("111", "1"),
toList(StringUtil.split("111", "1")));
toStringList(StringUtil.split("111", "1")));

Assert.assertThrows(IllegalArgumentException.class, () -> {
StringUtil.split("123", "");
Expand All @@ -60,7 +60,7 @@ private static List<String> guavaSplit(String line, String delimiter) {
return Splitter.on(delimiter).splitToList(line);
}

private static List<String> toList(List<CharSequence> chars) {
private static List<String> toStringList(List<CharSequence> chars) {
List<String> results = new ArrayList<>(chars.size());
for (CharSequence seq : chars) {
results.add(seq.toString());
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/com/baidu/hugegraph/unit/util/TimeUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ public void testReadableTime() {
Assert.assertEquals("15s", TimeUtil.readableTime(15 * 1000));
Assert.assertEquals("59.99s", TimeUtil.readableTime(59990));
Assert.assertEquals("1m", TimeUtil.readableTime(60000));
Assert.assertEquals("1m 0.1s", TimeUtil.readableTime(60000 + 100));
// Ignore milliseconds part
Assert.assertEquals("1m", TimeUtil.readableTime(60000 + 100));
Assert.assertEquals("1m 1s", TimeUtil.readableTime(60000 + 1000));
Assert.assertEquals("1m 1s", TimeUtil.readableTime(60000 + 1200));
Assert.assertEquals("59m", TimeUtil.readableTime(59 * 60 * 1000));
Assert.assertEquals("1h", TimeUtil.readableTime(60 * 60 * 1000));
Assert.assertEquals("1h 1m", TimeUtil.readableTime(60 * 60 * 1000 +
60 * 1000));
Assert.assertEquals("23h 59m 59s", TimeUtil.readableTime(
23 * 60 * 60 * 1000 +
59 * 60 * 1000 +
Expand All @@ -82,6 +86,6 @@ public void testReadableTime() {
Assert.assertEquals("25h 1m 1s", TimeUtil.readableTime(
25 * 60 * 60 * 1000 +
1 * 60 * 1000 +
1 * 1000));
1 * 1200));
}
}

0 comments on commit d8cdc9e

Please sign in to comment.