Title: [134216] trunk/Source
Revision
134216
Author
kei...@webkit.org
Date
2012-11-12 05:23:34 -0800 (Mon, 12 Nov 2012)

Log Message

Add support for week/month to Locale::formatDateTime()
https://bugs.webkit.org/show_bug.cgi?id=101878

Reviewed by Kent Tamura.

Source/WebCore:

Adding support for week/month to Locale::formatDateTime() in preparation for datalist support for <input type=week/month>.

Added Chromium tests LocaleMacTest.formatWeek and LocaleMacTest.formatMonth.

* platform/text/PlatformLocale.cpp:
(WebCore::DateTimeStringBuilder::visitField):
(WebCore::Locale::formatDateTime): Support week and month types.

Source/WebKit/chromium:

* tests/LocaleMacTest.cpp:
(LocaleMacTest::formatWeek): Takes ISO string and returns localized string.
(LocaleMacTest):
(LocaleMacTest::formatMonth): Ditto.
(TEST_F):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (134215 => 134216)


--- trunk/Source/WebCore/ChangeLog	2012-11-12 13:21:28 UTC (rev 134215)
+++ trunk/Source/WebCore/ChangeLog	2012-11-12 13:23:34 UTC (rev 134216)
@@ -1,3 +1,18 @@
+2012-11-12  Keishi Hattori  <kei...@webkit.org>
+
+        Add support for week/month to Locale::formatDateTime()
+        https://bugs.webkit.org/show_bug.cgi?id=101878
+
+        Reviewed by Kent Tamura.
+
+        Adding support for week/month to Locale::formatDateTime() in preparation for datalist support for <input type=week/month>.
+
+        Added Chromium tests LocaleMacTest.formatWeek and LocaleMacTest.formatMonth.
+
+        * platform/text/PlatformLocale.cpp:
+        (WebCore::DateTimeStringBuilder::visitField):
+        (WebCore::Locale::formatDateTime): Support week and month types.
+
 2012-11-12  Allan Sandfeld Jensen  <allan.jen...@digia.com>
 
         [Qt] Can not load MHTML documents

Modified: trunk/Source/WebCore/platform/text/PlatformLocale.cpp (134215 => 134216)


--- trunk/Source/WebCore/platform/text/PlatformLocale.cpp	2012-11-12 13:21:28 UTC (rev 134215)
+++ trunk/Source/WebCore/platform/text/PlatformLocale.cpp	2012-11-12 13:23:34 UTC (rev 134216)
@@ -32,6 +32,7 @@
 #include "PlatformLocale.h"
 
 #include "DateTimeFormat.h"
