Changeset 122184 in webkit


Ignore:
Timestamp:
Jul 9, 2012 7:16:27 PM (12 years ago)
Author:
yosin@chromium.org
Message:

[Platform] Introduce LocaleMac class
https://bugs.webkit.org/show_bug.cgi?id=90248

Reviewed by Kent Tamura.

Source/WebCore:

This patch moves implementation of localized date format related
functions into LocaleMac class as of LocaleWin class for ease of
maintain and using specific locale in testing rather than system
default locale.

Tests: WebKit/chromium/tests/LocaleMacTest.cpp

  • WebCore.gyp/WebCore.gyp: Include LocaleMac.mm
  • WebCore.gypi:
  • platform/text/mac/LocaleMac.h: Replace LocaleWin to LocaleMac.
  • platform/text/mac/LocaleMac.mm:

(WebCore::LocaleMac::LocaleMac):
(WebCore::LocaleMac::~LocaleMac):
(WebCore::LocaleMac::create):
(WebCore::LocaleMac::currentLocale):
(WebCore::LocaleMac::createShortDateFormatter):
(WebCore::LocaleMac::parseDate):
(WebCore::LocaleMac::formatDate):
(WebCore::LocaleMac::dateFormatText):
(WebCore::LocaleMac::monthLabels):
(WebCore::LocaleMac::weekDayShortLabels):
(WebCore::LocaleMac::firstDayOfWeek):

  • platform/text/mac/LocalizedDateMac.cpp:

(WebCore::parseLocalizedDate):
(WebCore::formatLocalizedDate):
(WebCore::localizedDateFormatText):
(WebCore::monthLabels):
(WebCore::weekDayShortLabels):
(WebCore::firstDayOfWeek):

  • platform/text/mac/LocalizedDateMac.mm: Removed.

Source/WebKit/chromium:

This patch added tests for LocaleMac class.

  • WebKit.gypi:
  • tests/LocaleMacTest.cpp: Added.

(LocaleMacTest):
(LocaleMacTest::dateComponents):
(LocaleMacTest::msForDate):
(LocaleMacTest::formatDate):
(LocaleMacTest::parseDate):
(LocaleMacTest::dateFormatText):
(LocaleMacTest::firstDayOfWeek):
(LocaleMacTest::monthLabel):
(LocaleMacTest::weekDayShortLabel):
(TEST_F):

