Changeset 122303 in webkit


Ignore:
Timestamp:
Jul 10, 2012 11:52:27 PM (12 years ago)
Author:
yosin@chromium.org
Message:

[Chromium-Windows] Implement functions for localized time format information
https://bugs.webkit.org/show_bug.cgi?id=90236

Reviewed by Kent Tamura.

Source/WebCore:

This patch introduces following localized time format related
functions:

  • localizedTimeFormatText
  • localizeShortTimeFormatText()
  • timeAMPMLabels

for Windows in feature flag: ENABLE_INPUT_TYPE_TIME_MULTIPLE_FIELDS.

See also:

ICU version: https://bugs.webkit.org/show_bug.cgi?id=89965
Mac version: https://bugs.webkit.org/show_bug.cgi?id=90237

Tests: WebKit/chromium/tests/LocalWinTest.cpp

  • platform/text/LocaleWin.cpp:

(WebCore::mapCharacterToDateTimeFieldType): Added.
(WebCore::convertWindowsTimeFormatToLDML): Added.
(WebCore::LocaleWin::timeFormatText): Added.
(WebCore::LocaleWin::shortTimeFormatText): Added.
(WebCore::LocaleWin::timeAMPMLabels): Added.

  • platform/text/LocaleWin.h:

(LocaleWin): Added time format related functions and variables.

  • platform/text/LocalizedDateWin.cpp:

(WebCore::localizedTimeFormatText): Added.
(WebCore::localizedShortTimeFormatText): Added.
(WebCore::timeAMPMLabels): Added.

Source/WebKit/chromium:

This patch introduces test cases for date and time format related
functions in LocaleWin.

  • tests/LocaleWinTest.cpp:

(LocaleWinTest):
(LocaleWinTest::dateComponents): Added.
(LocaleWinTest::msForDate): Moved inside class LocaleWinTest.
(LocaleWinTest::formatDate): Added.
(LocaleWinTest::parseDate): Added.
(LocaleWinTest::dateFormatText): Added.
(LocaleWinTest::firstDayOfWeek): Added.
(LocaleWinTest::monthLabel): Added.
(LocaleWinTest::weekDayShortLabel): Added.
(LocaleWinTest::timeFormatText): Added.
(LocaleWinTest::shortTimeFormatText): Added.
(LocaleWinTest::timeAMPMLabel): Added.
(TEST_F):

