Skip to content

Commit c8ac8f7

Browse files
pedroerpfacebook-github-bot
authored andcommitted
Add tests for format_datetime() with time zone offsets (facebookincubator#10606)
Summary: Pull Request resolved: facebookincubator#10606 In the recent time zone support improvements, time zone offset support was added to the same API used for regular time zones. Hence, functions based on this API now also support offests. Adding unit tests to ensure format_datetime() has the expected behavior. Addresses facebookincubator#10101 Reviewed By: mbasmanova Differential Revision: D60430532 fbshipit-source-id: d471632526ef56ab313bca34d34079a8ecd48a6c
1 parent 7115014 commit c8ac8f7

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

velox/functions/prestosql/tests/DateTimeFunctionsTest.cpp

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,6 @@ class DateTimeFunctionsTest : public functions::test::FunctionBaseTest {
148148
return resultVector->as<SimpleVector<StringView>>()->valueAt(0);
149149
}
150150

151-
std::optional<std::string> formatDatetime(
152-
std::optional<Timestamp> timestamp,
153-
const std::string& format) {
154-
auto resultVector = evaluate(
155-
"format_datetime(c0, c1)",
156-
makeRowVector(
157-
{makeNullableFlatVector<Timestamp>({timestamp}),
158-
makeNullableFlatVector<std::string>({format})}));
159-
return resultVector->as<SimpleVector<StringView>>()->valueAt(0);
160-
}
161-
162151
template <typename T>
163152
std::optional<T> evaluateWithTimestampWithTimezone(
164153
const std::string& expression,
@@ -2994,6 +2983,12 @@ TEST_F(DateTimeFunctionsTest, parseDatetime) {
29942983
}
29952984

29962985
TEST_F(DateTimeFunctionsTest, formatDateTime) {
2986+
const auto formatDatetime = [&](std::optional<Timestamp> timestamp,
2987+
std::optional<std::string> format) {
2988+
return evaluateOnce<std::string>(
2989+
"format_datetime(c0, c1)", timestamp, format);
2990+
};
2991+
29972992
// era test cases - 'G'
29982993
EXPECT_EQ("AD", formatDatetime(parseTimestamp("1970-01-01"), "G"));
29992994
EXPECT_EQ("BC", formatDatetime(parseTimestamp("-100-01-01"), "G"));
@@ -3248,6 +3243,12 @@ TEST_F(DateTimeFunctionsTest, formatDateTime) {
32483243
parseTimestamp("1970-01-01 02:33:11.5"),
32493244
"G C Y e 'asdfghjklzxcvbnmqwertyuiop' E '' y D M d a K h H k m s S 1234567890\\\"!@#$%^&*()-+`~{}[];:,./ zzzz"));
32503245

3246+
disableAdjustTimestampToTimezone();
3247+
EXPECT_EQ(
3248+
"1970-01-01 00:00:00",
3249+
formatDatetime(
3250+
parseTimestamp("1970-01-01 00:00:00"), "YYYY-MM-dd HH:mm:ss"));
3251+
32513252
// User format errors or unsupported errors
32523253
EXPECT_THROW(
32533254
formatDatetime(parseTimestamp("1970-01-01"), "x"), VeloxUserError);
@@ -3276,24 +3277,37 @@ TEST_F(DateTimeFunctionsTest, formatDateTimeTimezone) {
32763277
format);
32773278
};
32783279

3279-
const auto zeroTs = parseTimestamp("1970-01-01");
3280-
3281-
// No timezone set; default to GMT.
3280+
// UTC explicitly set.
32823281
EXPECT_EQ(
3283-
"1970-01-01 00:00:00", formatDatetime(zeroTs, "YYYY-MM-dd HH:mm:ss"));
3282+
"1970-01-01 00:00:00",
3283+
formatDatetimeWithTimezone(
3284+
TimestampWithTimezone(0, "UTC"), "YYYY-MM-dd HH:mm:ss"));
32843285

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

32923292
EXPECT_EQ(
32933293
"1969-12-31 16:00:00",
32943294
formatDatetimeWithTimezone(
3295-
TimestampWithTimezone(zeroTs.toMillis(), "America/Los_Angeles"),
3295+
TimestampWithTimezone(0, "America/Los_Angeles"),
32963296
"YYYY-MM-dd HH:mm:ss"));
3297+
3298+
// Make sure format_datetime() works with timezone offsets.
3299+
EXPECT_EQ(
3300+
"1969-12-31 16:00:00",
3301+
formatDatetimeWithTimezone(
3302+
TimestampWithTimezone(0, "-08:00"), "YYYY-MM-dd HH:mm:ss"));
3303+
EXPECT_EQ(
3304+
"1969-12-31 23:45:00",
3305+
formatDatetimeWithTimezone(
3306+
TimestampWithTimezone(0, "-00:15"), "YYYY-MM-dd HH:mm:ss"));
3307+
EXPECT_EQ(
3308+
"1970-01-01 00:07:00",
3309+
formatDatetimeWithTimezone(
3310+
TimestampWithTimezone(0, "+00:07"), "YYYY-MM-dd HH:mm:ss"));
32973311
}
32983312

32993313
TEST_F(DateTimeFunctionsTest, dateFormat) {

0 commit comments

Comments
 (0)