Title: [115600] trunk/Source
Revision
115600
Author
[email protected]
Date
2012-04-29 17:25:56 -0700 (Sun, 29 Apr 2012)

Log Message

[Mac] Add LocalizedDateMac
https://bugs.webkit.org/show_bug.cgi?id=85039

Reviewed by Kentaro Hara.

Source/WebCore:

A date shown <input type=date> should be formatted for user's OS
settings. Chromium-Mac used LocalizedDateICU.cpp to format/parse visible
date strings and it didn't reflect user-settings.

Test: covered by fast/forms/date/date-appearance.html

* WebCore.gyp/WebCore.gyp:
Use LocalizedDateMac.mm for OS X instead of LocalizedDateICU.cpp.
* WebCore.gypi: Add LocalizedDateMac.mm
* platform/text/mac/LocalizedDateMac.mm: Added.
(WebCore::createShortDateFormatter):
Creates a NSDateFormatter with desired settings.
(WebCore::parseLocalizedDate): Impelment for tyep=date.
(WebCore::formatLocalizedDate): ditto.
(WebCore::isYearSymbol): A readability helper for format string parsing.
(WebCore::isMonthSymbol): ditto.
(WebCore::isDaySymbol): ditto.
(WebCore::localizeDateFormat):
Parse a format string, and replace symbols with user-friendly labels.
(WebCore::localizedDateFormatText):
Gets a format string, and apply localizeDateFormat().
(WebCore::monthLabels): Obtain month names from the system.
(WebCore::weekDayShortLabels): Obtain week day symbols from the system.
(WebCore::firstDayOfWeek): Obtain first day of week from the system.
* platform/text/ICULocale.cpp:
(WebCore::createFallbackMonthLabels): Uses WTF::monthFullName.

Source/WTF:

* wtf/DateMath.h:
(WTF): Add monthFullName[]. It is useful to make fallback month
names for calendar/date related features.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (115599 => 115600)


--- trunk/Source/WTF/ChangeLog	2012-04-29 20:08:39 UTC (rev 115599)
+++ trunk/Source/WTF/ChangeLog	2012-04-30 00:25:56 UTC (rev 115600)
@@ -1,3 +1,14 @@
+2012-04-29  Kent Tamura  <[email protected]>
+
+        [Mac] Add LocalizedDateMac
+        https://bugs.webkit.org/show_bug.cgi?id=85039
+
+        Reviewed by Kentaro Hara.
+
+        * wtf/DateMath.h:
+        (WTF): Add monthFullName[]. It is useful to make fallback month
+        names for calendar/date related features.
+
 2012-04-26  Carlos Garcia Campos  <[email protected]>
 
         [SOUP] Add a way to register custom uri schemes in WebKit2

Modified: trunk/Source/WTF/wtf/DateMath.h (115599 => 115600)


