Changeset 134216 in webkit


Ignore:
Timestamp:
Nov 12, 2012 5:23:34 AM (11 years ago)
Author:
keishi@webkit.org
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):

Location:
trunk/Source
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r134214 r134216  
     12012-11-12  Keishi Hattori  <keishi@webkit.org>
     2
     3        Add support for week/month to Locale::formatDateTime()
     4        https://bugs.webkit.org/show_bug.cgi?id=101878
     5
     6        Reviewed by Kent Tamura.
     7
     8        Adding support for week/month to Locale::formatDateTime() in preparation for datalist support for <input type=week/month>.
     9
     10        Added Chromium tests LocaleMacTest.formatWeek and LocaleMacTest.formatMonth.
     11
     12        * platform/text/PlatformLocale.cpp:
     13        (WebCore::DateTimeStringBuilder::visitField):
     14        (WebCore::Locale::formatDateTime): Support week and month types.
     15
    1162012-11-12  Allan Sandfeld Jensen  <allan.jensen@digia.com>
    217
  • trunk/Source/WebCore/platform/text/PlatformLocale.cpp

    r133284 r134216  
    3333
    3434#include "DateTimeFormat.h"
     35#include "LocalizedStrings.h"
    3536#include <wtf/text/StringBuilder.h>
    3637
     
    99100        appendNumber(m_date.fullYear(), 4);
    100101        return;
    101     case DateTimeFormat::FieldTypeMonth:
    102         // Always use padding width of 2 so it matches DateTimeEditElement.
    103         appendNumber(m_date.month() + 1, 2);
     102    case DateTimeFormat::FieldTypeMonth:   
     103        if (numberOfPatternCharacters == 3)
     104            m_builder.append(m_localizer.shortMonthLabels()[m_date.month()]);
     105        else if (numberOfPatternCharacters == 4)
     106            m_builder.append(m_localizer.monthLabels()[m_date.month()]);
     107        else {
     108            // Always use padding width of 2 so it matches DateTimeEditElement.
     109            appendNumber(m_date.month() + 1, 2);
     110        }
     111        return;
     112    case DateTimeFormat::FieldTypeMonthStandAlone:
     113        if (numberOfPatternCharacters == 3)
     114            m_builder.append(m_localizer.shortStandAloneMonthLabels()[m_date.month()]);
     115        else if (numberOfPatternCharacters == 4)
     116            m_builder.append(m_localizer.standAloneMonthLabels()[m_date.month()]);
     117        else {
     118            // Always use padding width of 2 so it matches DateTimeEditElement.
     119            appendNumber(m_date.month() + 1, 2);
     120        }
    104121        return;
    105122    case DateTimeFormat::FieldTypeDayOfMonth:
    106123        // Always use padding width of 2 so it matches DateTimeEditElement.
    107124        appendNumber(m_date.monthDay(), 2);
     125        return;
     126    case DateTimeFormat::FieldTypeWeekOfYear:
     127        // Always use padding width of 2 so it matches DateTimeEditElement.
     128        appendNumber(m_date.week(), 2);
    108129        return;
    109130    case DateTimeFormat::FieldTypePeriod:
     
    346367String Locale::formatDateTime(const DateComponents& date, FormatType formatType)
    347368{
    348     if (date.type() != DateComponents::Time && date.type() != DateComponents::Date)
     369    if (date.type() == DateComponents::DateTime || date.type() == DateComponents::DateTimeLocal || date.type() == DateComponents::Invalid)
    349370        return String();
    350371    // FIXME: Supports all types.
    351372
    352373    DateTimeStringBuilder builder(*this, date);
    353     if (date.type() == DateComponents::Time)
     374    switch (date.type()) {
     375    case DateComponents::Time:
    354376        builder.build(formatType == FormatTypeShort ? shortTimeFormat() : timeFormat());
    355     else if (date.type() == DateComponents::Date)
     377        break;
     378    case DateComponents::Date:
    356379        builder.build(dateFormat());
     380        break;
     381    case DateComponents::Week:
     382        builder.build(weekFormatInLDML());
     383        break;
     384    case DateComponents::Month:
     385        builder.build(monthFormat());
     386        break;
     387    case DateComponents::Invalid:
     388    case DateComponents::DateTime:
     389    case DateComponents::DateTimeLocal:
     390        ASSERT_NOT_REACHED();
     391        break;
     392    }
    357393    return builder.toString();
    358394}
  • trunk/Source/WebKit/chromium/ChangeLog

    r134177 r134216  
     12012-11-12  Keishi Hattori  <keishi@webkit.org>
     2
     3        Add support for week/month to Locale::formatDateTime()
     4        https://bugs.webkit.org/show_bug.cgi?id=101878
     5
     6        Reviewed by Kent Tamura.
     7
     8        * tests/LocaleMacTest.cpp:
     9        (LocaleMacTest::formatWeek): Takes ISO string and returns localized string.
     10        (LocaleMacTest):
     11        (LocaleMacTest::formatMonth): Ditto.
     12        (TEST_F):
     13
    1142012-11-11  Sadrul Habib Chowdhury  <sadrul@chromium.org>
    215
  • trunk/Source/WebKit/chromium/tests/LocaleMacTest.cpp

    r133134 r134216  
    7070    }
    7171
     72    String formatWeek(const String& localeString, const String& isoString)
     73    {
     74        OwnPtr<LocaleMac> locale = LocaleMac::create(localeString);
     75        DateComponents date;
     76        unsigned end;
     77        date.parseWeek(isoString.characters(), isoString.length(), 0, end);
     78        return locale->formatDateTime(date);
     79    }
     80
     81    String formatMonth(const String& localeString, const String& isoString)
     82    {
     83        OwnPtr<LocaleMac> locale = LocaleMac::create(localeString);
     84        DateComponents date;
     85        unsigned end;
     86        date.parseMonth(isoString.characters(), isoString.length(), 0, end);
     87        return locale->formatDateTime(date);
     88    }
     89
    7290    String formatDate(const String& localeString, int year, int month, int day)
    7391    {
     
    158176#endif
    159177};
     178
     179TEST_F(LocaleMacTest, formatWeek)
     180{
     181    EXPECT_STREQ("Week 04, 2005", formatWeek("en_US", "2005-W04").utf8().data());
     182    EXPECT_STREQ("Week 52, 2005", formatWeek("en_US", "2005-W52").utf8().data());
     183}
     184
     185TEST_F(LocaleMacTest, formatMonth)
     186{
     187    EXPECT_STREQ("April 2005", formatMonth("en_US", "2005-04").utf8().data());
     188    EXPECT_STREQ("avril 2005", formatMonth("fr_FR", "2005-04").utf8().data());
     189    EXPECT_STREQ("2005\xE5\xB9\xB4" "04\xE6\x9C\x88", formatMonth("ja_JP", "2005-04").utf8().data());
     190}
    160191
    161192TEST_F(LocaleMacTest, formatDate)
Note: See TracChangeset for help on using the changeset viewer.