Reviewed by Maciej.
authordarin <darin@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 13 Sep 2004 06:22:56 +0000 (06:22 +0000)
committerdarin <darin@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 13 Sep 2004 06:22:56 +0000 (06:22 +0000)
        - fixed <rdar://problem/3798209> any non-ASCII characters are garbled in the result of toLocaleString

        * kjs/date_object.cpp:
        (formatLocaleDate): Replaced two old functions that used LongDateTime with this one new function that
        uses CFDateFormatter.
        (DateProtoFuncImp::call): Call the new formatLocaleDate instead of both formatLocaleDate and formatLocaleTime.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@7521 268f45cc-cd09-0410-ab3c-d52691b4dbfc

JavaScriptCore/ChangeLog
JavaScriptCore/kjs/date_object.cpp

index 368a68f4a61826365261c276153d2cf50bf6476b..60d8c79a06c3f278a57eac9eed1c467d43b5e506 100644 (file)
@@ -1,3 +1,14 @@
+2004-09-12  Darin Adler  <darin@apple.com>
+
+        Reviewed by Maciej.
+
+        - fixed <rdar://problem/3798209> any non-ASCII characters are garbled in the result of toLocaleString
+
+        * kjs/date_object.cpp:
+        (formatLocaleDate): Replaced two old functions that used LongDateTime with this one new function that
+        uses CFDateFormatter.
+        (DateProtoFuncImp::call): Call the new formatLocaleDate instead of both formatLocaleDate and formatLocaleTime.
+
 2004-09-09  Maciej Stachowiak  <mjs@apple.com>
 
         Reviewed by Richard.
index fe1201babf92d093a68d505d278613f77807c0d8..d5d9dd4e54598993085814fe5f06012796ea4d61 100644 (file)
@@ -69,6 +69,7 @@ const time_t invalidDate = -1;
 #include <CoreFoundation/CoreFoundation.h>
 #include <CoreServices/CoreServices.h>
 
+using KJS::UChar;
 using KJS::UString;
 
 #define gmtime(x) gmtimeUsingCF(x)
@@ -240,26 +241,32 @@ static UString formatTime(struct tm &tm)
     return UString(buffer);
 }
 
-static UString formatLocaleDate(time_t tv)
+static UString formatLocaleDate(time_t tv, bool includeDate, bool includeTime)
 {
     LongDateTime longDateTime;
     UCConvertCFAbsoluteTimeToLongDateTime(tv - kCFAbsoluteTimeIntervalSince1970, &longDateTime);
 
-    unsigned char string[257];
-    LongDateString(&longDateTime, longDate, string, 0);
-    string[string[0] + 1] = '\0';
-    return (char *)&string[1];
-}
-
-static UString formatLocaleTime(time_t tv)
-{
-    LongDateTime longDateTime;
-    UCConvertCFAbsoluteTimeToLongDateTime(tv - kCFAbsoluteTimeIntervalSince1970, &longDateTime);
-
-    unsigned char string[257];
-    LongTimeString(&longDateTime, true, string, 0);
-    string[string[0] + 1] = '\0';
-    return (char *)&string[1];
+    CFLocaleRef locale = CFLocaleCopyCurrent();
+    CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, locale,
+        includeDate ? kCFDateFormatterLongStyle : kCFDateFormatterNoStyle,
+        includeTime ? kCFDateFormatterLongStyle : kCFDateFormatterNoStyle);
+    CFStringRef string = CFDateFormatterCreateStringWithAbsoluteTime(NULL, formatter, tv - kCFAbsoluteTimeIntervalSince1970);
+
+    // We truncate the string returned from CFDateFormatter if it's absurdly long (> 200 characters).
+    // That's not great error handling, but it just won't happen so it doesn't matter.
+    UChar buffer[200];
+    const size_t bufferLength = sizeof(buffer) / sizeof(buffer[0]);
+    size_t length = CFStringGetLength(string);
+    assert(length <= bufferLength);
+    if (length > bufferLength)
+        length = bufferLength;
+    CFStringGetCharacters(string, CFRangeMake(0, length), reinterpret_cast<UniChar *>(buffer));
+
+    CFRelease(string);
+    CFRelease(formatter);
+    CFRelease(locale);
+    
+    return UString(buffer, length);
 }
 
 #endif // APPLE_CHANGES
@@ -439,13 +446,13 @@ Value DateProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &args)
     result = String(formatDateUTCVariant(*t) + " " + formatTime(*t));
     break;
   case ToLocaleString:
-    result = String(formatLocaleDate(tv) + " " + formatLocaleTime(tv));
+    result = String(formatLocaleDate(tv, true, true));
     break;
   case ToLocaleDateString:
-    result = String(formatLocaleDate(tv));
+    result = String(formatLocaleDate(tv, true, false));
     break;
   case ToLocaleTimeString:
-    result = String(formatLocaleTime(tv));
+    result = String(formatLocaleDate(tv, false, true));
     break;
 #else
   case ToString: