Title: [121415] trunk/Source
Revision
121415
Author
[email protected]
Date
2012-06-28 00:30:11 -0700 (Thu, 28 Jun 2012)

Log Message

[Platform] Implement functions for localized time format information
https://bugs.webkit.org/show_bug.cgi?id=89965

Reviewed by Kent Tamura.

Source/WebCore:

This patch introduces three functions for time format:
  1. localizedTimeFormatText()
  2. localizedShortTimeFormatText()
  2. timeAMPMLabels()
for input type "time" if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS) is true.

Having both localizedTimeFormat and localizedShortTimeFormat is for
displaying only two fields hour and minute when step >= 60. There is
no way to remove second field from "h:m:s" pattern string. We don't
know whether ":" after "m" belongs minute or second field.

Test: WebKit/chromium/tests/LocalizedDateICUTest.cpp

* platform/text/LocaleICU.cpp:
(WebCore::LocaleICU::LocaleICU):
(WebCore::createFallbackAMPMLabels): Added.
(WebCore::LocaleICU::initializeDateTimeFormat):  Added.
(WebCore::LocaleICU::localizedTimeFormatText):  Added.
(WebCore::LocaleICU::localizedShortTimeFormatText):  Added.
(WebCore::LocaleICU::timeAMPMLabels):  Added.
* platform/text/LocaleICU.h:
(LocaleICU):
* platform/text/LocalizedDate.h:
* platform/text/LocalizedDateICU.cpp:
(WebCore::localizedTimeFormatText):  Added.
(WebCore::localizedShortTimeFormatText):  Added.
(WebCore::timeAMPMLabels):  Added.

Source/WebKit/chromium:

This patch adds new test LocalizedDateICUTest if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
is true.

* WebKit.gypi:
* tests/LocalizedDateICUTest.cpp: Added.
(LocalizedDateICUTest):
(Labels):
(LocalizedDateICUTest::Labels::Labels):
(LocalizedDateICUTest::Labels::operator==):
(LocalizedDateICUTest::Labels::toString):
(LocalizedDateICUTest::labels):
(LocalizedDateICUTest::localizedDateFormatText):
(LocalizedDateICUTest::localizedShortDateFormatText):
(LocalizedDateICUTest::timeAMPMLabels):
(operator<<):
(TEST_F):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (121414 => 121415)


--- trunk/Source/WebCore/ChangeLog	2012-06-28 07:25:27 UTC (rev 121414)
+++ trunk/Source/WebCore/ChangeLog	2012-06-28 07:30:11 UTC (rev 121415)
@@ -1,3 +1,38 @@
+2012-06-28  Yoshifumi Inoue  <[email protected]>
+
+        [Platform] Implement functions for localized time format information
+        https://bugs.webkit.org/show_bug.cgi?id=89965
+
+        Reviewed by Kent Tamura.
+
+        This patch introduces three functions for time format:
+          1. localizedTimeFormatText()
+          2. localizedShortTimeFormatText()
+          2. timeAMPMLabels()
+        for input type "time" if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS) is true.
+
+        Having both localizedTimeFormat and localizedShortTimeFormat is for
+        displaying only two fields hour and minute when step >= 60. There is
+        no way to remove second field from "h:m:s" pattern string. We don't
+        know whether ":" after "m" belongs minute or second field.
+
+        Test: WebKit/chromium/tests/LocalizedDateICUTest.cpp
+
+        * platform/text/LocaleICU.cpp:
+        (WebCore::LocaleICU::LocaleICU):
+        (WebCore::createFallbackAMPMLabels): Added.
+        (WebCore::LocaleICU::initializeDateTimeFormat):  Added.
+        (WebCore::LocaleICU::localizedTimeFormatText):  Added.
+        (WebCore::LocaleICU::localizedShortTimeFormatText):  Added.
+        (WebCore::LocaleICU::timeAMPMLabels):  Added.
+        * platform/text/LocaleICU.h:
+        (LocaleICU):
+        * platform/text/LocalizedDate.h:
+        * platform/text/LocalizedDateICU.cpp:
+        (WebCore::localizedTimeFormatText):  Added.
+        (WebCore::localizedShortTimeFormatText):  Added.
+        (WebCore::timeAMPMLabels):  Added.
+
 2012-06-27  Kentaro Hara  <[email protected]>
 
         Performance: Optimize Dromaeo/dom-query.html by caching NodeRareData on Document

Modified: trunk/Source/WebCore/platform/text/LocaleICU.cpp (121414 => 121415)


