Bug 1669573 - Replace kDateFormatYearMonth/kDateFormatYearMonthLong with FormatDateTime; r=zbraniecki

Differential Revision: https://phabricator.services.mozilla.com/D93013
This commit is contained in:
Dan Minor 2020-10-15 12:19:42 +00:00
parent cc0579260c
commit a67119a8ad
4 changed files with 13 additions and 80 deletions

View File

@ -274,9 +274,7 @@ nsresult DateTimeFormat::FormatUDateTime(
// We didn't have a cached formatter for this key, so create one.
// Get the date style for the formatter.
nsAutoString skeletonDate;
nsAutoString patternDate;
bool haveSkeleton = true;
switch (aDateFormatSelector) {
case kDateFormatLong:
rv = OSPreferences::GetInstance()->GetDateTimePattern(
@ -284,7 +282,6 @@ nsresult DateTimeFormat::FormatUDateTime(
mozIOSPreferences::dateTimeFormatStyleNone,
nsDependentCString(mLocale->get()), patternDate);
NS_ENSURE_SUCCESS(rv, rv);
haveSkeleton = false;
break;
case kDateFormatShort:
rv = OSPreferences::GetInstance()->GetDateTimePattern(
@ -292,50 +289,14 @@ nsresult DateTimeFormat::FormatUDateTime(
mozIOSPreferences::dateTimeFormatStyleNone,
nsDependentCString(mLocale->get()), patternDate);
NS_ENSURE_SUCCESS(rv, rv);
haveSkeleton = false;
break;
case kDateFormatYearMonth:
skeletonDate.AssignLiteral("yyyyMM");
break;
case kDateFormatYearMonthLong:
skeletonDate.AssignLiteral("yyyyMMMM");
break;
case kDateFormatNone:
haveSkeleton = false;
break;
default:
NS_ERROR("Unknown nsDateFormatSelector");
return NS_ERROR_ILLEGAL_VALUE;
}
if (haveSkeleton) {
// Get pattern for skeleton.
UDateTimePatternGenerator* patternGenerator =
udatpg_open(mLocale->get(), &status);
if (U_SUCCESS(status)) {
int32_t patternLength;
patternDate.SetLength(DATETIME_FORMAT_INITIAL_LEN);
patternLength = udatpg_getBestPattern(
patternGenerator,
reinterpret_cast<const UChar*>(skeletonDate.BeginReading()),
skeletonDate.Length(),
reinterpret_cast<UChar*>(patternDate.BeginWriting()),
DATETIME_FORMAT_INITIAL_LEN, &status);
patternDate.SetLength(patternLength);
if (status == U_BUFFER_OVERFLOW_ERROR) {
status = U_ZERO_ERROR;
udatpg_getBestPattern(
patternGenerator,
reinterpret_cast<const UChar*>(skeletonDate.BeginReading()),
skeletonDate.Length(),
reinterpret_cast<UChar*>(patternDate.BeginWriting()),
patternLength, &status);
}
}
udatpg_close(patternGenerator);
}
// Get the time style for the formatter.
nsAutoString patternTime;
switch (aTimeFormatSelector) {

View File

@ -18,11 +18,9 @@ namespace mozilla {
enum nsDateFormatSelector : long {
// Do not change the order of the values below (see bug 1225696).
kDateFormatNone = 0, // do not include the date in the format string
kDateFormatLong, // provides the long date format for the given locale
kDateFormatShort, // provides the short date format for the given locale
kDateFormatYearMonth, // formats using only the year and month
kDateFormatYearMonthLong, // long version of kDateFormatYearMonth
kDateFormatNone = 0, // do not include the date in the format string
kDateFormatLong, // provides the long date format for the given locale
kDateFormatShort, // provides the short date format for the given locale
};
enum nsTimeFormatSelector : long {

View File

@ -99,14 +99,13 @@ TEST(DateTimeFormat, DateFormatSelectors)
mozilla::DateTimeFormat::DeleteCache();
nsAutoString formattedTime;
nsresult rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
kDateFormatYearMonth, kTimeFormatNone, &prExplodedTime, formattedTime);
nsresult rv = mozilla::DateTimeFormat::FormatDateTime(
&prExplodedTime, DateTimeFormat::Skeleton::yyyyMM, formattedTime);
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_STREQ("01/1970", NS_ConvertUTF16toUTF8(formattedTime).get());
rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
kDateFormatYearMonthLong, kTimeFormatNone, &prExplodedTime,
formattedTime);
rv = mozilla::DateTimeFormat::FormatDateTime(
&prExplodedTime, DateTimeFormat::Skeleton::yyyyMMMM, formattedTime);
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_STREQ("January 1970", NS_ConvertUTF16toUTF8(formattedTime).get());
@ -116,18 +115,6 @@ TEST(DateTimeFormat, DateFormatSelectors)
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_STREQ("January", NS_ConvertUTF16toUTF8(formattedTime).get());
rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
kDateFormatYearMonth, kTimeFormatShort, &prExplodedTime, formattedTime);
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_STREQ("01/1970, 12:00 AM",
nt(NS_ConvertUTF16toUTF8(formattedTime)).get());
rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
kDateFormatYearMonth, kTimeFormatLong, &prExplodedTime, formattedTime);
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_STREQ("01/1970, 12:00:00 AM",
nt(NS_ConvertUTF16toUTF8(formattedTime)).get());
rv = mozilla::DateTimeFormat::GetCalendarSymbol(
mozilla::DateTimeFormat::Field::Weekday,
mozilla::DateTimeFormat::Style::Abbreviated, &prExplodedTime,
@ -227,14 +214,13 @@ TEST(DateTimeFormat, DateFormatSelectorsForeign)
mozilla::DateTimeFormat::DeleteCache();
nsAutoString formattedTime;
nsresult rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
kDateFormatYearMonth, kTimeFormatNone, &prExplodedTime, formattedTime);
nsresult rv = mozilla::DateTimeFormat::FormatDateTime(
&prExplodedTime, DateTimeFormat::Skeleton::yyyyMM, formattedTime);
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_STREQ("01.1970", NS_ConvertUTF16toUTF8(formattedTime).get());
rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
kDateFormatYearMonthLong, kTimeFormatNone, &prExplodedTime,
formattedTime);
rv = mozilla::DateTimeFormat::FormatDateTime(
&prExplodedTime, DateTimeFormat::Skeleton::yyyyMMMM, formattedTime);
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_STREQ("Januar 1970", NS_ConvertUTF16toUTF8(formattedTime).get());
@ -244,18 +230,6 @@ TEST(DateTimeFormat, DateFormatSelectorsForeign)
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_STREQ("Januar", NS_ConvertUTF16toUTF8(formattedTime).get());
rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
kDateFormatYearMonth, kTimeFormatShort, &prExplodedTime, formattedTime);
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_STREQ("01.1970, 00:00",
ntd(NS_ConvertUTF16toUTF8(formattedTime)).get());
rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
kDateFormatYearMonth, kTimeFormatLong, &prExplodedTime, formattedTime);
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_STREQ("01.1970, 00:00:00",
ntd(NS_ConvertUTF16toUTF8(formattedTime)).get());
rv = mozilla::DateTimeFormat::GetCalendarSymbol(
mozilla::DateTimeFormat::Field::Weekday,
mozilla::DateTimeFormat::Style::Abbreviated, &prExplodedTime,

View File

@ -3259,8 +3259,8 @@ void nsNavHistory::GetMonthName(const PRExplodedTime& aTime,
void nsNavHistory::GetMonthYear(const PRExplodedTime& aTime,
nsACString& aResult) {
nsAutoString monthYear;
nsresult rv = DateTimeFormat::FormatPRExplodedTime(
kDateFormatYearMonthLong, kTimeFormatNone, &aTime, monthYear);
nsresult rv = mozilla::DateTimeFormat::FormatDateTime(
&aTime, DateTimeFormat::Skeleton::yyyyMMMM, monthYear);
if (NS_FAILED(rv)) {
aResult = nsPrintfCString("[%d-%d]", aTime.tm_month + 1, aTime.tm_year);
return;