--- trunk/Source/WTF/wtf/DateMath.h	2012-04-29 20:08:39 UTC (rev 115599)
+++ trunk/Source/WTF/wtf/DateMath.h	2012-04-30 00:25:56 UTC (rev 115600)
@@ -75,6 +75,7 @@
 
 const char* const weekdayName[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
 const char* const monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+const char* const monthFullName[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
 
 const double hoursPerDay = 24.0;
 const double minutesPerHour = 60.0;

Modified: trunk/Source/WebCore/ChangeLog (115599 => 115600)


--- trunk/Source/WebCore/ChangeLog	2012-04-29 20:08:39 UTC (rev 115599)
+++ trunk/Source/WebCore/ChangeLog	2012-04-30 00:25:56 UTC (rev 115600)
@@ -1,3 +1,37 @@
+2012-04-29  Kent Tamura  <[email protected]>
+
+        [Mac] Add LocalizedDateMac
+        https://bugs.webkit.org/show_bug.cgi?id=85039
+
+        Reviewed by Kentaro Hara.
+
+        A date shown <input type=date> should be formatted for user's OS
+        settings. Chromium-Mac used LocalizedDateICU.cpp to format/parse visible
+        date strings and it didn't reflect user-settings.
+
+        Test: covered by fast/forms/date/date-appearance.html
+
+        * WebCore.gyp/WebCore.gyp:
+        Use LocalizedDateMac.mm for OS X instead of LocalizedDateICU.cpp.
+        * WebCore.gypi: Add LocalizedDateMac.mm
+        * platform/text/mac/LocalizedDateMac.mm: Added.
+        (WebCore::createShortDateFormatter):
+        Creates a NSDateFormatter with desired settings.
+        (WebCore::parseLocalizedDate): Impelment for tyep=date.
+        (WebCore::formatLocalizedDate): ditto.
+        (WebCore::isYearSymbol): A readability helper for format string parsing.
+        (WebCore::isMonthSymbol): ditto.
+        (WebCore::isDaySymbol): ditto.
+        (WebCore::localizeDateFormat):
+        Parse a format string, and replace symbols with user-friendly labels.
+        (WebCore::localizedDateFormatText):
+        Gets a format string, and apply localizeDateFormat().
+        (WebCore::monthLabels): Obtain month names from the system.
+        (WebCore::weekDayShortLabels): Obtain week day symbols from the system.
+        (WebCore::firstDayOfWeek): Obtain first day of week from the system.
+        * platform/text/ICULocale.cpp:
+        (WebCore::createFallbackMonthLabels): Uses WTF::monthFullName.
+
 2012-04-29  Sam Weinig  <[email protected]>
 
         Add support for the Blob constructor (Part 2)

Modified: trunk/Source/WebCore/WebCore.gyp/WebCore.gyp (115599 => 115600)


--- trunk/Source/WebCore/WebCore.gyp/WebCore.gyp	2012-04-29 20:08:39 UTC (rev 115599)
+++ trunk/Source/WebCore/WebCore.gyp/WebCore.gyp	2012-04-30 00:25:56 UTC (rev 115600)
@@ -1600,6 +1600,10 @@
 
             ['include', 'WebKit/mac/WebCoreSupport/WebSystemInterface\\.mm$'],
 
+            # We use LocalizedDateMac.mm instead of LocalizedDateICU.cpp.
+            ['exclude', 'platform/text/LocalizedDateICU\\.cpp$'],
+            ['include', 'platform/text/mac/LocalizedDateMac\\.mm$'],
+
             # The Mac uses platform/mac/KillRingMac.mm instead of the dummy
             # implementation.
             ['exclude', 'platform/KillRingNone\\.cpp$'],

Modified: trunk/Source/WebCore/WebCore.gypi (115599 => 115600)


--- trunk/Source/WebCore/WebCore.gypi	2012-04-29 20:08:39 UTC (rev 115599)
+++ trunk/Source/WebCore/WebCore.gypi	2012-04-30 00:25:56 UTC (rev 115600)
@@ -4523,6 +4523,7 @@
             'platform/text/gtk/TextCodecGtk.cpp',
             'platform/text/gtk/TextCodecGtk.h',
             'platform/text/mac/HyphenationMac.mm',
+            'platform/text/mac/LocalizedDateMac.mm',
             'platform/text/mac/LocalizedNumberMac.mm',
             'platform/text/mac/ShapeArabic.c',
             'platform/text/mac/ShapeArabic.h',

Modified: trunk/Source/WebCore/platform/text/ICULocale.cpp (115599 => 115600)


--- trunk/Source/WebCore/platform/text/ICULocale.cpp	2012-04-29 20:08:39 UTC (rev 115599)
+++ trunk/Source/WebCore/platform/text/ICULocale.cpp	2012-04-30 00:25:56 UTC (rev 115600)
@@ -33,6 +33,7 @@
 
 #include "LocalizedStrings.h"
 #include <limits>
+#include <wtf/DateMath.h>
 #include <wtf/PassOwnPtr.h>
 #include <wtf/text/StringBuilder.h>
 
@@ -418,19 +419,9 @@
 static PassOwnPtr<Vector<String> > createFallbackMonthLabels()
 {
     OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>());
-    labels->reserveCapacity(12);
-    labels->append("January");
-    labels->append("February");
-    labels->append("March");
-    labels->append("April");
-    labels->append("May");
-    labels->append("June");
-    labels->append("July");
-    labels->append("August");
-    labels->append("September");
-    labels->append("October");
-    labels->append("November");
-    labels->append("December");
+    labels->reserveCapacity(WTF_ARRAY_LENGTH(WTF::monthFullName));
+    for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthFullName); ++i)
+        labels->append(WTF::monthFullName[i]);
     return labels.release();
 }
 

Added: trunk/Source/WebCore/platform/text/mac/LocalizedDateMac.mm (0 => 115600)


