From e08bac2c1dfbdec64c9d813097b87de8e813ba1a Mon Sep 17 00:00:00 2001 From: Yoshi Cheng-Hao Huang Date: Tue, 19 Oct 2021 07:34:00 +0000 Subject: [PATCH] Bug 1719551 - Part 1: Add DateTimePatternGenerator::GetPlaceholderPattern. r=platform-i18n-reviewers,anba,gregtatum Differential Revision: https://phabricator.services.mozilla.com/D128460 --- intl/components/gtest/TestDateTimeFormat.cpp | 8 +++++++ .../components/src/DateTimePatternGenerator.h | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/intl/components/gtest/TestDateTimeFormat.cpp b/intl/components/gtest/TestDateTimeFormat.cpp index 43a2a5a2c045..1d4de6e3287f 100644 --- a/intl/components/gtest/TestDateTimeFormat.cpp +++ b/intl/components/gtest/TestDateTimeFormat.cpp @@ -179,6 +179,14 @@ TEST(IntlDateTimePatternGenerator, GetSkeleton) ASSERT_TRUE(buffer.verboseMatches(u"yMd")); } +TEST(IntlDateTimePatternGenerator, GetPlaceholderPattern) +{ + auto gen = DateTimePatternGenerator::TryCreate("en").unwrap(); + auto span = gen->GetPlaceholderPattern(); + // The default date-time pattern for 'en' locale is u"{1}, {0}". + ASSERT_EQ(span, MakeStringSpan(u"{1}, {0}")); +} + // A utility function to help test the DateTimeFormat::ComponentsBag. [[nodiscard]] bool FormatComponents( TestBuffer& aBuffer, DateTimeFormat::ComponentsBag& aComponents, diff --git a/intl/components/src/DateTimePatternGenerator.h b/intl/components/src/DateTimePatternGenerator.h index a5de35cee74c..0d3da1b24258 100644 --- a/intl/components/src/DateTimePatternGenerator.h +++ b/intl/components/src/DateTimePatternGenerator.h @@ -92,6 +92,28 @@ class DateTimePatternGenerator final { }); } + /** + * Get a pattern of the form "{1} {0}" to combine separate date and time + * patterns into a single pattern. The "{0}" part is the placeholder for the + * time pattern and "{1}" is the placeholder for the date pattern. + * + * See dateTimeFormat from + * https://unicode.org/reports/tr35/tr35-dates.html#dateTimeFormat + * + * Note: + * In CLDR, it's called Date-Time Combined Format + * https://cldr.unicode.org/translation/date-time/datetime-patterns#h.x7ca7qwzh4m + * + * The naming 'placeholder pattern' is from ICU4X. + * https://unicode-org.github.io/icu4x-docs/doc/icu_pattern/index.html + */ + Span GetPlaceholderPattern() const { + int32_t length; + const char16_t* combined = + udatpg_getDateTimeFormat(mGenerator.GetConst(), &length); + return Span{combined, static_cast(length)}; + } + /** * TODO(Bug 1686965) - Temporarily get the underlying ICU object while * migrating to the unified API. This should be removed when completing the