This is an automated email from the ASF dual-hosted git repository.

tanxinyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new b8af6a576db Fix convertMillisecondToDurationStr and add UT (#12361)
b8af6a576db is described below

commit b8af6a576db8832dcd362e345b114d5040e7e69b
Author: Li Yu Heng <[email protected]>
AuthorDate: Wed Apr 17 17:12:33 2024 +0800

    Fix convertMillisecondToDurationStr and add UT (#12361)
---
 .../iotdb/commons/utils/CommonDateTimeUtils.java   | 23 +++++++++---
 .../commons/utils/CommonDateTimeUtilsTest.java     | 41 ++++++++++++++++++++++
 2 files changed, 60 insertions(+), 4 deletions(-)

diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/CommonDateTimeUtils.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/CommonDateTimeUtils.java
index 0eeb6e63bff..3366011bc4b 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/CommonDateTimeUtils.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/CommonDateTimeUtils.java
@@ -59,6 +59,13 @@ public class CommonDateTimeUtils {
   }
 
   public static String convertMillisecondToDurationStr(long millisecond) {
+    StringBuilder stringBuilder = new StringBuilder();
+    boolean minus = false;
+    if (millisecond < 0) {
+      minus = true;
+      millisecond = -millisecond;
+      stringBuilder.append("-(");
+    }
     Duration duration = Duration.ofMillis(millisecond);
     long days = duration.toDays();
     long years = days / 365;
@@ -69,11 +76,10 @@ public class CommonDateTimeUtils {
     long minutes = duration.toMinutes() % 60;
     long seconds = duration.getSeconds() % 60;
     long ms = millisecond % 1000;
-    StringBuilder result = new StringBuilder();
     BiConsumer<Long, String> append =
         (value, unit) -> {
           if (value > 0) {
-            result.append(value).append(" ").append(unit).append(" ");
+            stringBuilder.append(value).append(" ").append(unit).append(" ");
           }
         };
     append.accept(years, "year");
@@ -83,7 +89,16 @@ public class CommonDateTimeUtils {
     append.accept(minutes, "minute");
     append.accept(seconds, "second");
     append.accept(ms, "ms");
-    result.delete(result.length() - 1, result.length());
-    return result.toString();
+    String result = stringBuilder.toString();
+    if (result.endsWith(" ")) {
+      result = result.substring(0, result.length() - 1);
+    }
+    if (minus) {
+      result += ")";
+    }
+    if (result.isEmpty()) {
+      result = "0 ms";
+    }
+    return result;
   }
 }
diff --git 
a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/CommonDateTimeUtilsTest.java
 
b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/CommonDateTimeUtilsTest.java
new file mode 100644
index 00000000000..9ae8197ea64
--- /dev/null
+++ 
b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/utils/CommonDateTimeUtilsTest.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.commons.utils;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import static 
org.apache.iotdb.commons.utils.CommonDateTimeUtils.convertMillisecondToDurationStr;
+
+public class CommonDateTimeUtilsTest {
+  @Test
+  public void convertMillisecondToDurationStrTest() {
+    Assert.assertEquals("0 ms", convertMillisecondToDurationStr(0));
+    Assert.assertEquals("1 ms", convertMillisecondToDurationStr(1));
+    Assert.assertEquals("999 ms", convertMillisecondToDurationStr(999));
+    Assert.assertEquals(
+        "1 day 3 hour 46 minute 39 second 999 ms", 
convertMillisecondToDurationStr(99999999));
+    Assert.assertEquals("1 day 3 hour 39 second 999 ms", 
convertMillisecondToDurationStr(97239999));
+    Assert.assertEquals(
+        "3170979 year 2 month 12 day 9 hour 46 minute 39 second 999 ms",
+        convertMillisecondToDurationStr(99999999999999999L));
+    Assert.assertEquals("-(10 second 86 ms)", 
convertMillisecondToDurationStr(-10086));
+  }
+}

Reply via email to