--- trunk/Source/WebCore/platform/text/mac/LocalizedDateMac.mm	                        (rev 0)
+++ trunk/Source/WebCore/platform/text/mac/LocalizedDateMac.mm	2012-04-30 00:25:56 UTC (rev 115600)
@@ -0,0 +1,198 @@
+/*
+ * Copyright (C) 2012 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "LocalizedDate.h"
+
+#import <Foundation/NSDateFormatter.h>
+#include "LocalizedStrings.h"
+#include <limits>
+#include <wtf/DateMath.h>
+#include <wtf/RetainPtr.h>
+#include <wtf/text/StringBuilder.h>
+
+using namespace std;
+
+namespace WebCore {
+
+static NSDateFormatter *createShortDateFormatter()
+{
+    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+    [formatter setDateStyle:NSDateFormatterShortStyle];
+    [formatter setTimeStyle:NSDateFormatterNoStyle];
+    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
+    [formatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
+    return formatter;
+}
+
+double parseLocalizedDate(const String& input, DateComponents::Type type)
+{
+    switch (type) {
+    case DateComponents::Date: {
+        RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
+        return [[formatter.get() dateFromString:input] timeIntervalSince1970] * msPerSecond;
+    }
+    case DateComponents::DateTime:
+    case DateComponents::DateTimeLocal:
+    case DateComponents::Month:
+    case DateComponents::Time:
+    case DateComponents::Week:
+    case DateComponents::Invalid:
+        break;
+    }
+    return numeric_limits<double>::quiet_NaN();
+}
+
+String formatLocalizedDate(const DateComponents& dateComponents)
+{
+    switch (dateComponents.type()) {
+    case DateComponents::Date: {
+        RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
+        NSTimeInterval interval = dateComponents.millisecondsSinceEpoch() / msPerSecond;
+        return String([formatter.get() stringFromDate:[NSDate dateWithTimeIntervalSince1970:interval]]);
+    }
+    case DateComponents::DateTime:
+    case DateComponents::DateTimeLocal:
+    case DateComponents::Month:
+    case DateComponents::Time:
+    case DateComponents::Week:
+    case DateComponents::Invalid:
+        break;
+    }
+    return String();
+}
+
+#if ENABLE(CALENDAR_PICKER)
+static bool isYearSymbol(UChar letter) { return letter == 'y' || letter == 'Y' || letter == 'u'; }
+static bool isMonthSymbol(UChar letter) { return letter == 'M' || letter == 'L'; }
+static bool isDaySymbol(UChar letter) { return letter == 'd'; }
+
+// http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
+static String localizeDateFormat(const String& format)
+{
+    String yearText = dateFormatYearText().isEmpty() ? "Year" : dateFormatYearText();
+    String monthText = dateFormatMonthText().isEmpty() ? "Month" : dateFormatMonthText();
+    String dayText = dateFormatDayInMonthText().isEmpty() ? "Day" : dateFormatDayInMonthText();
+    StringBuilder buffer;
+    bool inQuote = false;
+    for (unsigned i = 0; i < format.length(); ++i) {
+        UChar ch = format[i];
+        if (inQuote) {
+            if (ch == '\'') {
+                inQuote = false;
+                ASSERT(i);
+                if (format[i - 1] == '\'')
+                    buffer.append('\'');
+            } else
+                buffer.append(ch);
+            continue;
+        }
+
+        if (ch == '\'') {
+            inQuote = true;
+            if (i > 0 && format[i - 1] == '\'')
+                buffer.append(ch);
+        } else if (isYearSymbol(ch)) {
+            if (i > 0 && format[i - 1] == ch)
+                continue;
+            buffer.append(yearText);
+        } else if (isMonthSymbol(ch)) {
+            if (i > 0 && format[i - 1] == ch)
+                continue;
+            buffer.append(monthText);
+        } else if (isDaySymbol(ch)) {
+            if (i > 0 && format[i - 1] == ch)
+                continue;
+            buffer.append(dayText);
+        } else
+            buffer.append(ch);
+    }
+    return buffer.toString();
+}
+
+String localizedDateFormatText()
+{
+    DEFINE_STATIC_LOCAL(String, text, ());
+    if (!text.isEmpty())
+        return text;
+    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
+    text = localizeDateFormat(String([formatter.get() dateFormat]));
+    return text;
+}
+
+const Vector<String>& monthLabels()
+{
+    DEFINE_STATIC_LOCAL(Vector<String>, labels, ());
+    if (!labels.isEmpty())
+        return labels;
+    labels.reserveCapacity(12);
+    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
+    NSArray *array = [formatter.get() monthSymbols];
+    if ([array count] == 12) {
+        for (unsigned i = 0; i < 12; ++i)
+            labels.append(String([array objectAtIndex:i]));
+        return labels;
+    }
+    for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthFullName); ++i)
+        labels->append(WTF::monthFullName[i]);
+    return labels;
+}
+
+const Vector<String>& weekDayShortLabels()
+{
+    DEFINE_STATIC_LOCAL(Vector<String>, labels, ());
+    if (!labels.isEmpty())
+        return labels;
+    labels.reserveCapacity(7);
+    RetainPtr<NSDateFormatter> formatter(AdoptNS, createShortDateFormatter());
+    NSArray *array = [formatter.get() shortWeekdaySymbols];
+    if ([array count] == 7) {
+        for (unsigned i = 0; i < 7; ++i)
+            labels.append(String([array objectAtIndex:i]));
+        return labels;
+    }
+    for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::weekdayName); ++i) {
+        // weekdayName starts with Monday.
+        labels.append(WTF::weekdayName[(i + 6) % 7]);
+    }
+    return labels;
+}
+
+unsigned firstDayOfWeek()
+{
+    RetainPtr<NSCalendar> calendar(AdoptNS, [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]);
+    // The document for NSCalendar - firstWeekday doesn't have an explanation of
+    // firstWeekday value. We can guess it by the document of NSDateComponents -
+    // weekDay, so it can be 1 through 7 and 1 is Sunday.
+    return [calendar.get() firstWeekday] - 1;
+}
+#endif
+
+}
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to