--- trunk/Source/WebCore/platform/text/LocaleICU.cpp	2012-06-28 07:25:27 UTC (rev 121414)
+++ trunk/Source/WebCore/platform/text/LocaleICU.cpp	2012-06-28 07:30:11 UTC (rev 121415)
@@ -51,6 +51,11 @@
 #if ENABLE(CALENDAR_PICKER)
     , m_firstDayOfWeek(0)
 #endif
+#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
+    , m_mediumTimeFormat(0)
+    , m_shortTimeFormat(0)
+    , m_didCreateTimeFormat(false)
+#endif
 {
 }
 
@@ -317,7 +322,7 @@
     return String::adopt(buffer);
 }
 
-#if ENABLE(CALENDAR_PICKER)
+#if ENABLE(CALENDAR_PICKER) || ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
 static String getDateFormatPattern(const UDateFormat* dateFormat)
 {
     if (!dateFormat)
@@ -334,7 +339,9 @@
         return emptyString();
     return String::adopt(buffer);
 }
+#endif
 
+#if ENABLE(CALENDAR_PICKER)
 static inline bool isICUYearSymbol(UChar letter)
 {
     return letter == 'y' || letter == 'Y';
@@ -501,6 +508,54 @@
     return m_decimalSymbols[DecimalSeparatorIndex];
 }
 
+static PassOwnPtr<Vector<String> > createFallbackAMPMLabels()
+{
+    OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>());
+    labels->reserveCapacity(2);
+    labels->append("AM");
+    labels->append("PM");
+    return labels.release();
+}
+
+void LocaleICU::initializeDateTimeFormat()
+{
+    if (m_didCreateTimeFormat)
+        return;
+
+    // We assume ICU medium time pattern and short time pattern are compatible
+    // with LDML, because ICU specific pattern character "V" doesn't appear
+    // in both medium and short time pattern.
+    m_mediumTimeFormat = openDateFormat(UDAT_MEDIUM, UDAT_NONE);
+    m_localizedTimeFormatText = getDateFormatPattern(m_mediumTimeFormat);
+
+    m_shortTimeFormat = openDateFormat(UDAT_SHORT, UDAT_NONE);
+    m_localizedShortTimeFormatText = getDateFormatPattern(m_shortTimeFormat);
+
+    m_timeAMPMLabels = createLabelVector(m_mediumTimeFormat, UDAT_AM_PMS, UCAL_AM, 2);
+    if (!m_timeAMPMLabels)
+        m_timeAMPMLabels = createFallbackAMPMLabels();
+
+    m_didCreateTimeFormat = true;
+}
+
+String LocaleICU::localizedTimeFormatText()
+{
+    initializeDateTimeFormat();
+    return m_localizedTimeFormatText;
+}
+
+String LocaleICU::localizedShortTimeFormatText()
+{
+    initializeDateTimeFormat();
+    return m_localizedShortTimeFormatText;
+}
+
+const Vector<String>& LocaleICU::timeAMPMLabels()
+{
+    initializeDateTimeFormat();
+    return *m_timeAMPMLabels;
+}
+
 #endif
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/platform/text/LocaleICU.h (121414 => 121415)


--- trunk/Source/WebCore/platform/text/LocaleICU.h	2012-06-28 07:25:27 UTC (rev 121414)
+++ trunk/Source/WebCore/platform/text/LocaleICU.h	2012-06-28 07:30:11 UTC (rev 121415)
@@ -67,6 +67,12 @@
     unsigned firstDayOfWeek();
 #endif
 
+#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
+    String localizedTimeFormatText();
+    String localizedShortTimeFormatText();
+    const Vector<String>& timeAMPMLabels();
+#endif
+
 private:
     static PassOwnPtr<LocaleICU> createForCurrentLocale();
     explicit LocaleICU(const char*);
@@ -82,10 +88,17 @@
 
 #if ENABLE(CALENDAR_PICKER)
     void initializeLocalizedDateFormatText();
-    PassOwnPtr<Vector<String> > createLabelVector(const UDateFormat*, UDateFormatSymbolType, int32_t startIndex, int32_t size);
     void initializeCalendar();
 #endif
 
+#if ENABLE(CALENDAR_PICKER) || ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
+    PassOwnPtr<Vector<String> > createLabelVector(const UDateFormat*, UDateFormatSymbolType, int32_t startIndex, int32_t size);
+#endif
+
+#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
+    void initializeDateTimeFormat();
+#endif
+
     CString m_locale;
     UNumberFormat* m_numberFormat;
     UDateFormat* m_shortDateFormat;
