You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "BewareMyPower (via GitHub)" <gi...@apache.org> on 2023/08/04 15:54:15 UTC

[GitHub] [pulsar] BewareMyPower commented on a diff in pull request #20782: [refactor][cli][PIP-280] Create new `pulsar-cli-utils` module

BewareMyPower commented on code in PR #20782:
URL: https://github.com/apache/pulsar/pull/20782#discussion_r1284591431


##########
pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/RelativeTimeUtil.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.pulsar.cli.converters;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.concurrent.TimeUnit;
+import lombok.experimental.UtilityClass;
+
+/**
+ * Mirror of {@link org.apache.pulsar.common.util.RelativeTimeUtil}.
+ */
+@UtilityClass
+public class RelativeTimeUtil {
+    public static long parseRelativeTimeInSeconds(String relativeTime) {
+        if (relativeTime.isEmpty()) {
+            throw new IllegalArgumentException("time cannot be empty");
+        }
+
+        int lastIndex = relativeTime.length() - 1;
+        char lastChar = relativeTime.charAt(lastIndex);
+        final char timeUnit;
+
+        if (!Character.isAlphabetic(lastChar)) {
+            // No unit specified, assume seconds
+            timeUnit = 's';
+            lastIndex = relativeTime.length();
+        } else {
+            timeUnit = Character.toLowerCase(lastChar);
+        }
+
+        long duration = Long.parseLong(relativeTime.substring(0, lastIndex));
+
+        switch (timeUnit) {
+        case 's':
+            return duration;
+        case 'm':
+            return TimeUnit.MINUTES.toSeconds(duration);
+        case 'h':
+            return TimeUnit.HOURS.toSeconds(duration);
+        case 'd':
+            return TimeUnit.DAYS.toSeconds(duration);
+        case 'w':
+            return 7 * TimeUnit.DAYS.toSeconds(duration);
+        // No unit for months
+        case 'y':
+            return 365 * TimeUnit.DAYS.toSeconds(duration);
+        default:
+            throw new IllegalArgumentException("Invalid time unit '" + lastChar + "'");
+        }
+    }
+
+    /**
+     * Convert nanoseconds to seconds and keep three decimal places.
+     * @param ns
+     * @return seconds
+     */
+    public static double nsToSeconds(long ns) {

Review Comment:
   This method is not tested.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org