Changeset 17010 in webkit for trunk/JavaScriptCore


Ignore:
Timestamp:
Oct 12, 2006, 10:58:09 AM (19 years ago)
Author:
kmccullo
Message:

Reviewed by Geoff.

Added our own tm struct to have a consistent set of fields, which lets us display the DST offset and timezone strings correctly. Also there is some code cleanup.

  • kjs/DateMath.cpp: (KJS::timeToMS): (KJS::getUTCOffset): (KJS::getDSTOffsetSimple): (KJS::dateToMS): (KJS::msToTM): (KJS::tmToKJStm): (KJS::KJStmToTm):
  • kjs/DateMath.h:
  • kjs/date_object.cpp: (KJS::gmtoffset): (KJS::formatTime): (KJS::DateProtoFunc::callAsFunction): (KJS::DateObjectImp::construct): (KJS::DateObjectImp::callAsFunction): (KJS::DateObjectFuncImp::callAsFunction): (KJS::parseDate):
  • kjs/date_object.h:
Location:
trunk/JavaScriptCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r16945 r17010  
     12006-10-11  Kevin McCullough  <[email protected]>
     2
     3        Reviewed by Geoff.
     4
     5        Added our own tm struct to have a consistent set of fields, which lets us display the DST offset and timezone strings correctly.  Also there is some code cleanup.
     6
     7        * kjs/DateMath.cpp:
     8        (KJS::timeToMS):
     9        (KJS::getUTCOffset):
     10        (KJS::getDSTOffsetSimple):
     11        (KJS::dateToMS):
     12        (KJS::msToTM):
     13        (KJS::tmToKJStm):
     14        (KJS::KJStmToTm):
     15        * kjs/DateMath.h:
     16        * kjs/date_object.cpp:
     17        (KJS::gmtoffset):
     18        (KJS::formatTime):
     19        (KJS::DateProtoFunc::callAsFunction):
     20        (KJS::DateObjectImp::construct):
     21        (KJS::DateObjectImp::callAsFunction):
     22        (KJS::DateObjectFuncImp::callAsFunction):
     23        (KJS::parseDate):
     24        * kjs/date_object.h:
     25
    1262006-10-09  Krzysztof Kowalczyk  <[email protected]>
    227
  • trunk/JavaScriptCore/kjs/DateMath.cpp

    r16855 r17010  
    271271}
    272272
    273 static inline double timeToMseconds(double hour, double min, double sec, double ms)
     273static inline double timeToMS(double hour, double min, double sec, double ms)
    274274{
    275275    return (((hour * minutesPerHour + min) * secondsPerMinute + sec) * msPerSecond + ms);
     
    320320    static bool utcOffsetInitialized = false;
    321321    if (!utcOffsetInitialized) {
    322         struct tm ltime;
    323 
    324         ltime.tm_sec = 0;
    325         ltime.tm_min = 0;
    326         ltime.tm_hour = 0;
    327         ltime.tm_mon = 0;
    328         ltime.tm_wday = 0;
    329         ltime.tm_yday = 0;
    330         ltime.tm_isdst = 0;
    331 
     322        struct ::tm ltime;
     323
     324        memset(&ltime, 0, sizeof(ltime));
     325       
    332326        // get the difference between this time zone and GMT
    333327        ltime.tm_mday = 2;
    334328        ltime.tm_year = 70;
    335 
    336 #if !PLATFORM(WIN_OS)
    337         ltime.tm_zone = 0;
    338         ltime.tm_gmtoff = 0;
    339 #endif
    340329
    341330        utcOffset = mktime(&ltime) - (hoursPerDay * secondsPerHour);
     
    359348        localTimeSeconds += secondsPerDay;
    360349
    361     struct tm prtm;
    362350    double offsetTime = (localTimeSeconds * usecPerMsec) + getUTCOffset() ;
    363351
    364     prtm.tm_hour  =  msToHours(offsetTime);
    365     prtm.tm_min   =  msToMinutes(offsetTime);
     352    // Offset from UTC but doesn't include DST obviously
     353    int offsetHour =  msToHours(offsetTime);
     354    int offsetMinute =  msToMinutes(offsetTime);
    366355
    367356    // FIXME: time_t has a potential problem in 2038
    368357    time_t localTime = static_cast<time_t>(localTimeSeconds);
    369358
    370     struct tm tm;
     359    struct ::tm tm;
    371360    #if PLATFORM(WIN_OS)
    372361    localtime_s(&tm, &localTime);
     
    374363    localtime_r(&localTime, &tm);
    375364    #endif
    376 
    377     double diff = ((tm.tm_hour - prtm.tm_hour) * secondsPerHour) + ((tm.tm_min - prtm.tm_min) * 60);
     365   
     366    double diff = ((tm.tm_hour - offsetHour) * secondsPerHour) + ((tm.tm_min - offsetMinute) * 60);
    378367
    379368    if(diff < 0)
     
    383372}
    384373
    385 // get the DST offset the time passed in
     374// Get the DST offset the time passed in
     375// ms is in UTC
    386376static double getDSTOffset(double ms)
    387377{
     
    402392}
    403393
    404 double dateToMseconds(tm* t, double ms, bool inputIsUTC)
    405 {
    406     int day = dateToDayInYear(t->tm_year + 1900, t->tm_mon, t->tm_mday);
    407     double msec_time = timeToMseconds(t->tm_hour, t->tm_min, t->tm_sec, ms);
    408     double result = (day * msPerDay) + msec_time;
     394double dateToMS(const tm& t, double milliSeconds, bool inputIsUTC)
     395{
     396
     397    int day = dateToDayInYear(t.tm_year + 1900, t.tm_mon, t.tm_mday);
     398    double ms = timeToMS(t.tm_hour, t.tm_min, t.tm_sec, milliSeconds);
     399    double result = (day * msPerDay) + ms;
    409400
    410401    if(!inputIsUTC) { // convert to UTC
    411         result -= getUTCOffset();
     402        result -= getUTCOffset();       
    412403        result -= getDSTOffset(result);
    413404    }
     
    436427    tm.tm_isdst =  dstOff != 0.0;
    437428
    438     // All other OS' seems to have these fields
     429    tm.tm_gmtoff = (dstOff + getUTCOffset()) / usecPerMsec;
     430    tm.tm_zone = 0;
     431}
     432
     433// converting between the two tm structures
     434tm tmToKJStm(const struct ::tm& inTm)
     435{
     436    struct tm ret;
     437    memset(&ret, 0, sizeof(ret));
     438
     439    ret.tm_sec   =  inTm.tm_sec;
     440    ret.tm_min   =  inTm.tm_min;
     441    ret.tm_hour  =  inTm.tm_hour;
     442    ret.tm_wday  =  inTm.tm_wday;
     443    ret.tm_mday  =  inTm.tm_mday;
     444    ret.tm_yday  =  inTm.tm_yday;
     445    ret.tm_mon   =  inTm.tm_mon;
     446    ret.tm_year  =  inTm.tm_year;
     447    ret.tm_isdst =  inTm.tm_isdst;
     448
    439449#if !PLATFORM(WIN_OS)
    440     struct tm xtm;
    441     // FIXME: time_t has a potential problem in 2038
    442     time_t seconds = static_cast<time_t>(ms/usecPerMsec);
    443     localtime_r(&seconds, &xtm);
    444     tm.tm_gmtoff = xtm.tm_gmtoff;
    445     tm.tm_zone = xtm.tm_zone;
     450    ret.tm_gmtoff = inTm.tm_gmtoff;
     451    ret.tm_zone = inTm.tm_zone;
    446452#endif
     453
     454    return ret;
     455}
     456
     457::tm KJStmToTm(const struct tm& inTm)
     458{
     459    struct ::tm ret;
     460    memset(&ret, 0, sizeof(ret));
     461
     462    ret.tm_sec   =  inTm.tm_sec;
     463    ret.tm_min   =  inTm.tm_min;
     464    ret.tm_hour  =  inTm.tm_hour;
     465    ret.tm_wday  =  inTm.tm_wday;
     466    ret.tm_mday  =  inTm.tm_mday;
     467    ret.tm_yday  =  inTm.tm_yday;
     468    ret.tm_mon   =  inTm.tm_mon;
     469    ret.tm_year  =  inTm.tm_year;
     470    ret.tm_isdst =  inTm.tm_isdst;
     471
     472#if !PLATFORM(WIN_OS)
     473    ret.tm_gmtoff = inTm.tm_gmtoff;
     474    ret.tm_zone = inTm.tm_zone;
     475#endif
     476
     477    return ret;
    447478}
    448479
  • trunk/JavaScriptCore/kjs/DateMath.h

    r16855 r17010  
    4646namespace KJS {
    4747
     48// Intentionally overridding the default tm of the system
     49// Not all OS' have the same members of their tm's
     50struct tm {
     51    int tm_sec;
     52    int tm_min;
     53    int tm_hour;
     54    int tm_wday;
     55    int tm_mday;
     56    int tm_yday;
     57    int tm_mon;
     58    int tm_year;
     59    int tm_isdst;
     60    long tm_gmtoff;
     61    char* tm_zone;
     62};
     63
    4864// Constants //
    4965
     
    6177// Exported Functions //
    6278void msToTM(double, bool outputIsUTC, struct tm& );
    63 double dateToMseconds(tm*, double, bool inputIsUTC);
     79double dateToMS(const tm&, double, bool inputIsUTC);
    6480double getUTCOffset();
     81
     82tm tmToKJStm(const struct ::tm&);
     83::tm KJStmToTm(const struct tm&);
    6584
    6685}   //namespace KJS
  • trunk/JavaScriptCore/kjs/date_object.cpp

    r16855 r17010  
    6868inline int gmtoffset(const tm& t)
    6969{
    70 #if PLATFORM(WIN_OS)
    71     // Time is supposed to be in the current timezone.
    72     return -(_timezone / 60 - (t.tm_isdst != 0 ? 60 : 0 )) * 60;
    73 #else
    7470    return t.tm_gmtoff;
    75 #endif
    7671}
    7772
     
    187182    } else {
    188183        int offset = abs(gmtoffset(t));
    189 #if PLATFORM(WIN_OS)
    190184        char tzname[70];
    191         strftime(tzname, sizeof(tzname), "%Z", &t);
    192 #else
    193         const char *tzname = t.tm_zone;
    194 #endif
     185        struct ::tm gtm = KJStmToTm(t);
     186        strftime(tzname, sizeof(tzname), "%Z", &gtm);
     187
    195188        if (tzname) {
    196189            snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d GMT%c%02d%02d (%s)",
     
    487480    break;
    488481#else
    489   case ToLocaleString:
    490     strftime(timebuffer, bufsize, "%c", &t);
     482  case ToLocaleString: {
     483    struct ::tm gtm = KJStmToTm(t);
     484    strftime(timebuffer, bufsize, "%c", &gtm);
    491485    return jsString(timebuffer);
    492486    break;
    493   case ToLocaleDateString:
    494     strftime(timebuffer, bufsize, "%x", &t);
     487    }
     488  case ToLocaleDateString: {
     489    struct ::tm gtm = KJStmToTm(t);
     490    strftime(timebuffer, bufsize, "%x", &gtm);
    495491    return jsString(timebuffer);
    496492    break;
    497   case ToLocaleTimeString:
    498     strftime(timebuffer, bufsize, "%X", &t);
     493    }
     494  case ToLocaleTimeString: {
     495    struct ::tm gtm = KJStmToTm(t);
     496    strftime(timebuffer, bufsize, "%X", &gtm);
    499497    return jsString(timebuffer);
    500498    break;
     499    }
    501500#endif
    502501  case ValueOf:
     
    560559      id == SetMinutes || id == SetHours || id == SetDate ||
    561560      id == SetMonth || id == SetFullYear ) {
    562     result = jsNumber(dateToMseconds(&t, ms, utc));
     561    result = jsNumber(dateToMS(t, ms, utc));
    563562    thisDateObj->setInternalValue(result);
    564563  }
     
    647646      t.tm_isdst = -1;
    648647      double ms = (numArgs >= 7) ? roundValue(exec, args[6]) : 0;
    649       value = dateToMseconds(&t, ms, false);
     648      value = dateToMS(t, ms, false);
    650649    }
    651650  }
     
    660659{
    661660    time_t t = time(0);
    662     tm ts = *localtime(&t);
     661    tm ts = tmToKJStm(*localtime(&t));
    663662    return jsString(formatDate(ts) + " " + formatTime(ts, false));
    664663}
     
    700699    t.tm_sec = (n >= 6) ? args[5]->toInt32(exec) : 0;
    701700    double ms = (n >= 7) ? roundValue(exec, args[6]) : 0;
    702     return jsNumber(dateToMseconds(&t, ms, true));
     701    return jsNumber(dateToMS(t, ms, true));
    703702  }
    704703}
     
    10671066        t.tm_hour = hour;
    10681067
    1069         // Use our dateToMseconds() rather than mktime() as the latter can't handle the full year range.
    1070         return dateToMseconds(&t, 0, false);
     1068        // Use our dateToMS() rather than mktime() as the latter can't handle the full year range.
     1069        return dateToMS(t, 0, false);
    10711070    }
    10721071
  • trunk/JavaScriptCore/kjs/date_object.h

    r15846 r17010  
    2626
    2727namespace KJS {
     28
     29    struct tm;
    2830
    2931    class FunctionPrototype;
Note: See TracChangeset for help on using the changeset viewer.