Copilot commented on code in PR #5707:
URL: https://github.com/apache/ignite-3/pull/5707#discussion_r2068337107


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/IgniteSqlDateTimeUtils.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.ignite.internal.sql.engine.util;
+
+import static 
org.apache.calcite.avatica.util.DateTimeUtils.dateStringToUnixDate;
+
+import java.time.Year;
+import java.util.TimeZone;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.calcite.avatica.util.DateTimeUtils;
+import org.apache.ignite.internal.lang.IgniteStringBuilder;
+import org.apache.ignite.internal.sql.engine.exec.exp.IgniteSqlFunctions;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Contains a set of utility methods for converting temporal types.
+ */
+public class IgniteSqlDateTimeUtils {
+    /** Regex for time. */
+    private static final Pattern TIME_PATTERN =
+            Pattern.compile("^(\\d+):(\\d+):(\\d+)(\\.\\d*)?$");
+
+    /** Regex for date. */
+    private static final Pattern DATE_PATTERN =
+            Pattern.compile("^(\\d{4})-([0]\\d|1[0-2])-([0-2]\\d|3[01])$");
+
+    /** The maximum number of digits after the decimal point that are taken 
into account. */
+    private static final int USEFUL_FRACTION_OF_SECOND_LENGTH = 3;
+
+    private static final int[] FRACTION_OF_SECOND_MULTIPLIERS = {100, 10, 1};
+
+    /** Returns the timestamp value minus the offset of the specified 
timezone. */
+    public static Long subtractTimeZoneOffset(long timestamp, TimeZone 
timeZone) {
+        // A second offset calculation is required to handle DST transition 
period correctly.
+        int offset = timeZone.getOffset(timestamp - 
timeZone.getOffset(timestamp));
+
+        // After adjusting to UTC, you need to make sure that the value 
matches the allowed values.
+        return IgniteSqlFunctions.toTimestampLtzExact(timestamp - offset);
+    }
+
+    /**
+     * Helper for CAST({time} AS VARCHAR(n)).
+     *
+     * <p>Note: this method is a copy of the avatica {@link 
DateTimeUtils#unixTimestampToString(long, int)} method,
+     *          with the only difference being that it does not add trailing 
zeros.
+     */
+    public static String unixTimeToString(int time, int precision) {
+        IgniteStringBuilder buf = new IgniteStringBuilder(8 + (precision > 0 ? 
1 + precision : 0));
+
+        unixTimeToString(buf, time, precision);
+
+        return buf.toString();
+    }
+
+    private static void unixTimeToString(IgniteStringBuilder buf, int time, 
int precision) {
+        int h = time / 3600000;
+        int time2 = time % 3600000;
+        int m = time2 / 60000;
+        int time3 = time2 % 60000;
+        int s = time3 / 1000;
+        int ms = time3 % 1000;
+
+        buf.app((char) ('0' + (h / 10) % 10))
+                .app((char) ('0' + h % 10))
+                .app(':')
+                .app((char) ('0' + (m / 10) % 10))
+                .app((char) ('0' + m % 10))
+                .app(':')
+                .app((char) ('0' + (s / 10) % 10))
+                .app((char) ('0' + s % 10));
+
+        if (precision == 0 || ms == 0) {
+            return;
+        }
+
+        buf.app('.');
+        do {
+            buf.app((char) ('0' + (ms / 100)));
+
+            ms = ms % 100;

Review Comment:
   [nitpick] Consider defining a constant for the literal 100 used in the 
fraction conversion loop to improve code clarity and maintainability.
   ```suggestion
               buf.app((char) ('0' + (ms / MILLISECONDS_TO_CENTISECONDS)));
   
               ms = ms % MILLISECONDS_TO_CENTISECONDS;
   ```



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/IgniteSqlDateTimeUtils.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * 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.ignite.internal.sql.engine.util;
+
+import static 
org.apache.calcite.avatica.util.DateTimeUtils.dateStringToUnixDate;
+
+import java.time.Year;
+import java.util.TimeZone;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.calcite.avatica.util.DateTimeUtils;
+import org.apache.ignite.internal.lang.IgniteStringBuilder;
+import org.apache.ignite.internal.sql.engine.exec.exp.IgniteSqlFunctions;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Contains a set of utility methods for converting temporal types.
+ */
+public class IgniteSqlDateTimeUtils {
+    /** Regex for time. */
+    private static final Pattern TIME_PATTERN =
+            Pattern.compile("^(\\d+):(\\d+):(\\d+)(\\.\\d*)?$");
+
+    /** Regex for date. */
+    private static final Pattern DATE_PATTERN =
+            Pattern.compile("^(\\d{4})-([0]\\d|1[0-2])-([0-2]\\d|3[01])$");
+
+    /** The maximum number of digits after the decimal point that are taken 
into account. */
+    private static final int USEFUL_FRACTION_OF_SECOND_LENGTH = 3;
+
+    private static final int[] FRACTION_OF_SECOND_MULTIPLIERS = {100, 10, 1};
+
+    /** Returns the timestamp value minus the offset of the specified 
timezone. */
+    public static Long subtractTimeZoneOffset(long timestamp, TimeZone 
timeZone) {
+        // A second offset calculation is required to handle DST transition 
period correctly.
+        int offset = timeZone.getOffset(timestamp - 
timeZone.getOffset(timestamp));
+

Review Comment:
   [nitpick] Add an inline comment elaborating on the double offset calculation 
to clarify how DST transitions are handled.
   ```suggestion
           // The first offset calculation adjusts the timestamp to UTC, but 
during DST transitions,
           // the offset itself may change (e.g., when clocks move forward or 
backward). By recalculating
           // the offset using the adjusted timestamp, we ensure the correct 
offset is applied.
           int offset = timeZone.getOffset(timestamp - 
timeZone.getOffset(timestamp));
   ```



-- 
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: notifications-unsubscr...@ignite.apache.org

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

Reply via email to