Skip to content

Commit

Permalink
Add support for zzzz (and beyond) in format_datetime (#11330)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #11330

This diff adds support for JODA's zzzz (or more) patterns (all equivalent) in Presto's
forma_datetime function.

This is used to format long time zone names.

Long time zone names are not available from the IANA time zone database, so we
can't use the tz library to generate these.  Fortunately, unicode provides some
utilities to generate these.

Differential Revision: D64795407
  • Loading branch information
Kevin Wilfong authored and facebook-github-bot committed Oct 24, 2024
1 parent 29d40d7 commit 4e23457
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 12 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,18 @@ add_compile_definitions(FOLLY_HAVE_INT128_T=1)
set_source(folly)
resolve_dependency(folly)

if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
if(ON_APPLE_M1)
list(APPEND CMAKE_PREFIX_PATH "/opt/homebrew/opt/icu4c")
else()
list(APPEND CMAKE_PREFIX_PATH "/usr/local/opt/icu4c")
endif()
endif()

# ICU is only needed with Boost build from source
set_source(ICU)
resolve_dependency(ICU COMPONENTS i18n uc)

if(${VELOX_BUILD_TESTING})
# Spark qury runner depends on absl, gRPC.
set_source(absl)
Expand Down
3 changes: 2 additions & 1 deletion velox/functions/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ velox_link_libraries(velox_functions_util velox_vector velox_common_base)
velox_add_library(velox_functions_lib_date_time_formatter DateTimeFormatter.cpp
DateTimeFormatterBuilder.cpp)

velox_link_libraries(velox_functions_lib_date_time_formatter velox_type_tz)
velox_link_libraries(velox_functions_lib_date_time_formatter velox_type_tz
ICU::i18n ICU::uc)

velox_add_library(
velox_functions_lib
Expand Down
14 changes: 8 additions & 6 deletions velox/functions/lib/DateTimeFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,10 +1173,10 @@ uint32_t DateTimeFormatter::maxResultSize(const tz::TimeZone* timezone) const {
// https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
size += 5;
} else {
VELOX_NYI(
"Date format specifier is not yet implemented: {} ({})",
getSpecifierName(token.pattern.specifier),
token.pattern.minRepresentDigits);
// The longest time zone long name is 40, Australian Central Western
// Standard Time.
// https://www.timeanddate.com/time/zones/
size += 50;
}

break;
Expand Down Expand Up @@ -1432,8 +1432,10 @@ int32_t DateTimeFormatter::format(
std::memcpy(result, abbrev.data(), abbrev.length());
result += abbrev.length();
} else {
// TODO: implement full name time zone
VELOX_NYI("full time zone name is not yet supported");
std::string longName = timezone->getLongName(
std::chrono::milliseconds(timestamp.toMillis()));
std::memcpy(result, longName.data(), longName.length());
result += longName.length();
}
} break;

Expand Down
30 changes: 26 additions & 4 deletions velox/functions/prestosql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3272,21 +3272,43 @@ TEST_F(DateTimeFunctionsTest, formatDateTime) {
EXPECT_EQ("IST", formatDatetime(parseTimestamp("1970-01-01"), "zzz"));
EXPECT_EQ("IST", formatDatetime(parseTimestamp("1970-01-01"), "zz"));
EXPECT_EQ("IST", formatDatetime(parseTimestamp("1970-01-01"), "z"));
EXPECT_EQ(
"India Standard Time",
formatDatetime(parseTimestamp("1970-01-01"), "zzzz"));
EXPECT_EQ(
"India Standard Time",
formatDatetime(parseTimestamp("1970-01-01"), "zzzzzzzzzzzzzzzzzzzzzz"));

// Test daylight savings.
setQueryTimeZone("America/Los_Angeles");
EXPECT_EQ("PST", formatDatetime(parseTimestamp("1970-01-01"), "z"));
EXPECT_EQ("PDT", formatDatetime(parseTimestamp("1970-10-01"), "z"));
EXPECT_EQ(
"Pacific Standard Time",
formatDatetime(parseTimestamp("1970-01-01"), "zzzz"));

// Test ambiguous time.
EXPECT_EQ(
"PDT", formatDatetime(parseTimestamp("2024-11-03 01:30:00"), "zzz"));
EXPECT_EQ(
"Pacific Daylight Time",
formatDatetime(parseTimestamp("2024-11-03 01:30:00"), "zzzz"));

// Test a long abbreviation.
setQueryTimeZone("Asia/Colombo");
EXPECT_EQ("+0530", formatDatetime(parseTimestamp("1970-10-01"), "z"));
EXPECT_EQ(
"India Standard Time",
formatDatetime(parseTimestamp("1970-10-01"), "zzzz"));

setQueryTimeZone("Asia/Kolkata");
// We don't support more than 3 'z's yet.
EXPECT_THROW(
formatDatetime(parseTimestamp("1970-01-01"), "zzzz"), VeloxRuntimeError);
// Test a long long name.
setQueryTimeZone("Australia/Eucla");
EXPECT_EQ("+0845", formatDatetime(parseTimestamp("1970-10-01"), "z"));
EXPECT_EQ(
"Australian Central Western Standard Time",
formatDatetime(parseTimestamp("1970-10-01"), "zzzz"));

setQueryTimeZone("Asia/Kolkata");
// Literal test cases.
EXPECT_EQ("hello", formatDatetime(parseTimestamp("1970-01-01"), "'hello'"));
EXPECT_EQ("'", formatDatetime(parseTimestamp("1970-01-01"), "''"));
Expand Down
4 changes: 3 additions & 1 deletion velox/type/tz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ velox_link_libraries(
velox_external_date
Boost::regex
fmt::fmt
Folly::folly)
Folly::folly
ICU::i18n
ICU::uc)
34 changes: 34 additions & 0 deletions velox/type/tz/TimeZoneMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#pragma once

#include <unicode/locid.h>
#include <unicode/timezone.h>
#include <unicode/tzfmt.h>
#include <unicode/unistr.h>
#include <chrono>
#include <string>
#include "velox/common/base/Exceptions.h"
Expand Down Expand Up @@ -178,6 +182,36 @@ class TimeZone {
return getZonedTime(timePoint, choose).get_info().abbrev;
}

template <typename TDuration>
std::string getLongName(TDuration timestamp) const {
UErrorCode success = U_ZERO_ERROR;

static const icu::Locale locale("en", "US");
static const std::unique_ptr<icu::TimeZoneFormat> format(
icu::TimeZoneFormat::createInstance(locale, success));
VELOX_USER_CHECK_NOT_NULL(format);

// Get the ICU TimeZone by name
std::unique_ptr<icu::TimeZone> tz(icu::TimeZone::createTimeZone(
icu::UnicodeString(timeZoneName_.data(), timeZoneName_.length())));
VELOX_USER_CHECK_NOT_NULL(tz);

// Format the time zone to get the long name.
icu::UnicodeString longName;
format->format(
UTimeZoneFormatStyle::UTZFMT_STYLE_SPECIFIC_LONG,
*tz,
std::chrono::duration_cast<std::chrono::milliseconds>(timestamp)
.count(),
longName);

// Convert the UnicodeString back to a string and write it out
std::string longNameStr;
longName.toUTF8String(longNameStr);

return longNameStr;
}

private:
template <typename TDuration>
date::zoned_time<TDuration> getZonedTime(
Expand Down

0 comments on commit 4e23457

Please sign in to comment.