Source/JavaScriptCore/ChangeLog

 12012-04-08 Patrick Gansterer <paroga@webkit.org>
 2
 3 [WIN] Use GetDateFormat and GetTimeFormat instead of strftime
 4 https://bugs.webkit.org/show_bug.cgi?id=83436
 5
 6 Reviewed by NOBODY (OOPS!).
 7
 8 The MS CRT implementation of strftime calls the same two functions.
 9 Using them directly avoids the overhead of parsing the format string and removes
 10 the dependency on strftime() for WinCE where this function does not exist.
 11
 12 * runtime/DatePrototype.cpp:
 13 (JSC::formatLocaleDate):
 14
1152012-04-06 Benjamin Poulain <bpoulain@apple.com>
216
317 Do not abuse ArrayStorage's m_length for testing array consistency

Source/JavaScriptCore/runtime/DatePrototype.cpp

6666#include <unicode/udat.h>
6767#endif
6868
69 #if OS(WINCE) && !PLATFORM(QT)
70 extern "C" size_t strftime(char * const s, const size_t maxsize, const char * const format, const struct tm * const t); //provided by libce
71 #endif
72 
7369using namespace WTF;
7470
7571namespace JSC {

@@static JSCell* formatLocaleDate(ExecState* exec, DateInstance* dateObject, doubl
223219
224220static JSCell* formatLocaleDate(ExecState* exec, const GregorianDateTime& gdt, LocaleDateTimeFormat format)
225221{
 222#if OS(WINDOWS)
 223 SYSTEMTIME systemTime;
 224 memset(&systemTime, 0, sizeof(systemTime));
 225 systemTime.wYear = gdt.year + 1900;
 226 systemTime.wMonth = gdt.month + 1;
 227 systemTime.wDay = gdt.monthDay;
 228 systemTime.wDayOfWeek = gdt.weekDay;
 229 systemTime.wHour = gdt.hour;
 230 systemTime.wMinute = gdt.minute;
 231 systemTime.wSecond = gdt.second;
 232
 233 Vector<UChar, 128> buffer;
 234 size_t length = 0;
 235
 236 if (format == LocaleDate) {
 237 buffer.resize(GetDateFormatW(LOCALE_USER_DEFAULT, 0, &systemTime, 0, 0, 0));
 238 length = GetDateFormatW(LOCALE_USER_DEFAULT, 0, &systemTime, 0, buffer.data(), buffer.size());
 239 } else if (format == LocaleTime) {
 240 buffer.resize(GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &systemTime, 0, 0, 0));
 241 length = GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &systemTime, 0, buffer.data(), buffer.size());
 242 } else if (format == LocaleDateAndTime) {
 243 buffer.resize(GetDateFormatW(LOCALE_USER_DEFAULT, 0, &systemTime, 0, 0, 0) + GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &systemTime, 0, 0, 0));
 244 length = GetDateFormatW(LOCALE_USER_DEFAULT, 0, &systemTime, 0, buffer.data(), buffer.size());
 245 if (length) {
 246 buffer[length - 1] = ' ';
 247 length += GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &systemTime, 0, buffer.data() + length, buffer.size() - length);
 248 }
 249 } else
 250 ASSERT_NOT_REACHED();
 251
 252 // Remove terminating null character.
 253 if (length)
 254 length--;
 255
 256 return jsNontrivialString(exec, UString(buffer.data(), length));
 257
 258#else // OS(WINDOWS)
 259
226260#if HAVE(LANGINFO_H)
227261 static const nl_item formats[] = { D_T_FMT, D_FMT, T_FMT };
228 #elif (OS(WINCE) && !PLATFORM(QT))
229  // strftime() does not support '#' on WinCE
230  static const char* const formatStrings[] = { "%c", "%x", "%X" };
231262#else
232263 static const char* const formatStrings[] = { "%#c", "%#x", "%X" };
233264#endif
234 
 265
235266 // Offset year if needed
236267 struct tm localTM = gdt;
237268 int year = gdt.year + 1900;
238269 bool yearNeedsOffset = year < 1900 || year > 2038;
239270 if (yearNeedsOffset)
240271 localTM.tm_year = equivalentYearForDST(year) - 1900;
241 
 272
242273#if HAVE(LANGINFO_H)
243274 // We do not allow strftime to generate dates with 2-digits years,
244275 // both to avoid ambiguity, and a crash in strncpy, for years that

@@static JSCell* formatLocaleDate(ExecState* exec, const GregorianDateTime& gdt, L
259290#else
260291 size_t ret = strftime(timebuffer, bufsize, formatStrings[format], &localTM);
261292#endif
262 
 293
263294 if (ret == 0)
264295 return jsEmptyString(exec);
265 
 296
266297 // Copy original into the buffer
267298 if (yearNeedsOffset && format != LocaleTime) {
268299 static const int yearLen = 5; // FIXME will be a problem in the year 10,000
269300 char yearString[yearLen];
270 
 301
271302 snprintf(yearString, yearLen, "%d", localTM.tm_year + 1900);
272303 char* yearLocation = strstr(timebuffer, yearString);
273304 snprintf(yearString, yearLen, "%d", year);
274 
 305
275306 strncpy(yearLocation, yearString, yearLen - 1);
276307 }
277308

@@static JSCell* formatLocaleDate(ExecState* exec, const GregorianDateTime& gdt, L
296327#endif
297328
298329 return jsNontrivialString(exec, timebuffer);
 330#endif // OS(WINDOWS)
299331}
300332
301333static JSCell* formatLocaleDate(ExecState* exec, DateInstance* dateObject, double, LocaleDateTimeFormat format)