Ignore:
Timestamp:
Jul 27, 2005, 11:22:18 AM (20 years ago)
Author:
ggaren
Message:

JavaScriptCore:

-rolled in patch by Carsten Guenther <[email protected]>
for http://bugzilla.opendarwin.org/show_bug.cgi?id=3759
Date object enhancements

Test cases added:

  • layout-tests/fast/js/date-preserve-milliseconds-expected.txt: Added.
  • layout-tests/fast/js/date-preserve-milliseconds.html: Added.

Reviewed by darin.

  • kjs/date_object.cpp: (timeFromArgs): (DateProtoFuncImp::call): (DateObjectImp::construct): (DateObjectFuncImp::call): (KJS::makeTime):
  • kjs/date_object.h:
  • tests/mozilla/expected.html:

WebCore:

-added testcase for http://bugzilla.opendarwin.org/show_bug.cgi?id=3759
Date object enhancements

Reviewed by darin.

Test cases added:

  • layout-tests/fast/js/date-preserve-milliseconds-expected.txt: Added.
  • layout-tests/fast/js/date-preserve-milliseconds.html: Added.
File:
1 edited

Legend:

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

    r9889 r9922  
    6060
    6161// some constants
    62 const time_t invalidDate = -1;
     62const time_t invalidDate = LONG_MIN;
    6363const double hoursPerDay = 24;
    6464const double minutesPerHour = 60;
     
    8484#define localtime(x) localtimeUsingCF(x)
    8585#define mktime(x) mktimeUsingCF(x)
    86 #define timegm(x) timegmUsingCF(x)
    8786#define time(x) timeUsingCF(x)
    8887
     
    199198    CFRelease(timeZone);
    200199    return result;
    201 }
    202 
    203 static time_t timegmUsingCF(struct tm *tm)
    204 {
    205     return timetUsingCF(tm, UTCTimeZone());
    206200}
    207201
     
    392386#  endif
    393387#endif
     388}
     389
     390static double timeFromArgs(ExecState *exec, const List &args, int maxArgs, double ms, struct tm *t)
     391{
     392    double result = 0;
     393    int idx = 0;
     394    int numArgs = args.size();
     395   
     396    // process up to max_args arguments
     397    if (numArgs > maxArgs)
     398        numArgs = maxArgs;
     399    // hours
     400    if (maxArgs >= 4 && idx < numArgs) {
     401        t->tm_hour = 0;
     402        result = args[idx++].toInt32(exec) * msPerHour;
     403    }
     404    // minutes
     405    if (maxArgs >= 3 && idx < numArgs) {
     406        t->tm_min = 0;
     407        result += args[idx++].toInt32(exec) * msPerMinute;
     408    }
     409    // seconds
     410    if (maxArgs >= 2 && idx < numArgs) {
     411        t->tm_sec = 0;
     412        result += args[idx++].toInt32(exec) * msPerSecond;
     413    }
     414    // read ms from args if present or add the old value
     415    result += idx < numArgs ? roundValue(exec, args[idx]) : ms;
     416           
     417    return result;
    394418}
    395419
     
    558582
    559583  time_t tv = (time_t) floor(milli / 1000.0);
    560   int ms = int(milli - tv * 1000.0);
     584  double ms = milli - tv * 1000.0;
    561585
    562586  struct tm *t = utc ? gmtime(&tv) : localtime(&tv);
     
    682706    break;
    683707  case SetTime:
    684     milli = roundValue(exec,args[0]);
     708    milli = roundValue(exec, args[0]);
    685709    result = Number(milli);
    686710    thisObj.setInternalValue(result);
    687711    break;
    688712  case SetMilliSeconds:
    689     ms = args[0].toInt32(exec);
     713    ms = roundValue(exec, args[0]);
    690714    break;
    691715  case SetSeconds:
    692     t->tm_sec = args[0].toInt32(exec);
    693     if (args.size() >= 2)
    694       ms = args[1].toInt32(exec);
     716    ms = timeFromArgs(exec, args, 2, ms, t);
    695717    break;
    696718  case SetMinutes:
    697     t->tm_min = args[0].toInt32(exec);
    698     if (args.size() >= 2)
    699       t->tm_sec = args[1].toInt32(exec);
    700     if (args.size() >= 3)
    701       ms = args[2].toInt32(exec);
     719    ms = timeFromArgs(exec, args, 3, ms, t);
    702720    break;
    703721  case SetHours:
    704     t->tm_hour = args[0].toInt32(exec);
    705     if (args.size() >= 2)
    706       t->tm_min = args[1].toInt32(exec);
    707     if (args.size() >= 3)
    708       t->tm_sec = args[2].toInt32(exec);
    709     if (args.size() >= 4)
    710       ms = args[3].toInt32(exec);
     722    ms = timeFromArgs(exec, args, 4, ms, t);
    711723    break;
    712724  case SetDate:
     
    795807    value = utc;
    796808  } else if (numArgs == 1) {
    797     UString s = args[0].toString(exec);
    798     double d = s.toDouble();
    799     if (isNaN(d))
    800       value = parseDate(s);
    801     else
    802       value = d;
     809      if (args[0].type() == StringType)
     810          value = parseDate(args[0].toString(exec));
     811      else
     812          value = args[0].toPrimitive(exec).toNumber(exec);
    803813  } else {
    804814    struct tm t;
     
    821831      t.tm_sec = (numArgs >= 6) ? args[5].toInt32(exec) : 0;
    822832      t.tm_isdst = -1;
    823       int ms = (numArgs >= 7) ? args[6].toInt32(exec) : 0;
     833      double ms = (numArgs >= 7) ? roundValue(exec, args[6]) : 0;
    824834      value = makeTime(&t, ms, false);
    825835    }
     
    896906    t.tm_min = (n >= 5) ? args[4].toInt32(exec) : 0;
    897907    t.tm_sec = (n >= 6) ? args[5].toInt32(exec) : 0;
    898     int ms = (n >= 7) ? args[6].toInt32(exec) : 0;
    899     time_t mktimeResult = timegm(&t);
    900     if (mktimeResult == invalidDate)
    901       return Number(NaN);
    902     return Number(mktimeResult * 1000.0 + ms);
     908    double ms = (n >= 7) ? roundValue(exec, args[6]) : 0;
     909    return Number(makeTime(&t, ms, true));
    903910  }
    904911}
     
    957964};
    958965
    959 double KJS::makeTime(struct tm *t, int ms, bool utc)
     966double KJS::makeTime(struct tm *t, double ms, bool utc)
    960967{
    961968    int utcOffset;
     
    963970        time_t zero = 0;
    964971#if defined BSD || defined(__linux__) || defined(__APPLE__)
    965         struct tm *t3 = localtime(&zero);
    966         utcOffset = t3->tm_gmtoff;
    967         t->tm_isdst = t3->tm_isdst;
     972        struct tm t3;
     973        localtime_r(&zero, &t3);
     974        utcOffset = t3.tm_gmtoff;
     975        t->tm_isdst = t3.tm_isdst;
    968976#else
    969977        (void)localtime(&zero);
Note: See TracChangeset for help on using the changeset viewer.