Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions be/src/core/value/timestamptz_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions be/test/core/data_type/data_type_timestamptz_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <gtest/gtest-test-part.h>
#include <gtest/gtest.h>

#include <chrono>
#include <cstdint>
#include <cstring>
#include <string>
Expand Down Expand Up @@ -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"};
Expand Down
Loading