@@ -109,6 +122,15 @@
     OwnPtr<Vector<String> > m_weekDayShortLabels;
     unsigned m_firstDayOfWeek;
 #endif
+
+#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
+    UDateFormat* m_mediumTimeFormat;
+    UDateFormat* m_shortTimeFormat;
+    String m_localizedTimeFormatText;
+    String m_localizedShortTimeFormatText;
+    OwnPtr<Vector<String> > m_timeAMPMLabels;
+    bool m_didCreateTimeFormat;
+#endif
 };
 
 }

Modified: trunk/Source/WebCore/platform/text/LocalizedDate.h (121414 => 121415)


--- trunk/Source/WebCore/platform/text/LocalizedDate.h	2012-06-28 07:25:27 UTC (rev 121414)
+++ trunk/Source/WebCore/platform/text/LocalizedDate.h	2012-06-28 07:30:11 UTC (rev 121415)
@@ -60,7 +60,23 @@
 
 // The first day of a week. 0 is Sunday, and 6 is Saturday.
 unsigned firstDayOfWeek();
+
 #endif
+
+#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
+// 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/tr35-6.html#Date_Format_Patterns
+String localizedTimeFormatText();
+
+// Returns time format in Unicode TR35 LDML containing hour, and minute
+// with optional period(AM/PM), e.g. "h:mm a"
+// Note: Some platforms return same value as localizedTimeFormatText().
+String localizedShortTimeFormatText();
+
+// Returns localized period field(AM/PM) strings.
+const Vector<String>& timeAMPMLabels();
+#endif
 } // namespace WebCore
 
 #endif // LocalizedDate_h

Modified: trunk/Source/WebCore/platform/text/LocalizedDateICU.cpp (121414 => 121415)


--- trunk/Source/WebCore/platform/text/LocalizedDateICU.cpp	2012-06-28 07:25:27 UTC (rev 121414)
+++ trunk/Source/WebCore/platform/text/LocalizedDateICU.cpp	2012-06-28 07:30:11 UTC (rev 121415)
@@ -92,4 +92,21 @@
 }
 #endif
 
+#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
+String localizedTimeFormatText()
+{
+    return LocaleICU::currentLocale()->localizedTimeFormatText();
 }
+
+String localizedShortTimeFormatText()
+{
+    return LocaleICU::currentLocale()->localizedShortTimeFormatText();
+}
+
+const Vector<String>& timeAMPMLabels()
+{
+    return LocaleICU::currentLocale()->timeAMPMLabels();
+}
+#endif
+
+}

Modified: trunk/Source/WebKit/chromium/ChangeLog (121414 => 121415)


--- trunk/Source/WebKit/chromium/ChangeLog	2012-06-28 07:25:27 UTC (rev 121414)
+++ trunk/Source/WebKit/chromium/ChangeLog	2012-06-28 07:30:11 UTC (rev 121415)
@@ -1,3 +1,27 @@
+2012-06-28  Yoshifumi Inoue  <[email protected]>
+
+        [Platform] Implement functions for localized time format information
+        https://bugs.webkit.org/show_bug.cgi?id=89965
+
+        Reviewed by Kent Tamura.
+
+        This patch adds new test LocalizedDateICUTest if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
+        is true.
+
+        * WebKit.gypi:
+        * tests/LocalizedDateICUTest.cpp: Added.
+        (LocalizedDateICUTest):
+        (Labels):
+        (LocalizedDateICUTest::Labels::Labels):
+        (LocalizedDateICUTest::Labels::operator==):
+        (LocalizedDateICUTest::Labels::toString):
+        (LocalizedDateICUTest::labels):
+        (LocalizedDateICUTest::localizedDateFormatText):
+        (LocalizedDateICUTest::localizedShortDateFormatText):
+        (LocalizedDateICUTest::timeAMPMLabels):
+        (operator<<):
+        (TEST_F):
+
 2012-06-27  Sheriff Bot  <[email protected]>
 
         Unreviewed, rolling out r121405.

Modified: trunk/Source/WebKit/chromium/WebKit.gypi (121414 => 121415)


--- trunk/Source/WebKit/chromium/WebKit.gypi	2012-06-28 07:25:27 UTC (rev 121414)
+++ trunk/Source/WebKit/chromium/WebKit.gypi	2012-06-28 07:30:11 UTC (rev 121415)
@@ -123,6 +123,7 @@
             'tests/LevelDBTest.cpp',
             'tests/LinkHighlightTest.cpp',
             'tests/ListenerLeakTest.cpp',
