From 6d9d8a2b18e532047d88279c1d19d88996c1dd04 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 24 Jun 2024 12:29:01 -0700 Subject: [PATCH] Make createTimeZone() a static method instead of a method on DateInfo --- .../i18n/messageformat2_formattable.cpp | 33 +---------------- .../i18n/messageformat2_function_registry.cpp | 36 +++++++++++++++++-- ...essageformat2_function_registry_internal.h | 2 +- .../i18n/unicode/messageformat2_formattable.h | 10 ------ 4 files changed, 36 insertions(+), 45 deletions(-) diff --git a/icu4c/source/i18n/messageformat2_formattable.cpp b/icu4c/source/i18n/messageformat2_formattable.cpp index fe8082bdd09..0bbecaf25d7 100644 --- a/icu4c/source/i18n/messageformat2_formattable.cpp +++ b/icu4c/source/i18n/messageformat2_formattable.cpp @@ -12,6 +12,7 @@ #include "unicode/messageformat2_formattable.h" #include "unicode/smpdtfmt.h" #include "messageformat2_allocation.h" +#include "messageformat2_function_registry_internal.h" #include "messageformat2_macros.h" #include "limits.h" @@ -218,22 +219,6 @@ namespace message2 { return number::NumberFormatter::withLocale(locale).formatDecimal(toFormat, errorCode); } - TimeZone* DateInfo::createTimeZone(UErrorCode& errorCode) const { - NULL_ON_ERROR(errorCode); - - TimeZone* tz; - if (zoneName.length() == 0) { - // Floating time value -- use default time zone - tz = TimeZone::createDefault(); - } else { - tz = TimeZone::createTimeZone(zoneName); - } - if (tz == nullptr) { - errorCode = U_MEMORY_ALLOCATION_ERROR; - } - return tz; - } - DateFormat* defaultDateTimeInstance(const Locale& locale, UErrorCode& errorCode) { NULL_ON_ERROR(errorCode); LocalPointer df(DateFormat::createDateTimeInstance(DateFormat::SHORT, DateFormat::SHORT, locale)); @@ -244,22 +229,6 @@ namespace message2 { return df.orphan(); } - void formatDateWithDefaults(const Locale& locale, - const DateInfo& dateInfo, - UnicodeString& result, - UErrorCode& errorCode) { - CHECK_ERROR(errorCode); - - LocalPointer df(defaultDateTimeInstance(locale, errorCode)); - CHECK_ERROR(errorCode); - - // Non-Gregorian calendars not supported yet - U_ASSERT(dateInfo.calendarName.length() == 0); - df->adoptTimeZone(dateInfo.createTimeZone(errorCode)); - CHECK_ERROR(errorCode); - df->format(dateInfo.date, result, nullptr, errorCode); - } - // Called when output is required and the contents are an unevaluated `Formattable`; // formats the source `Formattable` to a string with defaults, if it can be // formatted with a default formatter diff --git a/icu4c/source/i18n/messageformat2_function_registry.cpp b/icu4c/source/i18n/messageformat2_function_registry.cpp index 5f635455bb6..ee906381afd 100644 --- a/icu4c/source/i18n/messageformat2_function_registry.cpp +++ b/icu4c/source/i18n/messageformat2_function_registry.cpp @@ -1040,6 +1040,22 @@ static UDate tryTimeZonePatterns(const UnicodeString& sourceStr, UErrorCode& err return d; } +static TimeZone* createTimeZone(const DateInfo& dateInfo, UErrorCode& errorCode) { + NULL_ON_ERROR(errorCode); + + TimeZone* tz; + if (dateInfo.zoneName.length() == 0) { + // Floating time value -- use default time zone + tz = TimeZone::createDefault(); + } else { + tz = TimeZone::createTimeZone(dateInfo.zoneName); + } + if (tz == nullptr) { + errorCode = U_MEMORY_ALLOCATION_ERROR; + } + return tz; +} + // Parse a string like [+-]nn:nn to a time zone SimpleTimeZone* createTimeZonePart(const UnicodeString& offsetStr, UErrorCode& errorCode) { NULL_ON_ERROR(errorCode); @@ -1151,6 +1167,22 @@ static DateInfo createDateInfoFromString(const UnicodeString& sourceStr, UErrorC return { absoluteDate, canonicalID, {} }; } +void formatDateWithDefaults(const Locale& locale, + const DateInfo& dateInfo, + UnicodeString& result, + UErrorCode& errorCode) { + CHECK_ERROR(errorCode); + + LocalPointer df(defaultDateTimeInstance(locale, errorCode)); + CHECK_ERROR(errorCode); + + // Non-Gregorian calendars not supported yet + U_ASSERT(dateInfo.calendarName.length() == 0); + df->adoptTimeZone(createTimeZone(dateInfo, errorCode)); + CHECK_ERROR(errorCode); + df->format(dateInfo.date, result, nullptr, errorCode); +} + FormattedPlaceholder StandardFunctions::DateTime::format(FormattedPlaceholder&& toFormat, FunctionOptions&& opts, UErrorCode& errorCode) const { @@ -1345,7 +1377,7 @@ FormattedPlaceholder StandardFunctions::DateTime::format(FormattedPlaceholder&& errorCode = U_MF_OPERAND_MISMATCH_ERROR; return {}; } - df->adoptTimeZone(dateInfo.createTimeZone(errorCode)); + df->adoptTimeZone(createTimeZone(dateInfo, errorCode)); // Use the parsed date as the source value // in the returned FormattedPlaceholder; this is necessary @@ -1359,7 +1391,7 @@ FormattedPlaceholder StandardFunctions::DateTime::format(FormattedPlaceholder&& const DateInfo* dateInfo = source.getDate(errorCode); U_ASSERT(U_SUCCESS(errorCode)); - df->adoptTimeZone(dateInfo->createTimeZone(errorCode)); + df->adoptTimeZone(createTimeZone(*dateInfo, errorCode)); df->format(dateInfo->date, result, 0, errorCode); if (U_FAILURE(errorCode)) { if (errorCode == U_ILLEGAL_ARGUMENT_ERROR) { diff --git a/icu4c/source/i18n/messageformat2_function_registry_internal.h b/icu4c/source/i18n/messageformat2_function_registry_internal.h index 084f47e1f38..93942305046 100644 --- a/icu4c/source/i18n/messageformat2_function_registry_internal.h +++ b/icu4c/source/i18n/messageformat2_function_registry_internal.h @@ -272,7 +272,7 @@ namespace message2 { }; - extern void formatDateWithDefaults(const Locale& locale, UDate date, UnicodeString&, UErrorCode& errorCode); + extern void formatDateWithDefaults(const Locale& locale, const DateInfo& date, UnicodeString&, UErrorCode& errorCode); extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, double toFormat, UErrorCode& errorCode); extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, int32_t toFormat, UErrorCode& errorCode); extern number::FormattedNumber formatNumberWithDefaults(const Locale& locale, int64_t toFormat, UErrorCode& errorCode); diff --git a/icu4c/source/i18n/unicode/messageformat2_formattable.h b/icu4c/source/i18n/unicode/messageformat2_formattable.h index 04cfa73eeec..3878f79be3f 100644 --- a/icu4c/source/i18n/unicode/messageformat2_formattable.h +++ b/icu4c/source/i18n/unicode/messageformat2_formattable.h @@ -99,16 +99,6 @@ namespace message2 { * @deprecated This API is for technology preview only. */ UnicodeString calendarName; - /** - * Creates a `TimeZone` from this `DateInfo`'s time zone name. - * - * @param status Input/output error code. - * @return A TimeZone object, which the caller adopts. - * - * @internal ICU 76 technology preview - * @deprecated This API is for technology preview only. - */ - TimeZone* createTimeZone(UErrorCode& status) const; }; class Formattable;