Add Localizer::monthFormat and implementations
https://bugs.webkit.org/show_bug.cgi?id=99704
Reviewed by Kentaro Hara.
Source/WebCore:
Localizer::monthFormat will be used for constructing input[type=month] UI.
Tests: Add unit tests to Source/WebKit/chromium/tests/.
* platform/text/Localizer.h:
(Localizer): Declare pure virtual monthFormat function.
* platform/text/LocaleNone.cpp:
(LocaleNone): Declare monthFormat.
(WebCore::LocaleNone::monthFormat):
Added. Always reutrns an ISO-8601 format, "yyyy-MM"
* platform/text/LocaleICU.h:
(LocaleICU): Declare monthFormat.
* platform/text/LocaleICU.cpp:
(WebCore::getFormatForSkeleton):
A helper to get a format for the specified skeleton.
The overflow-allocalte-try-again pattern is similar to
LocaleICU::decimalSymbol and LocaleICU::decimalTextAttribute.
(WebCore::LocaleICU::monthFormat):
Added. Calls getFormatForSkeleton with "yyyyMMM".
* platform/text/mac/LocaleMac.h:
(LocaleMac): Declare monthFormat.
* platform/text/mac/LocaleMac.mm:
(WebCore::LocaleMac::monthFormat):
Added. Calls NSDateFormatter::dateFormatFromTemplate with "yyyyMMM".
* platform/text/LocaleWin.h:
(LocaleWin): Declare monthFormat.
* platform/text/LocaleWin.cpp:
(WebCore::LocaleWin::monthFormat):
Get a format by LOCALE_SYEARMONTH, and convert it to an LDML format.
Source/WebKit/chromium:
* tests/LocaleMacTest.cpp:
(LocaleMacTest::monthFormat): A helper function.
(TEST_F): Added some tests.
* tests/LocaleWinTest.cpp:
(LocaleWinTest::monthFormat): A helper function.
(TEST_F): Added some tests.
* tests/LocalizedDateICUTest.cpp:
(LocalizedDateICUTest::monthFormat): A helper function.
(TEST_F): Added some tests.
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@131749 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/platform/text/LocaleICU.cpp b/Source/WebCore/platform/text/LocaleICU.cpp
index ce5f0afc..fe1a0d73 100644
--- a/Source/WebCore/platform/text/LocaleICU.cpp
+++ b/Source/WebCore/platform/text/LocaleICU.cpp
@@ -33,6 +33,7 @@
#include "LocalizedStrings.h"
#include <limits>
+#include <unicode/udatpg.h>
#include <unicode/uloc.h>
#include <wtf/DateMath.h>
#include <wtf/PassOwnPtr.h>
@@ -398,6 +399,36 @@
return m_dateFormat;
}
+static String getFormatForSkeleton(const char* locale, const String& skeleton)
+{
+ String format = ASCIILiteral("yyyy-MM");
+ UErrorCode status = U_ZERO_ERROR;
+ UDateTimePatternGenerator* patternGenerator = udatpg_open(locale, &status);
+ if (!patternGenerator)
+ return format;
+ status = U_ZERO_ERROR;
+ int32_t length = udatpg_getBestPattern(patternGenerator, skeleton.characters(), skeleton.length(), 0, 0, &status);
+ if (status == U_BUFFER_OVERFLOW_ERROR && length) {
+ Vector<UChar> buffer(length);
+ status = U_ZERO_ERROR;
+ udatpg_getBestPattern(patternGenerator, skeleton.characters(), skeleton.length(), buffer.data(), length, &status);
+ if (U_SUCCESS(status))
+ format = String::adopt(buffer);
+ }
+ udatpg_close(patternGenerator);
+ return format;
+}
+
+String LocaleICU::monthFormat()
+{
+ if (!m_monthFormat.isNull())
+ return m_monthFormat;
+ // Gets a format for "MMM", not "MM" because Windows API always provides
+ // formats for "MMM".
+ m_monthFormat = getFormatForSkeleton(m_locale.data(), ASCIILiteral("yyyyMMM"));
+ return m_monthFormat;
+}
+
String LocaleICU::timeFormat()
{
initializeDateTimeFormat();
diff --git a/Source/WebCore/platform/text/LocaleICU.h b/Source/WebCore/platform/text/LocaleICU.h
index a4b8122..92b310b 100644
--- a/Source/WebCore/platform/text/LocaleICU.h
+++ b/Source/WebCore/platform/text/LocaleICU.h
@@ -62,6 +62,7 @@
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
virtual String dateFormat() OVERRIDE;
+ virtual String monthFormat() OVERRIDE;
virtual String timeFormat() OVERRIDE;
virtual String shortTimeFormat() OVERRIDE;
virtual const Vector<String>& timeAMPMLabels() OVERRIDE;
@@ -107,6 +108,7 @@
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
String m_dateFormat;
+ String m_monthFormat;
UDateFormat* m_mediumTimeFormat;
UDateFormat* m_shortTimeFormat;
Vector<String> m_timeAMPMLabels;
diff --git a/Source/WebCore/platform/text/LocaleNone.cpp b/Source/WebCore/platform/text/LocaleNone.cpp
index 0ddaf30..6b4465c 100644
--- a/Source/WebCore/platform/text/LocaleNone.cpp
+++ b/Source/WebCore/platform/text/LocaleNone.cpp
@@ -42,6 +42,7 @@
#endif
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
virtual String dateFormat() OVERRIDE;
+ virtual String monthFormat() OVERRIDE;
#endif
};
@@ -80,6 +81,11 @@
{
return ASCIILiteral("dd/MM/yyyyy");
}
+
+String LocaleNone::monthFormat()
+{
+ return ASCIILiteral("yyyy-MM");
+}
#endif
} // namespace WebCore
diff --git a/Source/WebCore/platform/text/LocaleWin.cpp b/Source/WebCore/platform/text/LocaleWin.cpp
index 097b093..ef16680 100644
--- a/Source/WebCore/platform/text/LocaleWin.cpp
+++ b/Source/WebCore/platform/text/LocaleWin.cpp
@@ -713,6 +713,14 @@
return convertWindowsDateFormatToLDML(parseDateFormat(windowsFormat));
}
+String LocaleWin::monthFormat()
+{
+ if (!m_monthFormat.isNull())
+ return m_monthFormat;
+ m_monthFormat = convertWindowsDateFormatToLDML(parseDateFormat(getLocaleInfoString(LOCALE_SYEARMONTH)));
+ return m_monthFormat;
+}
+
String LocaleWin::timeFormat()
{
if (m_localizedTimeFormatText.isEmpty())
diff --git a/Source/WebCore/platform/text/LocaleWin.h b/Source/WebCore/platform/text/LocaleWin.h
index 21ad7bf..5e0f13f 100644
--- a/Source/WebCore/platform/text/LocaleWin.h
+++ b/Source/WebCore/platform/text/LocaleWin.h
@@ -57,6 +57,7 @@
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
virtual String dateFormat() OVERRIDE;
+ virtual String monthFormat() OVERRIDE;
virtual String timeFormat() OVERRIDE;
virtual String shortTimeFormat() OVERRIDE;
virtual const Vector<String>& timeAMPMLabels() OVERRIDE;
@@ -98,6 +99,7 @@
Vector<String> m_monthLabels;
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
String m_dateFormat;
+ String m_monthFormat;
#endif
#if ENABLE(CALENDAR_PICKER)
Vector<String> m_weekDayShortLabels;
diff --git a/Source/WebCore/platform/text/Localizer.h b/Source/WebCore/platform/text/Localizer.h
index 2ed277c..92b643e 100644
--- a/Source/WebCore/platform/text/Localizer.h
+++ b/Source/WebCore/platform/text/Localizer.h
@@ -62,6 +62,9 @@
// [1] LDML http://unicode.org/reports/tr35/#Date_Format_Patterns
virtual String dateFormat() = 0;
+ // Returns a year-month format in Unicode TR35 LDML.
+ virtual String monthFormat() = 0;
+
// Returns time format in Unicode TR35 LDML[1] containing hour, minute, and
// second with optional period(AM/PM), e.g. "h:mm:ss a"
// [1] LDML http://unicode.org/reports/tr35/#Date_Format_Patterns
diff --git a/Source/WebCore/platform/text/mac/LocaleMac.h b/Source/WebCore/platform/text/mac/LocaleMac.h
index ac029ea..467fff4 100644
--- a/Source/WebCore/platform/text/mac/LocaleMac.h
+++ b/Source/WebCore/platform/text/mac/LocaleMac.h
@@ -62,6 +62,7 @@
#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
virtual String dateFormat() OVERRIDE;
+ virtual String monthFormat() OVERRIDE;
virtual String timeFormat() OVERRIDE;
virtual String shortTimeFormat() OVERRIDE;
virtual const Vector<String>& timeAMPMLabels() OVERRIDE;
@@ -84,6 +85,7 @@
RetainPtr<NSDateFormatter> shortTimeFormatter();
String m_dateFormat;
+ String m_monthFormat;
String m_localizedTimeFormatText;
String m_localizedShortTimeFormatText;
Vector<String> m_timeAMPMLabels;
diff --git a/Source/WebCore/platform/text/mac/LocaleMac.mm b/Source/WebCore/platform/text/mac/LocaleMac.mm
index a20254e..5dca17a 100644
--- a/Source/WebCore/platform/text/mac/LocaleMac.mm
+++ b/Source/WebCore/platform/text/mac/LocaleMac.mm
@@ -246,6 +246,16 @@
return m_dateFormat;
}
+String LocaleMac::monthFormat()
+{
+ if (!m_monthFormat.isNull())
+ return m_monthFormat;
+ // Gets a format for "MMM", not "MM" because Windows API always provides
+ // formats for "MMM".
+ m_monthFormat = [NSDateFormatter dateFormatFromTemplate:@"yyyyMMM" options:0 locale:m_locale.get()];
+ return m_monthFormat;
+}
+
String LocaleMac::timeFormat()
{
if (!m_localizedTimeFormatText.isNull())