Skip to content

Commit

Permalink
Add support for zzzz (and beyond) in format_datetime
Browse files Browse the repository at this point in the history
Summary:
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 22, 2024
1 parent e198906 commit cf2ef03
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
41 changes: 35 additions & 6 deletions velox/functions/lib/DateTimeFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#include "velox/functions/lib/DateTimeFormatter.h"
#include <folly/String.h>
#include <unicode/locid.h>
#include <unicode/timezone.h>
#include <unicode/tzfmt.h>
#include <unicode/unistr.h>
#include <charconv>
#include <cstring>
#include <stdexcept>
Expand Down Expand Up @@ -1173,10 +1177,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 @@ -1430,8 +1434,33 @@ 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");
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
const std::string& timeZoneName = timezone->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,
(double)timestamp.getSeconds() * 1000.0, // ICU expects a double
longName);

// Convert the UnicodeString back to a string and write it out
std::string longNameStr;
longName.toUTF8String(longNameStr);
std::memcpy(result, longNameStr.data(), longNameStr.length());
result += longNameStr.length();
}
} break;

Expand Down
26 changes: 22 additions & 4 deletions velox/functions/prestosql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3272,21 +3272,39 @@ 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"));
EXPECT_EQ(
"Pacific Daylight Time",
formatDatetime(parseTimestamp("1970-10-01"), "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

0 comments on commit cf2ef03

Please sign in to comment.