Changeset 122184 in webkit
- Timestamp:
- Jul 9, 2012 7:16:27 PM (12 years ago)
- Location:
- trunk/Source
- Files:
-
- 1 added
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r122180 r122184 1 2012-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 1 39 2012-07-09 Dana Jansens <danakj@chromium.org> 2 40 -
trunk/Source/WebCore/WebCore.gyp/WebCore.gyp
r121882 r122184 1649 1649 ['include', 'WebKit/mac/WebCoreSupport/WebSystemInterface\\.mm$'], 1650 1650 1651 # We use LocalizedDateMac. mm instead of LocalizedDateICU.cpp.1651 # We use LocalizedDateMac.cpp with LocaleMac.mm instead of LocalizedDateICU.cpp. 1652 1652 ['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$'], 1654 1655 1655 1656 # The Mac uses platform/mac/KillRingMac.mm instead of the dummy -
trunk/Source/WebCore/WebCore.gypi
r122180 r122184 4437 4437 'platform/text/gtk/TextCodecGtk.h', 4438 4438 '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', 4440 4442 'platform/text/mac/LocalizedNumberMac.mm', 4441 4443 'platform/text/mac/ShapeArabic.c', -
trunk/Source/WebCore/platform/text/mac/LocaleMac.h
r122091 r122184 29 29 */ 30 30 31 #ifndef Locale Win_h32 #define Locale Win_h31 #ifndef LocaleMac_h 32 #define LocaleMac_h 33 33 34 #include <windows.h>35 34 #include <wtf/Forward.h> 35 #include <wtf/RetainPtr.h> 36 36 #include <wtf/Vector.h> 37 #include <wtf/text/WTFString.h> 38 39 OBJC_CLASS NSDateFormatter; 40 OBJC_CLASS NSLocale; 37 41 38 42 namespace WebCore { 39 43 40 44 class DateComponents; 41 struct DateFormatToken;42 45 43 class Locale Win{46 class LocaleMac { 44 47 public: 45 static PassOwnPtr<Locale Win> create(LCID);46 static Locale Win* currentLocale();47 ~Locale Win();48 static PassOwnPtr<LocaleMac> create(const String&); 49 static LocaleMac* currentLocale(); 50 ~LocaleMac(); 48 51 double parseDate(const String&); 49 52 String formatDate(const DateComponents&); 53 50 54 #if ENABLE(CALENDAR_PICKER) 51 55 String dateFormatText(); 52 56 const Vector<String>& monthLabels(); 53 57 const Vector<String>& weekDayShortLabels(); 54 unsigned firstDayOfWeek() { return m_firstDayOfWeek; }58 unsigned firstDayOfWeek(); 55 59 #endif 56 60 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); 61 private: 62 explicit LocaleMac(const String&); 63 NSDateFormatter *createShortDateFormatter(); 61 64 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; 71 66 #if ENABLE(CALENDAR_PICKER) 72 void ensureWeekDayShortLabels(); 67 String m_localizedDateFormatText; 68 Vector<String> m_monthLabels; 69 Vector<String> m_weekDayShortLabels; 73 70 #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 #endif84 85 71 }; 86 72 -
trunk/Source/WebCore/platform/text/mac/LocaleMac.mm
r122091 r122184 30 30 31 31 #include "config.h" 32 #include "Local izedDate.h"32 #include "LocaleMac.h" 33 33 34 34 #import <Foundation/NSDateFormatter.h> 35 #import <Foundation/NSLocale.h> 36 #include "LocalizedDate.h" 35 37 #include "LocalizedStrings.h" 36 #include <limits>37 38 #include <wtf/DateMath.h> 39 #include <wtf/PassOwnPtr.h> 38 40 #include <wtf/RetainPtr.h> 39 41 #include <wtf/text/StringBuilder.h> … … 43 45 namespace WebCore { 44 46 45 static NSDateFormatter *createShortDateFormatter() 47 LocaleMac::LocaleMac(const String& localeIdentifier) 48 : m_locale([[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier]) 49 { 50 } 51 52 LocaleMac::~LocaleMac() 53 { 54 } 55 56 PassOwnPtr<LocaleMac> LocaleMac::create(const String& localeIdentifier) 57 { 58 return adoptPtr(new LocaleMac(localeIdentifier)); 59 } 60 61 LocaleMac* LocaleMac::currentLocale() 62 { 63 static LocaleMac* currentLocale = LocaleMac::create([NSLocale currentLocale]).leakPtr(); 64 return currentLocale; 65 } 66 67 NSDateFormatter* LocaleMac::createShortDateFormatter() 46 68 { 47 69 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 70 [formatter setLocale:m_locale.get()]; 48 71 [formatter setDateStyle:NSDateFormatterShortStyle]; 49 72 [formatter setTimeStyle:NSDateFormatterNoStyle]; … … 53 76 } 54 77 55 double parseLocalizedDate(const String& input, DateComponents::Type type)78 double LocaleMac::parseDate(const String& input) 56 79 { 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; 74 85 } 75 86 76 String formatLocalizedDate(const DateComponents& dateComponents)87 String LocaleMac::formatDate(const DateComponents& dateComponents) 77 88 { 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]]); 93 92 } 94 93 … … 141 140 } 142 141 143 String localizedDateFormatText()142 String LocaleMac::dateFormatText() 144 143 { 145 DEFINE_STATIC_LOCAL(String, text, ()); 146 if (!text.isEmpty()) 147 return text; 144 if (!m_localizedDateFormatText.isEmpty()) 145 return m_localizedDateFormatText; 148 146 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; 151 149 } 152 150 153 const Vector<String>& monthLabels()151 const Vector<String>& LocaleMac::monthLabels() 154 152 { 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); 159 156 RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter()); 160 157 NSArray *array = [formatter.get() monthSymbols]; 161 158 if ([array count] == 12) { 162 159 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; 165 162 } 166 163 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; 169 166 } 170 167 171 const Vector<String>& weekDayShortLabels()168 const Vector<String>& LocaleMac::weekDayShortLabels() 172 169 { 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); 177 173 RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter()); 178 174 NSArray *array = [formatter.get() shortWeekdaySymbols]; 179 175 if ([array count] == 7) { 180 176 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; 183 179 } 184 180 for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::weekdayName); ++i) { 185 181 // weekdayName starts with Monday. 186 labels.append(WTF::weekdayName[(i + 6) % 7]);182 m_weekDayShortLabels.append(WTF::weekdayName[(i + 6) % 7]); 187 183 } 188 return labels;184 return m_weekDayShortLabels; 189 185 } 190 186 191 unsigned firstDayOfWeek()187 unsigned LocaleMac::firstDayOfWeek() 192 188 { 193 189 RetainPtr<NSCalendar> calendar(AdoptNS, [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]); 190 [calendar.get() setLocale:m_locale.get()]; 194 191 // The document for NSCalendar - firstWeekday doesn't have an explanation of 195 192 // firstWeekday value. We can guess it by the document of NSDateComponents - … … 198 195 } 199 196 #endif 200 201 197 } -
trunk/Source/WebCore/platform/text/mac/LocalizedDateMac.cpp
r122091 r122184 32 32 #include "LocalizedDate.h" 33 33 34 #include "Locale Win.h"34 #include "LocaleMac.h" 35 35 #include <limits> 36 36 … … 43 43 switch (type) { 44 44 case DateComponents::Date: 45 return Locale Win::currentLocale()->parseDate(input);45 return LocaleMac::currentLocale()->parseDate(input); 46 46 case DateComponents::DateTime: 47 47 case DateComponents::DateTimeLocal: … … 59 59 switch (dateComponents.type()) { 60 60 case DateComponents::Date: 61 return Locale Win::currentLocale()->formatDate(dateComponents);61 return LocaleMac::currentLocale()->formatDate(dateComponents); 62 62 case DateComponents::DateTime: 63 63 case DateComponents::DateTimeLocal: … … 74 74 String localizedDateFormatText() 75 75 { 76 return Locale Win::currentLocale()->dateFormatText();76 return LocaleMac::currentLocale()->dateFormatText(); 77 77 } 78 78 79 79 const Vector<String>& monthLabels() 80 80 { 81 return Locale Win::currentLocale()->monthLabels();81 return LocaleMac::currentLocale()->monthLabels(); 82 82 } 83 83 84 84 const Vector<String>& weekDayShortLabels() 85 85 { 86 return Locale Win::currentLocale()->weekDayShortLabels();86 return LocaleMac::currentLocale()->weekDayShortLabels(); 87 87 } 88 88 89 89 unsigned firstDayOfWeek() 90 90 { 91 return Locale Win::currentLocale()->firstDayOfWeek();91 return LocaleMac::currentLocale()->firstDayOfWeek(); 92 92 } 93 93 #endif -
trunk/Source/WebKit/chromium/ChangeLog
r122180 r122184 1 2012-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 1 23 2012-07-09 Dana Jansens <danakj@chromium.org> 2 24 -
trunk/Source/WebKit/chromium/WebKit.gypi
r122180 r122184 182 182 ], 183 183 }], 184 ['OS=="mac"', { 185 'webkit_unittest_files': [ 186 'tests/LocaleMacTest.cpp', 187 ], 188 }], 184 189 ['OS!="mac"', { 185 190 'webkit_unittest_files': [
Note: See TracChangeset
for help on using the changeset viewer.