Make createTimeZone() a static method instead of a method on DateInfo

This commit is contained in:
Tim Chevalier 2024-06-24 12:29:01 -07:00
parent dea6034c66
commit 6d9d8a2b18
4 changed files with 36 additions and 45 deletions

View file

@ -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<DateFormat> 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<DateFormat> 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

View file

@ -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<DateFormat> 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) {

View file

@ -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);

View file

@ -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;