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:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.