+            'tests/LocalizedDateICUTest.cpp',
             'tests/LocalizedNumberICUTest.cpp',
             'tests/MockCCQuadCuller.h',
             'tests/OpaqueRectTrackingContentLayerDelegateTest.cpp',

Added: trunk/Source/WebKit/chromium/tests/LocalizedDateICUTest.cpp (0 => 121415)


--- trunk/Source/WebKit/chromium/tests/LocalizedDateICUTest.cpp	                        (rev 0)
+++ trunk/Source/WebKit/chromium/tests/LocalizedDateICUTest.cpp	2012-06-28 07:30:11 UTC (rev 121415)
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2012 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
+
+#include "LocaleICU.h"
+#include <gtest/gtest.h>
+#include <wtf/PassOwnPtr.h>
+#include <wtf/text/StringBuilder.h>
+
+using namespace WebCore;
+
+class LocalizedDateICUTest : public ::testing::Test {
+public:
+    // Labels class is used for printing results in EXPECT_EQ macro.
+    class Labels {
+    public:
+        Labels(const Vector<String> labels)
+            : m_labels(labels)
+        {
+        }
+
+        // FIXME: We should use Vector<T>::operator==() if it works.
+        bool operator==(const Labels& other) const
+        {
+            if (m_labels.size() != other.m_labels.size())
+                return false;
+            for (unsigned index = 0; index < m_labels.size(); ++index)
+                if (m_labels[index] != other.m_labels[index])
+                    return false;
+            return true;
+        }
+
+        String toString() const
+        {
+            StringBuilder builder;
+            builder.append("labels(");
+            for (unsigned index = 0; index < m_labels.size(); ++index) {
+                if (index)
+                    builder.append(", ");
+                builder.append('"');
+                builder.append(m_labels[index]);
+                builder.append('"');
+            }
+            builder.append(')');
+            return builder.toString();
+        }
+
+    private:
+        Vector<String> m_labels;
+    };
+
+protected:
+    Labels labels(const String& element1, const String& element2)
+    {
+        Vector<String> labels = Vector<String>();
+        labels.append(element1);
+        labels.append(element2);
+        return Labels(labels);
+    }
+
+    String localizedDateFormatText(const char* localeString)
+    {
+        OwnPtr<LocaleICU> locale = LocaleICU::create(localeString);
+        return locale->localizedTimeFormatText();
+    }
+
+    String localizedShortDateFormatText(const char* localeString)
+    {
+        OwnPtr<LocaleICU> locale = LocaleICU::create(localeString);
+        return locale->localizedShortTimeFormatText();
+    }
+
+    Labels timeAMPMLabels(const char* localeString)
+    {
+        OwnPtr<LocaleICU> locale = LocaleICU::create(localeString);
+        return Labels(locale->timeAMPMLabels());
+    }
+};
+
+std::ostream& operator<<(std::ostream& os, const LocalizedDateICUTest::Labels& labels)
+{
+    return os << labels.toString().utf8().data();
+}
+
+TEST_F(LocalizedDateICUTest, localizedDateFormatText)
+{
+    // Note: EXPECT_EQ(String, String) doesn't print result as string.
+    EXPECT_STREQ("h:mm:ss a", localizedDateFormatText("en_US").utf8().data());
+    EXPECT_STREQ("HH:mm:ss", localizedDateFormatText("fr").utf8().data());
+    EXPECT_STREQ("H:mm:ss", localizedDateFormatText("ja").utf8().data());
+}
+
+TEST_F(LocalizedDateICUTest, localizedShortDateFormatText)
+{
+    EXPECT_STREQ("h:mm a", localizedShortDateFormatText("en_US").utf8().data());
+    EXPECT_STREQ("HH:mm", localizedShortDateFormatText("fr").utf8().data());
+    EXPECT_STREQ("H:mm", localizedShortDateFormatText("ja").utf8().data());
+}
+
+TEST_F(LocalizedDateICUTest, timeAMPMLabels)
+{
+    EXPECT_EQ(labels("AM", "PM"), timeAMPMLabels("en_US"));
+    EXPECT_EQ(labels("AM", "PM"), timeAMPMLabels("fr"));
+
+    UChar jaAM[3] = { 0x5348, 0x524d, 0 };
+    UChar jaPM[3] = { 0x5348, 0x5F8C, 0 };
+    EXPECT_EQ(labels(String(jaAM), String(jaPM)), timeAMPMLabels("ja"));
+}
+
+#endif
Property changes on: trunk/Source/WebKit/chromium/tests/LocalizedDateICUTest.cpp
___________________________________________________________________

Added: svn:eol-style

_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to