Location:
trunk/Source
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r122302 r122303  
     12012-07-10  Yoshifumi Inoue  <yosin@chromium.org>
     2
     3        [Chromium-Windows] Implement functions for localized time format information
     4        https://bugs.webkit.org/show_bug.cgi?id=90236
     5
     6        Reviewed by Kent Tamura.
     7
     8        This patch introduces following localized time format related
     9        functions:
     10            - localizedTimeFormatText
     11            - localizeShortTimeFormatText()
     12            - timeAMPMLabels
     13        for Windows in feature flag: ENABLE_INPUT_TYPE_TIME_MULTIPLE_FIELDS.
     14
     15        See also:
     16          ICU version: https://bugs.webkit.org/show_bug.cgi?id=89965
     17          Mac version: https://bugs.webkit.org/show_bug.cgi?id=90237
     18
     19        Tests: WebKit/chromium/tests/LocalWinTest.cpp
     20
     21        * platform/text/LocaleWin.cpp:
     22        (WebCore::mapCharacterToDateTimeFieldType): Added.
     23        (WebCore::convertWindowsTimeFormatToLDML): Added.
     24        (WebCore::LocaleWin::timeFormatText): Added.
     25        (WebCore::LocaleWin::shortTimeFormatText): Added.
     26        (WebCore::LocaleWin::timeAMPMLabels): Added.
     27        * platform/text/LocaleWin.h:
     28        (LocaleWin): Added time format related functions and variables.
     29        * platform/text/LocalizedDateWin.cpp:
     30        (WebCore::localizedTimeFormatText): Added.
     31        (WebCore::localizedShortTimeFormatText):  Added.
     32        (WebCore::timeAMPMLabels):  Added.
     33
    1342012-07-10  Douglas Stockwell  <dstockwell@chromium.org>
    235
  • trunk/Source/WebCore/platform/text/LocaleWin.cpp

    r115713 r122303  
    3333
    3434#include "DateComponents.h"
     35#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
     36#include "DateTimeFormat.h"
     37#endif
    3538#include "LocalizedStrings.h"
    3639#include <limits>
     
    591594#endif
    592595
    593 }
     596#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
     597static DateTimeFormat::FieldType mapCharacterToDateTimeFieldType(UChar ch)
     598{
     599    switch (ch) {
     600    case 'h':
     601        return DateTimeFormat::FieldTypeHour12;
     602
     603    case 'H':
     604        return DateTimeFormat::FieldTypeHour23;
     605
     606    case 'm':
     607        return DateTimeFormat::FieldTypeMinute;
     608
     609    case 's':
     610        return DateTimeFormat::FieldTypeSecond;
     611
     612    case 't':
     613        return DateTimeFormat::FieldTypePeriod;
     614
     615    default:
     616        return DateTimeFormat::FieldTypeLiteral;
     617    }
     618}
     619
     620// This class used for converting Windows time pattern format[1] into LDML[2]
     621// time format string.
     622// [1] http://msdn.microsoft.com/en-us/library/windows/desktop/dd318148(v=vs.85).aspx
     623// [2] LDML http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
     624static String convertWindowsTimeFormatToLDML(const String& windowsTimeFormat)
     625{
     626    StringBuilder builder;
     627    int counter = 0;
     628    DateTimeFormat::FieldType lastFieldType = DateTimeFormat::FieldTypeLiteral;
     629    for (unsigned index = 0; index < windowsTimeFormat.length(); ++index) {
     630        UChar const ch = windowsTimeFormat[index];
     631        DateTimeFormat::FieldType fieldType = mapCharacterToDateTimeFieldType(ch);
     632        if (fieldType == DateTimeFormat::FieldTypeLiteral)
     633            builder.append(ch);
     634        else if (fieldType == lastFieldType) {
     635            ++counter;
     636            if (counter == 2 && lastFieldType != DateTimeFormat::FieldTypePeriod)
     637                builder.append(static_cast<UChar>(lastFieldType));
     638        } else {
     639            if (lastFieldType != DateTimeFormat::FieldTypeLiteral)
     640                builder.append(static_cast<UChar>(lastFieldType));
     641            builder.append(static_cast<UChar>(fieldType));
     642            counter = 1;
     643        }
     644        lastFieldType = fieldType;
     645    }
     646    return builder.toString();
     647}
     648
     649String LocaleWin::timeFormatText()
     650{
     651    if (m_timeFormatText.isEmpty())
     652        m_timeFormatText = convertWindowsTimeFormatToLDML(getLocaleInfoString(LOCALE_STIMEFORMAT));
     653    return m_timeFormatText;
     654}
     655
     656// Note: To make XP/Vista and Windows 7/later same behavior, we don't use
     657// LOCALE_SSHORTTIME.
     658String LocaleWin::shortTimeFormatText()
     659{
     660    return timeFormatText();
     661}
     662
     663const Vector<String>& LocaleWin::timeAMPMLabels()
     664{
     665    if (m_timeAMPMLabels.isEmpty()) {
     666        m_timeAMPMLabels.append(getLocaleInfoString(LOCALE_S1159));
     667        m_timeAMPMLabels.append(getLocaleInfoString(LOCALE_S2359));
     668    }
     669    return m_timeAMPMLabels;
     670}
     671#endif
     672
     673}
  • trunk/Source/WebCore/platform/text/LocaleWin.h

    r115713 r122303  
    3535#include <wtf/Forward.h>
    3636#include <wtf/Vector.h>
     37#include <wtf/text/WTFString.h>
    3738
    3839namespace WebCore {
     
    5354    const Vector<String>& weekDayShortLabels();
    5455    unsigned firstDayOfWeek() { return m_firstDayOfWeek; }
     56#endif
     57
     58#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
     59    String timeFormatText();
     60    String shortTimeFormatText();
     61    const Vector<String>& timeAMPMLabels();
    5562#endif
    5663
     
    8289    unsigned m_firstDayOfWeek;
    8390#endif
    84 
     91#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
     92    String m_timeFormatText;
     93    Vector<String> m_timeAMPMLabels;
     94#endif
    8595};
    8696
  • trunk/Source/WebCore/platform/text/LocalizedDateWin.cpp

    r115713 r122303  
    9393#endif
    9494
     95#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
     96String localizedTimeFormatText()
     97{
     98    return LocaleWin::currentLocale()->timeFormatText();
    9599}
     100
     101String localizedShortTimeFormatText()
     102{
     103    return LocaleWin::currentLocale()->shortTimeFormatText();
     104}
     105
     106const Vector<String>& timeAMPMLabels()
     107{
     108    return LocaleWin::currentLocale()->timeAMPMLabels();
     109}
     110#endif
     111
     112}
  • trunk/Source/WebKit/chromium/ChangeLog

    r122292 r122303  
     12012-07-10  Yoshifumi Inoue  <yosin@chromium.org>
     2
     3        [Chromium-Windows] Implement functions for localized time format information
     4        https://bugs.webkit.org/show_bug.cgi?id=90236
     5
     6        Reviewed by Kent Tamura.
     7
     8        This patch introduces test cases for date and time format related
     9        functions in LocaleWin.
     10
     11        * tests/LocaleWinTest.cpp:
     12        (LocaleWinTest):
     13        (LocaleWinTest::dateComponents): Added.
     14        (LocaleWinTest::msForDate): Moved inside class LocaleWinTest.
     15        (LocaleWinTest::formatDate): Added.
     16        (LocaleWinTest::parseDate): Added.
     17        (LocaleWinTest::dateFormatText): Added.
     18        (LocaleWinTest::firstDayOfWeek): Added.
     19        (LocaleWinTest::monthLabel): Added.
     20        (LocaleWinTest::weekDayShortLabel): Added.
     21        (LocaleWinTest::timeFormatText): Added.
     22        (LocaleWinTest::shortTimeFormatText): Added.
     23        (LocaleWinTest::timeAMPMLabel): Added.
     24        (TEST_F):
     25
    1262012-07-10  Adam Barth  <abarth@webkit.org>
    227
  • trunk/Source/WebKit/chromium/tests/LocaleWinTest.cpp

    r115713 r122303  
    3232#include "LocaleWin.h"
    3333
     34#include "DateComponents.h"
    3435#include <gtest/gtest.h>
    3536#include <wtf/DateMath.h>
     
    4142using namespace std;
    4243
    43 enum {
    44     January = 0, February, March,
    45     April, May, June,
    46     July, August, September,
    47     October, November, December,
     44class LocaleWinTest : public ::testing::Test {
     45protected:
     46    enum {
     47        January = 0, February, March,
     48        April, May, June,
     49        July, August, September,
     50        October, November, December,
     51    };
     52
     53    enum {
     54        Sunday = 0, Monday, Tuesday,
     55        Wednesday, Thursday, Friday,
     56        Saturday,
     57    };
     58
     59    // See http://msdn.microsoft.com/en-us/goglobal/bb964664.aspx
     60    enum {
     61        EnglishUS = 0x409,
     62        FrenchFR = 0x40C,
     63        JapaneseJP = 0x411,
     64    };
     65
     66    DateComponents dateComponents(int year, int month, int day)
     67    {
     68        DateComponents date;
     69        date.setMillisecondsSinceEpochForDate(msForDate(year, month, day));
     70        return date;
     71    }
     72
     73    double msForDate(int year, int month, int day)
     74    {
     75        return dateToDaysFrom1970(year, month, day) * msPerDay;
     76    }
     77
     78    String formatDate(LCID lcid, int year, int month, int day)
     79    {
     80        OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
     81        return locale->formatDate(dateComponents(year, month, day));
     82    }
     83
     84    double parseDate(LCID lcid, const String& dateString)
     85    {
     86        OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
     87        return locale->parseDate(dateString);
     88    }
     89
     90#if ENABLE(CALENDAR_PICKER)
     91    String dateFormatText(LCID lcid)
     92    {
     93        OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
     94        return locale->dateFormatText();
     95    }
     96
     97    unsigned firstDayOfWeek(LCID lcid)
     98    {
     99        OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
     100        return locale->firstDayOfWeek();
     101    }
     102
     103    String monthLabel(LCID lcid, unsigned index)
     104    {
     105        OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
     106        return locale->monthLabels()[index];
     107    }
     108
     109    String weekDayShortLabel(LCID lcid, unsigned index)
     110    {
     111        OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
     112        return locale->weekDayShortLabels()[index];
     113    }
     114#endif
     115
     116#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
     117    String timeFormatText(LCID lcid)
     118    {
     119        OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
     120        return locale->timeFormatText();
     121    }
     122
     123    String shortTimeFormatText(LCID lcid)
     124    {
     125        OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
     126        return locale->shortTimeFormatText();
     127    }
     128
     129    String timeAMPMLabel(LCID lcid, unsigned index)
     130    {
     131        OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
     132        return locale->timeAMPMLabels()[index];
     133    }
     134#endif
    48135};
    49136
    50 static double msForDate(int year, int month, int day)
    51 {
    52     return dateToDaysFrom1970(year, month, day) * msPerDay;
    53 }
    54 
    55 TEST(LocaleWinTest, TestLocalizedDateFormatText)
     137TEST_F(LocaleWinTest, TestLocalizedDateFormatText)
    56138{
    57139    EXPECT_STREQ("year/month/day", LocaleWin::dateFormatText("y/M/d", "year", "month", "day").utf8().data());
     
    67149}
    68150
    69 TEST(LocaleWinTest, TestFormat)
    70 {
    71     const LCID EnglishUS = 0x0409;
     151TEST_F(LocaleWinTest, TestFormat)
     152{
    72153    OwnPtr<LocaleWin> locale = LocaleWin::create(EnglishUS);
    73154
     
    127208}
    128209
    129 TEST(LocaleWinTest, TestParse)
    130 {
    131     const LCID EnglishUS = 0x0409;
     210TEST_F(LocaleWinTest, TestParse)
     211{
    132212    OwnPtr<LocaleWin> locale = LocaleWin::create(EnglishUS);
    133213
     
    162242    EXPECT_TRUE(isnan(locale->parseDate("MMMM/d/y", 2012, "-1/-1/-1")));
    163243}
     244
     245TEST_F(LocaleWinTest, formatDate)
     246{
     247    EXPECT_STREQ("4/27/2005", formatDate(EnglishUS, 2005, April, 27).utf8().data());
     248    EXPECT_STREQ("27/04/2005", formatDate(FrenchFR, 2005, April, 27).utf8().data());
     249    EXPECT_STREQ("2005/04/27", formatDate(JapaneseJP, 2005, April, 27).utf8().data());
     250}
     251
     252TEST_F(LocaleWinTest, parseDate)
     253{
     254    EXPECT_EQ(msForDate(2005, April, 27), parseDate(EnglishUS, "April/27/2005"));
     255    EXPECT_EQ(msForDate(2005, April, 27), parseDate(FrenchFR, "27/avril/2005"));
     256    EXPECT_EQ(msForDate(2005, April, 27), parseDate(JapaneseJP, "2005/04/27"));
     257}
     258
     259#if ENABLE(CALENDAR_PICKER)
     260TEST_F(LocaleWinTest, dateFormatText)
     261{
     262    EXPECT_STREQ("Month/Day/Year", dateFormatText(EnglishUS).utf8().data());
     263    EXPECT_STREQ("Day/Month/Year", dateFormatText(FrenchFR).utf8().data());
     264    EXPECT_STREQ("Year/Month/Day", dateFormatText(JapaneseJP).utf8().data());
     265}
     266
     267TEST_F(LocaleWinTest, firstDayOfWeek)
     268{
     269    EXPECT_EQ(Sunday, firstDayOfWeek(EnglishUS));
     270    EXPECT_EQ(Monday, firstDayOfWeek(FrenchFR));
     271    EXPECT_EQ(Sunday, firstDayOfWeek(JapaneseJP));
     272}
     273
     274TEST_F(LocaleWinTest, monthLabels)
     275{
     276    EXPECT_STREQ("January", monthLabel(EnglishUS, January).utf8().data());
     277    EXPECT_STREQ("June", monthLabel(EnglishUS, June).utf8().data());
     278    EXPECT_STREQ("December", monthLabel(EnglishUS, December).utf8().data());
     279
     280    EXPECT_STREQ("janvier", monthLabel(FrenchFR, January).utf8().data());
     281    EXPECT_STREQ("juin", monthLabel(FrenchFR, June).utf8().data());
     282    EXPECT_STREQ("d\xC3\xA9" "cembre", monthLabel(FrenchFR, December).utf8().data());
     283
     284    EXPECT_STREQ("1\xE6\x9C\x88", monthLabel(JapaneseJP, January).utf8().data());
     285    EXPECT_STREQ("6\xE6\x9C\x88", monthLabel(JapaneseJP, June).utf8().data());
     286    EXPECT_STREQ("12\xE6\x9C\x88", monthLabel(JapaneseJP, December).utf8().data());
     287}
     288
     289TEST_F(LocaleWinTest, weekDayShortLabels)
     290{
     291    EXPECT_STREQ("Sun", weekDayShortLabel(EnglishUS, Sunday).utf8().data());
     292    EXPECT_STREQ("Wed", weekDayShortLabel(EnglishUS, Wednesday).utf8().data());
     293    EXPECT_STREQ("Sat", weekDayShortLabel(EnglishUS, Saturday).utf8().data());
     294
     295    EXPECT_STREQ("dim.", weekDayShortLabel(FrenchFR, Sunday).utf8().data());
     296    EXPECT_STREQ("mer.", weekDayShortLabel(FrenchFR, Wednesday).utf8().data());
     297    EXPECT_STREQ("sam.", weekDayShortLabel(FrenchFR, Saturday).utf8().data());
     298
     299    EXPECT_STREQ("\xE6\x97\xA5", weekDayShortLabel(JapaneseJP, Sunday).utf8().data());
     300    EXPECT_STREQ("\xE6\xB0\xB4", weekDayShortLabel(JapaneseJP, Wednesday).utf8().data());
     301    EXPECT_STREQ("\xE5\x9C\x9F", weekDayShortLabel(JapaneseJP, Saturday).utf8().data());
     302}
     303#endif
     304
     305#if ENABLE(INPUT_TYPE_TIME_MULTIPLE_FIELDS)
     306TEST_F(LocaleWinTest, timeFormatText)
     307{
     308    EXPECT_STREQ("h:mm:ss a", timeFormatText(EnglishUS).utf8().data());
     309    EXPECT_STREQ("HH:mm:ss", timeFormatText(FrenchFR).utf8().data());
     310    EXPECT_STREQ("H:mm:ss", timeFormatText(JapaneseJP).utf8().data());
     311}
     312
     313TEST_F(LocaleWinTest, shortTimeFormatText)
     314{
     315    EXPECT_STREQ("h:mm:ss a", shortTimeFormatText(EnglishUS).utf8().data());
     316    EXPECT_STREQ("HH:mm:ss", shortTimeFormatText(FrenchFR).utf8().data());
     317    EXPECT_STREQ("H:mm:ss", shortTimeFormatText(JapaneseJP).utf8().data());
     318}
     319
     320TEST_F(LocaleWinTest, timeAMPMLabels)
     321{
     322    EXPECT_STREQ("AM", timeAMPMLabel(EnglishUS, 0).utf8().data());
     323    EXPECT_STREQ("PM", timeAMPMLabel(EnglishUS, 1).utf8().data());
     324
     325    EXPECT_STREQ("", timeAMPMLabel(FrenchFR, 0).utf8().data());
     326    EXPECT_STREQ("", timeAMPMLabel(FrenchFR, 1).utf8().data());
     327
     328    EXPECT_STREQ("\xE5\x8D\x88\xE5\x89\x8D", timeAMPMLabel(JapaneseJP, 0).utf8().data());
     329    EXPECT_STREQ("\xE5\x8D\x88\xE5\xBE\x8C", timeAMPMLabel(JapaneseJP, 1).utf8().data());
     330}
     331#endif
Note: See TracChangeset for help on using the changeset viewer.