Ignore:
Timestamp:
Oct 24, 2006, 1:46:03 PM (19 years ago)
Author:
kmccullo
Message:

Reviewed by Brady.

  • Fixes a date formatting issue on win. Specifically strftime cannot handle some ranges of time so we shift time call strftime and then manipulate the returned string, if needed.
  • kjs/date_object.cpp: (KJS::): (KJS::formatLocaleDate): (KJS::DateProtoFunc::callAsFunction):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/date_object.cpp

    r17239 r17258  
    155155}
    156156
    157 #endif // PLATFORM(MAC)
     157#else if PLATFORM(WIN_OS)
     158
     159enum LocaleDateTimeFormat { LocaleDateAndTime, LocaleDate, LocaleTime };
     160 
     161static JSCell* formatLocaleDate(GregorianDateTime gdt, const LocaleDateTimeFormat format)
     162{
     163    static const char* formatStrings[] = {"%#c", "%#x", "%X"};
     164 
     165    // Offset year if needed
     166    struct tm localTM = gdt;
     167    gdt.year += 1900;
     168    bool yearNeedsOffset = gdt.year < 1900 || gdt.year > 2038;
     169    if (yearNeedsOffset) {
     170        localTM.tm_year = equivalentYearForDST(gdt.year) - 1900;
     171     }
     172 
     173    // Do the formatting
     174    const int bufsize=128;
     175    char timebuffer[bufsize];
     176    int ret = strftime(timebuffer, bufsize, formatStrings[format], &localTM);
     177 
     178    if ( ret == 0 )
     179        return jsString("");
     180 
     181    // Copy original into the buffer
     182    if (yearNeedsOffset && format != LocaleTime) {
     183        static const int yearLen = 5;   // FIXME will be a problem in the year 10,000
     184        char yearString[yearLen];
     185 
     186        snprintf(yearString, yearLen, "%d", localTM.tm_year + 1900);
     187        char* yearLocation = strstr(timebuffer, yearString);
     188        snprintf(yearString, yearLen, "%d", gdt.year);
     189 
     190        strncpy(yearLocation, yearString, yearLen - 1);
     191    }
     192 
     193    return jsString(timebuffer);
     194}
     195
     196#endif // PLATFORM(WIN_OS)
    158197
    159198static UString formatDate(const GregorianDateTime &t)
     
    480519    break;
    481520#else
    482   case ToLocaleString: {
    483     struct tm gtm = t;
    484     strftime(timebuffer, bufsize, "%c", &gtm);
    485     return jsString(timebuffer);
    486     break;
    487     }
    488   case ToLocaleDateString: {
    489     struct tm gtm = t;
    490     strftime(timebuffer, bufsize, "%x", &gtm);
    491     return jsString(timebuffer);
    492     break;
    493     }
    494   case ToLocaleTimeString: {
    495     struct tm gtm = t;
    496     strftime(timebuffer, bufsize, "%X", &gtm);
    497     return jsString(timebuffer);
    498     break;
    499     }
     521  case ToLocaleString:
     522    return formatLocaleDate(t, LocaleDateAndTime);
     523    break;
     524  case ToLocaleDateString:
     525    return formatLocaleDate(t, LocaleDate);
     526    break;
     527  case ToLocaleTimeString:
     528    return formatLocaleDate(t, LocaleTime);
     529    break;
    500530#endif
    501531  case ValueOf:
Note: See TracChangeset for help on using the changeset viewer.