Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for format_datetime() with time zone offsets #10606

Closed
wants to merge 1 commit into from
Closed
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
50 changes: 32 additions & 18 deletions velox/functions/prestosql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,6 @@ class DateTimeFunctionsTest : public functions::test::FunctionBaseTest {
return resultVector->as<SimpleVector<StringView>>()->valueAt(0);
}

std::optional<std::string> formatDatetime(
std::optional<Timestamp> timestamp,
const std::string& format) {
auto resultVector = evaluate(
"format_datetime(c0, c1)",
makeRowVector(
{makeNullableFlatVector<Timestamp>({timestamp}),
makeNullableFlatVector<std::string>({format})}));
return resultVector->as<SimpleVector<StringView>>()->valueAt(0);
}

template <typename T>
std::optional<T> evaluateWithTimestampWithTimezone(
const std::string& expression,
Expand Down Expand Up @@ -2994,6 +2983,12 @@ TEST_F(DateTimeFunctionsTest, parseDatetime) {
}

TEST_F(DateTimeFunctionsTest, formatDateTime) {
const auto formatDatetime = [&](std::optional<Timestamp> timestamp,
std::optional<std::string> format) {
return evaluateOnce<std::string>(
"format_datetime(c0, c1)", timestamp, format);
};

// era test cases - 'G'
EXPECT_EQ("AD", formatDatetime(parseTimestamp("1970-01-01"), "G"));
EXPECT_EQ("BC", formatDatetime(parseTimestamp("-100-01-01"), "G"));
Expand Down Expand Up @@ -3248,6 +3243,12 @@ TEST_F(DateTimeFunctionsTest, formatDateTime) {
parseTimestamp("1970-01-01 02:33:11.5"),
"G C Y e 'asdfghjklzxcvbnmqwertyuiop' E '' y D M d a K h H k m s S 1234567890\\\"!@#$%^&*()-+`~{}[];:,./ zzzz"));

disableAdjustTimestampToTimezone();
EXPECT_EQ(
"1970-01-01 00:00:00",
formatDatetime(
parseTimestamp("1970-01-01 00:00:00"), "YYYY-MM-dd HH:mm:ss"));

// User format errors or unsupported errors
EXPECT_THROW(
formatDatetime(parseTimestamp("1970-01-01"), "x"), VeloxUserError);
Expand Down Expand Up @@ -3276,24 +3277,37 @@ TEST_F(DateTimeFunctionsTest, formatDateTimeTimezone) {
format);
};

const auto zeroTs = parseTimestamp("1970-01-01");

// No timezone set; default to GMT.
// UTC explicitly set.
EXPECT_EQ(
"1970-01-01 00:00:00", formatDatetime(zeroTs, "YYYY-MM-dd HH:mm:ss"));
"1970-01-01 00:00:00",
formatDatetimeWithTimezone(
TimestampWithTimezone(0, "UTC"), "YYYY-MM-dd HH:mm:ss"));

// Check that string is adjusted to the timezone set.
EXPECT_EQ(
"1970-01-01 05:30:00",
formatDatetimeWithTimezone(
TimestampWithTimezone(zeroTs.toMillis(), "Asia/Kolkata"),
"YYYY-MM-dd HH:mm:ss"));
TimestampWithTimezone(0, "Asia/Kolkata"), "YYYY-MM-dd HH:mm:ss"));

EXPECT_EQ(
"1969-12-31 16:00:00",
formatDatetimeWithTimezone(
TimestampWithTimezone(zeroTs.toMillis(), "America/Los_Angeles"),
TimestampWithTimezone(0, "America/Los_Angeles"),
"YYYY-MM-dd HH:mm:ss"));

// Make sure format_datetime() works with timezone offsets.
EXPECT_EQ(
"1969-12-31 16:00:00",
formatDatetimeWithTimezone(
TimestampWithTimezone(0, "-08:00"), "YYYY-MM-dd HH:mm:ss"));
EXPECT_EQ(
"1969-12-31 23:45:00",
formatDatetimeWithTimezone(
TimestampWithTimezone(0, "-00:15"), "YYYY-MM-dd HH:mm:ss"));
EXPECT_EQ(
"1970-01-01 00:07:00",
formatDatetimeWithTimezone(
TimestampWithTimezone(0, "+00:07"), "YYYY-MM-dd HH:mm:ss"));
}

TEST_F(DateTimeFunctionsTest, dateFormat) {
Expand Down
Loading