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

yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.1 by this push:
     new b25e2570e38 branch-4.1: [fix](function) preserve sign for negative 
sub-hour TIMESTAMPTZ offsets #62823 (#63053)
b25e2570e38 is described below

commit b25e2570e38088ec5eb0f3eeaa376a77e9016b47
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Sat May 9 15:13:44 2026 +0800

    branch-4.1: [fix](function) preserve sign for negative sub-hour TIMESTAMPTZ 
offsets #62823 (#63053)
    
    Cherry-picked from #62823
    
    Co-authored-by: Mryange <[email protected]>
---
 be/src/core/value/timestamptz_value.cpp               | 14 ++++++++------
 be/test/core/data_type/data_type_timestamptz_test.cpp | 12 ++++++++++++
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/be/src/core/value/timestamptz_value.cpp 
b/be/src/core/value/timestamptz_value.cpp
index 542ce11740e..40ca96bdf7f 100644
--- a/be/src/core/value/timestamptz_value.cpp
+++ b/be/src/core/value/timestamptz_value.cpp
@@ -40,8 +40,10 @@ std::string TimestampTzValue::to_string(const 
cctz::time_zone& tz, int scale) co
     cctz::civil_second civ = lookup_result.cs;
     auto time_offset = lookup_result.offset;
 
-    int offset_hours = time_offset / 3600;
-    int offset_mins = (std::abs(time_offset) % 3600) / 60;
+    bool is_negative_offset = time_offset < 0;
+    int abs_offset = std::abs(time_offset);
+    int offset_hours = abs_offset / 3600;
+    int offset_mins = (abs_offset % 3600) / 60;
 
     /// TODO: We could directly use datetime's to_string here. In the future,
     /// when we support a function like 'show datetime with timezone',
@@ -57,13 +59,13 @@ std::string TimestampTzValue::to_string(const 
cctz::time_zone& tz, int scale) co
     int len = tmp_dt.to_buffer(buffer, scale);
     // timezone +03:00
     // buffer[len++] = ' ';
-    buffer[len++] = (offset_hours >= 0 ? '+' : '-');
-    buffer[len++] = static_cast<char>('0' + std::abs(offset_hours) / 10);
-    buffer[len++] = '0' + std::abs(offset_hours) % 10;
+    buffer[len++] = (is_negative_offset ? '-' : '+');
+    buffer[len++] = static_cast<char>('0' + offset_hours / 10);
+    buffer[len++] = '0' + offset_hours % 10;
     buffer[len++] = ':';
     buffer[len++] = static_cast<char>('0' + offset_mins / 10);
     buffer[len++] = '0' + offset_mins % 10;
-    return std::string(buffer, len);
+    return {buffer, static_cast<size_t>(len)};
 }
 
 bool TimestampTzValue::from_datetime(const DateV2Value<DateTimeV2ValueType>& 
origin_dt,
diff --git a/be/test/core/data_type/data_type_timestamptz_test.cpp 
b/be/test/core/data_type/data_type_timestamptz_test.cpp
index 8d52dd58cb6..d2dad93e1f4 100644
--- a/be/test/core/data_type/data_type_timestamptz_test.cpp
+++ b/be/test/core/data_type/data_type_timestamptz_test.cpp
@@ -21,6 +21,7 @@
 #include <gtest/gtest-test-part.h>
 #include <gtest/gtest.h>
 
+#include <chrono>
 #include <cstdint>
 #include <cstring>
 #include <string>
@@ -101,6 +102,17 @@ TEST_F(DataTypeTimeStampTzTest, test_serder) {
     }
 }
 
+TEST_F(DataTypeTimeStampTzTest, test_to_string_negative_sub_hour_offset) {
+    cctz::time_zone time_zone = 
cctz::fixed_time_zone(std::chrono::minutes(-30));
+
+    DateV2Value<DateTimeV2ValueType> local_dt;
+    local_dt.unchecked_set_time(2023, 12, 31, 23, 45, 0, 0);
+
+    TimestampTzValue value;
+    EXPECT_TRUE(value.from_datetime(local_dt, time_zone, 6, 6));
+    EXPECT_EQ(value.to_string(time_zone), "2023-12-31 23:45:00.000000-00:30");
+}
+
 TEST_F(DataTypeTimeStampTzTest, test_sort) {
     MockRuntimeState _state;
     RuntimeProfile _profile {"test"};


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to