+#include "LocalizedStrings.h"
 #include <wtf/text/StringBuilder.h>
 
 namespace WebCore {
@@ -98,14 +99,34 @@
         // Always use padding width of 4 so it matches DateTimeEditElement.
         appendNumber(m_date.fullYear(), 4);
         return;
-    case DateTimeFormat::FieldTypeMonth:
-        // Always use padding width of 2 so it matches DateTimeEditElement.
-        appendNumber(m_date.month() + 1, 2);
+    case DateTimeFormat::FieldTypeMonth:    
+        if (numberOfPatternCharacters == 3)
+            m_builder.append(m_localizer.shortMonthLabels()[m_date.month()]);
+        else if (numberOfPatternCharacters == 4)
+            m_builder.append(m_localizer.monthLabels()[m_date.month()]);
+        else {
+            // Always use padding width of 2 so it matches DateTimeEditElement.
+            appendNumber(m_date.month() + 1, 2);
+        }
         return;
+    case DateTimeFormat::FieldTypeMonthStandAlone:
+        if (numberOfPatternCharacters == 3)
+            m_builder.append(m_localizer.shortStandAloneMonthLabels()[m_date.month()]);
+        else if (numberOfPatternCharacters == 4)
+            m_builder.append(m_localizer.standAloneMonthLabels()[m_date.month()]);
+        else {
+            // Always use padding width of 2 so it matches DateTimeEditElement.
+            appendNumber(m_date.month() + 1, 2);
+        }
+        return;
     case DateTimeFormat::FieldTypeDayOfMonth:
         // Always use padding width of 2 so it matches DateTimeEditElement.
         appendNumber(m_date.monthDay(), 2);
         return;
+    case DateTimeFormat::FieldTypeWeekOfYear:
+        // Always use padding width of 2 so it matches DateTimeEditElement.
+        appendNumber(m_date.week(), 2);
+        return;
     case DateTimeFormat::FieldTypePeriod:
         m_builder.append(m_localizer.timeAMPMLabels()[(m_date.hour() >= 12 ? 1 : 0)]);
         return;
@@ -345,15 +366,30 @@
 
 String Locale::formatDateTime(const DateComponents& date, FormatType formatType)
 {
-    if (date.type() != DateComponents::Time && date.type() != DateComponents::Date)
+    if (date.type() == DateComponents::DateTime || date.type() == DateComponents::DateTimeLocal || date.type() == DateComponents::Invalid)
         return String();
     // FIXME: Supports all types.
 
     DateTimeStringBuilder builder(*this, date);
-    if (date.type() == DateComponents::Time)
+    switch (date.type()) {
+    case DateComponents::Time:
         builder.build(formatType == FormatTypeShort ? shortTimeFormat() : timeFormat());
-    else if (date.type() == DateComponents::Date)
+        break;
+    case DateComponents::Date:
         builder.build(dateFormat());
+        break;
+    case DateComponents::Week:
+        builder.build(weekFormatInLDML());
+        break;
+    case DateComponents::Month:
+        builder.build(monthFormat());
+        break;
+    case DateComponents::Invalid:
+    case DateComponents::DateTime:
+    case DateComponents::DateTimeLocal:
+        ASSERT_NOT_REACHED();
+        break;
+    }
     return builder.toString();
 }
 #endif

Modified: trunk/Source/WebKit/chromium/ChangeLog (134215 => 134216)


--- trunk/Source/WebKit/chromium/ChangeLog	2012-11-12 13:21:28 UTC (rev 134215)
+++ trunk/Source/WebKit/chromium/ChangeLog	2012-11-12 13:23:34 UTC (rev 134216)
@@ -1,3 +1,16 @@
+2012-11-12  Keishi Hattori  <kei...@webkit.org>
+
+        Add support for week/month to Locale::formatDateTime()
+        https://bugs.webkit.org/show_bug.cgi?id=101878
+
+        Reviewed by Kent Tamura.
+
+        * tests/LocaleMacTest.cpp:
+        (LocaleMacTest::formatWeek): Takes ISO string and returns localized string.
+        (LocaleMacTest):
+        (LocaleMacTest::formatMonth): Ditto.
+        (TEST_F):
+
 2012-11-11  Sadrul Habib Chowdhury  <sad...@chromium.org>
 
         [chromium] Expose CustomEvent through WebDOMCustomEvent.

Modified: trunk/Source/WebKit/chromium/tests/LocaleMacTest.cpp (134215 => 134216)


--- trunk/Source/WebKit/chromium/tests/LocaleMacTest.cpp	2012-11-12 13:21:28 UTC (rev 134215)
+++ trunk/Source/WebKit/chromium/tests/LocaleMacTest.cpp	2012-11-12 13:23:34 UTC (rev 134216)
@@ -69,6 +69,24 @@
         return dateToDaysFrom1970(year, month, day) * msPerDay;
     }
 
+    String formatWeek(const String& localeString, const String& isoString)
+    {
+        OwnPtr<LocaleMac> locale = LocaleMac::create(localeString);
+        DateComponents date;
+        unsigned end;
+        date.parseWeek(isoString.characters(), isoString.length(), 0, end);
+        return locale->formatDateTime(date);
+    }
+
+    String formatMonth(const String& localeString, const String& isoString)
+    {
+        OwnPtr<LocaleMac> locale = LocaleMac::create(localeString);
+        DateComponents date;
+        unsigned end;
+        date.parseMonth(isoString.characters(), isoString.length(), 0, end);
+        return locale->formatDateTime(date);
+    }
+
     String formatDate(const String& localeString, int year, int month, int day)
     {
         OwnPtr<LocaleMac> locale = LocaleMac::create(localeString);
@@ -158,6 +176,19 @@
 #endif
 };
 
+TEST_F(LocaleMacTest, formatWeek)
+{
+    EXPECT_STREQ("Week 04, 2005", formatWeek("en_US", "2005-W04").utf8().data());
+    EXPECT_STREQ("Week 52, 2005", formatWeek("en_US", "2005-W52").utf8().data());
+}
+
+TEST_F(LocaleMacTest, formatMonth)
+{
+    EXPECT_STREQ("April 2005", formatMonth("en_US", "2005-04").utf8().data());
+    EXPECT_STREQ("avril 2005", formatMonth("fr_FR", "2005-04").utf8().data());
+    EXPECT_STREQ("2005\xE5\xB9\xB4" "04\xE6\x9C\x88", formatMonth("ja_JP", "2005-04").utf8().data());
+}
+
 TEST_F(LocaleMacTest, formatDate)
 {
     EXPECT_STREQ("04/27/2005", formatDate("en_US", 2005, April, 27).utf8().data());
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to