lancelly commented on code in PR #11429:
URL: https://github.com/apache/iotdb/pull/11429#discussion_r1378875918


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/DateTimeUtils.java:
##########
@@ -737,23 +738,66 @@ public static TimeUnit toTimeUnit(String t) {
 
   public static final long MS_TO_MONTH = 30 * 86400_000L;
 
+  public static long calcPositiveIntervalByMonth(
+      long startTime, TimeDuration duration, long times) {
+    return TimeDuration.calcPositiveIntervalByMonth(
+        startTime,
+        duration,
+        times,
+        SessionManager.getInstance().getSessionTimeZone(),
+        TimestampPrecisionUtils.currPrecision);
+  }
+
+  public static long calcNegativeIntervalByMonth(long startTime, TimeDuration 
duration) {
+    return TimeDuration.calcNegativeIntervalByMonth(
+        startTime,
+        duration,
+        SessionManager.getInstance().getSessionTimeZone(),
+        TimestampPrecisionUtils.currPrecision);
+  }
+
   /**
-   * add natural months based on the startTime to avoid edge cases, ie 2/28
+   * Storage the duration into two parts: month part and non-month part, the 
non-month part's
+   * precision is depended on current time precision. e.g. ms precision: 
'1y1mo1ms' -> monthDuration
+   * = 13, nonMonthDuration = 1, ns precision: '1y1mo1ms' -> monthDuration = 
13, nonMonthDuration =
+   * 1000_000.
    *
-   * @param startTime current start time
-   * @param numMonths numMonths is updated in hasNextWithoutConstraint()
-   * @return nextStartTime
+   * @param duration the input duration string
+   * @return the TimeDuration instance contains month part and non-month part
    */
-  public static long calcIntervalByMonth(long startTime, long numMonths) {
-    Calendar calendar = Calendar.getInstance();
-    calendar.setTimeZone(SessionManager.getInstance().getSessionTimeZone());
-    calendar.setTimeInMillis(startTime);
-    boolean isLastDayOfMonth =
-        calendar.get(Calendar.DAY_OF_MONTH) == 
calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
-    calendar.add(Calendar.MONTH, (int) (numMonths));
-    if (isLastDayOfMonth) {
-      calendar.set(Calendar.DAY_OF_MONTH, 
calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
+  public static TimeDuration constructTimeDuration(String duration) {
+    duration = duration.toLowerCase();
+    String currTimePrecision = 
CommonDescriptor.getInstance().getConfig().getTimestampPrecision();
+    int temp = 0;
+    int monthDuration = 0;
+    long nonMonthDuration = 0;
+    for (int i = 0; i < duration.length(); i++) {
+      char ch = duration.charAt(i);
+      if (Character.isDigit(ch)) {
+        temp *= 10;
+        temp += (ch - '0');
+      } else {
+        String unit = String.valueOf(duration.charAt(i));
+        // This is to identify units with two letters.
+        if (i + 1 < duration.length() && !Character.isDigit(duration.charAt(i 
+ 1))) {
+          i++;
+          unit += duration.charAt(i);
+        }
+        if (unit.equals("y")) {
+          monthDuration = temp * 12;
+          temp = 0;
+          continue;
+        }
+        if (unit.equals("mo")) {
+          monthDuration += temp;
+          temp = 0;
+          continue;
+        }
+        nonMonthDuration +=
+            DateTimeUtils.convertDurationStrToLong(-1, temp, unit, 
currTimePrecision);

Review Comment:
   if the input is 1d-1ns, the exception message will be not so clear, maybe 
you can handle situations like this in DateTimeUtils.convertDurationStrToLong



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to