Location:
trunk/Source
Files:
1 added
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r122180 r122184  
     12012-07-09  Yoshifumi Inoue  <yosin@chromium.org>
     2
     3        [Platform] Introduce LocaleMac class
     4        https://bugs.webkit.org/show_bug.cgi?id=90248
     5
     6        Reviewed by Kent Tamura.
     7
     8        This patch moves implementation of localized date format related
     9        functions into LocaleMac class as of LocaleWin class for ease of
     10        maintain and using specific locale in testing rather than system
     11        default locale.
     12
     13        Tests: WebKit/chromium/tests/LocaleMacTest.cpp
     14
     15        * WebCore.gyp/WebCore.gyp: Include LocaleMac.mm
     16        * WebCore.gypi:
     17        * platform/text/mac/LocaleMac.h: Replace LocaleWin to LocaleMac.
     18        * platform/text/mac/LocaleMac.mm:
     19        (WebCore::LocaleMac::LocaleMac):
     20        (WebCore::LocaleMac::~LocaleMac):
     21        (WebCore::LocaleMac::create):
     22        (WebCore::LocaleMac::currentLocale):
     23        (WebCore::LocaleMac::createShortDateFormatter):
     24        (WebCore::LocaleMac::parseDate):
     25        (WebCore::LocaleMac::formatDate):
     26        (WebCore::LocaleMac::dateFormatText):
     27        (WebCore::LocaleMac::monthLabels):
     28        (WebCore::LocaleMac::weekDayShortLabels):
     29        (WebCore::LocaleMac::firstDayOfWeek):
     30        * platform/text/mac/LocalizedDateMac.cpp:
     31        (WebCore::parseLocalizedDate):
     32        (WebCore::formatLocalizedDate):
     33        (WebCore::localizedDateFormatText):
     34        (WebCore::monthLabels):
     35        (WebCore::weekDayShortLabels):
     36        (WebCore::firstDayOfWeek):
     37        * platform/text/mac/LocalizedDateMac.mm: Removed.
     38
    1392012-07-09  Dana Jansens  <danakj@chromium.org>
    240
  • trunk/Source/WebCore/WebCore.gyp/WebCore.gyp

    r121882 r122184  
    16491649            ['include', 'WebKit/mac/WebCoreSupport/WebSystemInterface\\.mm$'],
    16501650
    1651             # We use LocalizedDateMac.mm instead of LocalizedDateICU.cpp.
     1651            # We use LocalizedDateMac.cpp with LocaleMac.mm instead of LocalizedDateICU.cpp.
    16521652            ['exclude', 'platform/text/LocalizedDateICU\\.cpp$'],
    1653             ['include', 'platform/text/mac/LocalizedDateMac\\.mm$'],
     1653            ['include', 'platform/text/mac/LocaleMac\\.mm$'],
     1654            ['include', 'platform/text/mac/LocalizedDateMac\\.cpp$'],
    16541655
    16551656            # The Mac uses platform/mac/KillRingMac.mm instead of the dummy
  • trunk/Source/WebCore/WebCore.gypi

    r122180 r122184  
    44374437            'platform/text/gtk/TextCodecGtk.h',
    44384438            'platform/text/mac/HyphenationMac.mm',
    4439             'platform/text/mac/LocalizedDateMac.mm',
     4439            'platform/text/mac/LocaleMac.h',
     4440            'platform/text/mac/LocaleMac.mm',
     4441            'platform/text/mac/LocalizedDateMac.cpp',
    44404442            'platform/text/mac/LocalizedNumberMac.mm',
    44414443            'platform/text/mac/ShapeArabic.c',
  • trunk/Source/WebCore/platform/text/mac/LocaleMac.h

    r122091 r122184  
    2929 */
    3030
    31 #ifndef LocaleWin_h
    32 #define LocaleWin_h
     31#ifndef LocaleMac_h
     32#define LocaleMac_h
    3333
    34 #include <windows.h>
    3534#include <wtf/Forward.h>
     35#include <wtf/RetainPtr.h>
    3636#include <wtf/Vector.h>
     37#include <wtf/text/WTFString.h>
     38
     39OBJC_CLASS NSDateFormatter;
     40OBJC_CLASS NSLocale;
    3741
    3842namespace WebCore {
    3943
    4044class DateComponents;
    41 struct DateFormatToken;
    4245
    43 class LocaleWin {
     46class LocaleMac {
    4447public:
    45     static PassOwnPtr<LocaleWin> create(LCID);
    46     static LocaleWin* currentLocale();
    47     ~LocaleWin();
     48    static PassOwnPtr<LocaleMac> create(const String&);
     49    static LocaleMac* currentLocale();
     50    ~LocaleMac();
    4851    double parseDate(const String&);
    4952    String formatDate(const DateComponents&);
     53
    5054#if ENABLE(CALENDAR_PICKER)
    5155    String dateFormatText();
    5256    const Vector<String>& monthLabels();
    5357    const Vector<String>& weekDayShortLabels();
    54     unsigned firstDayOfWeek() { return m_firstDayOfWeek; }
     58    unsigned firstDayOfWeek();
    5559#endif
    5660
    57     // For testing.
    58     double parseDate(const String& format, int baseYear, const String& input);
    59     String formatDate(const String& format, int baseYear, int year, int month, int day);
    60     static String dateFormatText(const String& format, const String& yearText, const String& monthText, const String& dayText);
     61private:
     62    explicit LocaleMac(const String&);
     63    NSDateFormatter *createShortDateFormatter();
    6164
    62 private:
    63     explicit LocaleWin(LCID);
    64     String getLocaleInfoString(LCTYPE);
    65     void ensureShortMonthLabels();
    66     void ensureMonthLabels();
    67     void ensureShortDateTokens();
    68     int parseNumberOrMonth(const String&, unsigned& index);
    69     double parseDate(const Vector<DateFormatToken>&, int baseYear, const String&);
    70     String formatDate(const Vector<DateFormatToken>&, int baseYear, int year, int month, int day);
     65    RetainPtr<NSLocale> m_locale;
    7166#if ENABLE(CALENDAR_PICKER)
    72     void ensureWeekDayShortLabels();
     67    String m_localizedDateFormatText;
     68    Vector<String> m_monthLabels;
     69    Vector<String> m_weekDayShortLabels;
    7370#endif
    74 
    75     LCID m_lcid;
    76     int m_baseYear;
    77     Vector<DateFormatToken> m_shortDateTokens;
    78     Vector<String> m_shortMonthLabels;
    79     Vector<String> m_monthLabels;
    80 #if ENABLE(CALENDAR_PICKER)
    81     Vector<String> m_weekDayShortLabels;
    82     unsigned m_firstDayOfWeek;
    83 #endif
    84 
    8571};
    8672
  • trunk/Source/WebCore/platform/text/mac/LocaleMac.mm

    r122091 r122184  
    3030
    3131#include "config.h"
    32 #include "LocalizedDate.h"
     32#include "LocaleMac.h"
    3333
    3434#import <Foundation/NSDateFormatter.h>
     35#import <Foundation/NSLocale.h>
     36#include "LocalizedDate.h"
    3537#include "LocalizedStrings.h"
    36 #include <limits>
    3738#include <wtf/DateMath.h>
     39#include <wtf/PassOwnPtr.h>
    3840#include <wtf/RetainPtr.h>
    3941#include <wtf/text/StringBuilder.h>
     
    4345namespace WebCore {
    4446
    45 static NSDateFormatter *createShortDateFormatter()
     47LocaleMac::LocaleMac(const String& localeIdentifier)
     48    : m_locale([[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier])
     49{
     50}
     51
     52LocaleMac::~LocaleMac()
     53{
     54}
     55
     56PassOwnPtr<LocaleMac> LocaleMac::create(const String& localeIdentifier)
     57{
     58    return adoptPtr(new LocaleMac(localeIdentifier));
     59}
     60
     61LocaleMac* LocaleMac::currentLocale()
     62{
     63    static LocaleMac* currentLocale = LocaleMac::create([NSLocale currentLocale]).leakPtr();
     64    return currentLocale;
     65}
     66
     67NSDateFormatter* LocaleMac::createShortDateFormatter()
    4668{
    4769    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     70    [formatter setLocale:m_locale.get()];
    4871    [formatter setDateStyle:NSDateFormatterShortStyle];
    4972    [formatter setTimeStyle:NSDateFormatterNoStyle];
     
    5376}
    5477
    55 double parseLocalizedDate(const String& input, DateComponents::Type type)
     78double LocaleMac::parseDate(const String& input)
    5679{
    57     switch (type) {
    58     case DateComponents::Date: {
    59         RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
    60         NSDate *date = [formatter.get() dateFromString:input];
    61         if (!date)
    62             break;
    63         return [date timeIntervalSince1970] * msPerSecond;
    64     }
    65     case DateComponents::DateTime:
    66     case DateComponents::DateTimeLocal:
    67     case DateComponents::Month:
    68     case DateComponents::Time:
    69     case DateComponents::Week:
    70     case DateComponents::Invalid:
    71         break;
    72     }
    73     return numeric_limits<double>::quiet_NaN();
     80    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
     81    NSDate *date = [formatter.get() dateFromString:input];
     82    if (!date)
     83        return std::numeric_limits<double>::quiet_NaN();
     84    return [date timeIntervalSince1970] * msPerSecond;
    7485}
    7586
    76 String formatLocalizedDate(const DateComponents& dateComponents)
     87String LocaleMac::formatDate(const DateComponents& dateComponents)
    7788{
    78     switch (dateComponents.type()) {
    79     case DateComponents::Date: {
    80         RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
    81         NSTimeInterval interval = dateComponents.millisecondsSinceEpoch() / msPerSecond;
    82         return String([formatter.get() stringFromDate:[NSDate dateWithTimeIntervalSince1970:interval]]);
    83     }
    84     case DateComponents::DateTime:
    85     case DateComponents::DateTimeLocal:
    86     case DateComponents::Month:
    87     case DateComponents::Time:
    88     case DateComponents::Week:
    89     case DateComponents::Invalid:
    90         break;
    91     }
    92     return String();
     89    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
     90    NSTimeInterval interval = dateComponents.millisecondsSinceEpoch() / msPerSecond;
     91    return String([formatter.get() stringFromDate:[NSDate dateWithTimeIntervalSince1970:interval]]);
    9392}
    9493
     
    141140}
    142141
    143 String localizedDateFormatText()
     142String LocaleMac::dateFormatText()
    144143{
    145     DEFINE_STATIC_LOCAL(String, text, ());
    146     if (!text.isEmpty())
    147         return text;
     144    if (!m_localizedDateFormatText.isEmpty())
     145        return m_localizedDateFormatText;
    148146    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
    149     text = localizeDateFormat(String([formatter.get() dateFormat]));
    150     return text;
     147    m_localizedDateFormatText = localizeDateFormat(String([formatter.get() dateFormat]));
     148    return  m_localizedDateFormatText;
    151149}
    152150
    153 const Vector<String>& monthLabels()
     151const Vector<String>& LocaleMac::monthLabels()
    154152{
    155     DEFINE_STATIC_LOCAL(Vector<String>, labels, ());
    156     if (!labels.isEmpty())
    157         return labels;
    158     labels.reserveCapacity(12);
     153    if (!m_monthLabels.isEmpty())
     154        return m_monthLabels;
     155    m_monthLabels.reserveCapacity(12);
    159156    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
    160157    NSArray *array = [formatter.get() monthSymbols];
    161158    if ([array count] == 12) {
    162159        for (unsigned i = 0; i < 12; ++i)
    163             labels.append(String([array objectAtIndex:i]));
    164         return labels;
     160            m_monthLabels.append(String([array objectAtIndex:i]));
     161        return m_monthLabels;
    165162    }
    166163    for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthFullName); ++i)
    167         labels.append(WTF::monthFullName[i]);
    168     return labels;
     164        m_monthLabels.append(WTF::monthFullName[i]);
     165    return m_monthLabels;
    169166}
    170167
    171 const Vector<String>& weekDayShortLabels()
     168const Vector<String>& LocaleMac::weekDayShortLabels()
    172169{
    173     DEFINE_STATIC_LOCAL(Vector<String>, labels, ());
    174     if (!labels.isEmpty())
    175         return labels;
    176     labels.reserveCapacity(7);
     170    if (!m_weekDayShortLabels.isEmpty())
     171        return m_weekDayShortLabels;
     172    m_weekDayShortLabels.reserveCapacity(7);
    177173    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
    178174    NSArray *array = [formatter.get() shortWeekdaySymbols];
    179175    if ([array count] == 7) {
    180176        for (unsigned i = 0; i < 7; ++i)
    181             labels.append(String([array objectAtIndex:i]));
    182         return labels;
     177            m_weekDayShortLabels.append(String([array objectAtIndex:i]));
     178        return m_weekDayShortLabels;
    183179    }
    184180    for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::weekdayName); ++i) {
    185181        // weekdayName starts with Monday.
    186         labels.append(WTF::weekdayName[(i + 6) % 7]);
     182        m_weekDayShortLabels.append(WTF::weekdayName[(i + 6) % 7]);
    187183    }
    188     return labels;
     184    return m_weekDayShortLabels;
    189185}
    190186
    191 unsigned firstDayOfWeek()
     187unsigned LocaleMac::firstDayOfWeek()
    192188{
    193189    RetainPtr<NSCalendar> calendar(AdoptNS, [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]);
     190    [calendar.get() setLocale:m_locale.get()];
    194191    // The document for NSCalendar - firstWeekday doesn't have an explanation of
    195192    // firstWeekday value. We can guess it by the document of NSDateComponents -
     
    198195}
    199196#endif
    200 
    201197}
  • trunk/Source/WebCore/platform/text/mac/LocalizedDateMac.cpp

    r122091 r122184  
    3232#include "LocalizedDate.h"
    3333
    34 #include "LocaleWin.h"
     34#include "LocaleMac.h"
    3535#include <limits>
    3636
     
    4343    switch (type) {
    4444    case DateComponents::Date:
    45         return LocaleWin::currentLocale()->parseDate(input);
     45        return LocaleMac::currentLocale()->parseDate(input);
    4646    case DateComponents::DateTime:
    4747    case DateComponents::DateTimeLocal:
     
    5959    switch (dateComponents.type()) {
    6060    case DateComponents::Date:
    61         return LocaleWin::currentLocale()->formatDate(dateComponents);
     61        return LocaleMac::currentLocale()->formatDate(dateComponents);
    6262    case DateComponents::DateTime:
    6363    case DateComponents::DateTimeLocal:
     
    7474String localizedDateFormatText()
    7575{
    76     return LocaleWin::currentLocale()->dateFormatText();
     76    return LocaleMac::currentLocale()->dateFormatText();
    7777}
    7878
    7979const Vector<String>& monthLabels()
    8080{
    81     return LocaleWin::currentLocale()->monthLabels();
     81    return LocaleMac::currentLocale()->monthLabels();
    8282}
    8383
    8484const Vector<String>& weekDayShortLabels()
    8585{
    86     return LocaleWin::currentLocale()->weekDayShortLabels();
     86    return LocaleMac::currentLocale()->weekDayShortLabels();
    8787}
    8888
    8989unsigned firstDayOfWeek()
    9090{
    91     return LocaleWin::currentLocale()->firstDayOfWeek();
     91    return LocaleMac::currentLocale()->firstDayOfWeek();
    9292}
    9393#endif
  • trunk/Source/WebKit/chromium/ChangeLog

    r122180 r122184  
     12012-07-09  Yoshifumi Inoue  <yosin@chromium.org>
     2
     3        [Platform] Introduce LocaleMac class
     4        https://bugs.webkit.org/show_bug.cgi?id=90248
     5
     6        Reviewed by Kent Tamura.
     7
     8        This patch added tests for LocaleMac class.
     9
     10        * WebKit.gypi:
     11        * tests/LocaleMacTest.cpp: Added.
     12        (LocaleMacTest):
     13        (LocaleMacTest::dateComponents):
     14        (LocaleMacTest::msForDate):
     15        (LocaleMacTest::formatDate):
     16        (LocaleMacTest::parseDate):
     17        (LocaleMacTest::dateFormatText):
     18        (LocaleMacTest::firstDayOfWeek):
     19        (LocaleMacTest::monthLabel):
     20        (LocaleMacTest::weekDayShortLabel):
     21        (TEST_F):
     22
    1232012-07-09  Dana Jansens  <danakj@chromium.org>
    224
  • trunk/Source/WebKit/chromium/WebKit.gypi

    r122180 r122184  
    182182                ],
    183183            }],
     184            ['OS=="mac"', {
     185                'webkit_unittest_files': [
     186                    'tests/LocaleMacTest.cpp',
     187                ],
     188            }],
    184189            ['OS!="mac"', {
    185190                'webkit_unittest_files': [
Note: See TracChangeset for help on using the changeset viewer.