Skip to content
Permalink
Browse files
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):


Canonical link: https://commits.webkit.org/119985@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@134216 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
keishi committed Nov 12, 2012
1 parent f374b27 commit 785e3e6741c2b77d81b5e949a00ec24f166dbe18
Showing 4 changed files with 101 additions and 6 deletions.
@@ -1,3 +1,18 @@
2012-11-12 Keishi Hattori <keishi@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.jensen@digia.com>

[Qt] Can not load MHTML documents
@@ -32,6 +32,7 @@
#include "PlatformLocale.h"

#include "DateTimeFormat.h"
#include "LocalizedStrings.h"
#include <wtf/text/StringBuilder.h>

namespace WebCore {
@@ -98,14 +99,34 @@ void DateTimeStringBuilder::visitField(DateTimeFormat::FieldType fieldType, int
// 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::dateTimeFormatWithoutSeconds()

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
@@ -1,3 +1,16 @@
2012-11-12 Keishi Hattori <keishi@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 <sadrul@chromium.org>

[chromium] Expose CustomEvent through WebDOMCustomEvent.
@@ -69,6 +69,24 @@ class LocaleMacTest : public ::testing::Test {
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 @@ class LocaleMacTest : public ::testing::Test {
#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());

0 comments on commit 785e3e6

Please sign in to comment.