Changeset 17010 in webkit for trunk/JavaScriptCore
- Timestamp:
- Oct 12, 2006, 10:58:09 AM (19 years ago)
- Location:
- trunk/JavaScriptCore
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/ChangeLog
r16945 r17010 1 2006-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 1 26 2006-10-09 Krzysztof Kowalczyk <[email protected]> 2 27 -
trunk/JavaScriptCore/kjs/DateMath.cpp
r16855 r17010 271 271 } 272 272 273 static inline double timeToM seconds(double hour, double min, double sec, double ms)273 static inline double timeToMS(double hour, double min, double sec, double ms) 274 274 { 275 275 return (((hour * minutesPerHour + min) * secondsPerMinute + sec) * msPerSecond + ms); … … 320 320 static bool utcOffsetInitialized = false; 321 321 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(<ime, 0, sizeof(ltime)); 325 332 326 // get the difference between this time zone and GMT 333 327 ltime.tm_mday = 2; 334 328 ltime.tm_year = 70; 335 336 #if !PLATFORM(WIN_OS)337 ltime.tm_zone = 0;338 ltime.tm_gmtoff = 0;339 #endif340 329 341 330 utcOffset = mktime(<ime) - (hoursPerDay * secondsPerHour); … … 359 348 localTimeSeconds += secondsPerDay; 360 349 361 struct tm prtm;362 350 double offsetTime = (localTimeSeconds * usecPerMsec) + getUTCOffset() ; 363 351 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); 366 355 367 356 // FIXME: time_t has a potential problem in 2038 368 357 time_t localTime = static_cast<time_t>(localTimeSeconds); 369 358 370 struct tm tm;359 struct ::tm tm; 371 360 #if PLATFORM(WIN_OS) 372 361 localtime_s(&tm, &localTime); … … 374 363 localtime_r(&localTime, &tm); 375 364 #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); 378 367 379 368 if(diff < 0) … … 383 372 } 384 373 385 // get the DST offset the time passed in 374 // Get the DST offset the time passed in 375 // ms is in UTC 386 376 static double getDSTOffset(double ms) 387 377 { … … 402 392 } 403 393 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; 394 double 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; 409 400 410 401 if(!inputIsUTC) { // convert to UTC 411 result -= getUTCOffset(); 402 result -= getUTCOffset(); 412 403 result -= getDSTOffset(result); 413 404 } … … 436 427 tm.tm_isdst = dstOff != 0.0; 437 428 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 434 tm 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 439 449 #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; 446 452 #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; 447 478 } 448 479 -
trunk/JavaScriptCore/kjs/DateMath.h
r16855 r17010 46 46 namespace KJS { 47 47 48 // Intentionally overridding the default tm of the system 49 // Not all OS' have the same members of their tm's 50 struct 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 48 64 // Constants // 49 65 … … 61 77 // Exported Functions // 62 78 void msToTM(double, bool outputIsUTC, struct tm& ); 63 double dateToM seconds(tm*, double, bool inputIsUTC);79 double dateToMS(const tm&, double, bool inputIsUTC); 64 80 double getUTCOffset(); 81 82 tm tmToKJStm(const struct ::tm&); 83 ::tm KJStmToTm(const struct tm&); 65 84 66 85 } //namespace KJS -
trunk/JavaScriptCore/kjs/date_object.cpp
r16855 r17010 68 68 inline int gmtoffset(const tm& t) 69 69 { 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 #else74 70 return t.tm_gmtoff; 75 #endif76 71 } 77 72 … … 187 182 } else { 188 183 int offset = abs(gmtoffset(t)); 189 #if PLATFORM(WIN_OS)190 184 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", >m); 187 195 188 if (tzname) { 196 189 snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d GMT%c%02d%02d (%s)", … … 487 480 break; 488 481 #else 489 case ToLocaleString: 490 strftime(timebuffer, bufsize, "%c", &t); 482 case ToLocaleString: { 483 struct ::tm gtm = KJStmToTm(t); 484 strftime(timebuffer, bufsize, "%c", >m); 491 485 return jsString(timebuffer); 492 486 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", >m); 495 491 return jsString(timebuffer); 496 492 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", >m); 499 497 return jsString(timebuffer); 500 498 break; 499 } 501 500 #endif 502 501 case ValueOf: … … 560 559 id == SetMinutes || id == SetHours || id == SetDate || 561 560 id == SetMonth || id == SetFullYear ) { 562 result = jsNumber(dateToM seconds(&t, ms, utc));561 result = jsNumber(dateToMS(t, ms, utc)); 563 562 thisDateObj->setInternalValue(result); 564 563 } … … 647 646 t.tm_isdst = -1; 648 647 double ms = (numArgs >= 7) ? roundValue(exec, args[6]) : 0; 649 value = dateToM seconds(&t, ms, false);648 value = dateToMS(t, ms, false); 650 649 } 651 650 } … … 660 659 { 661 660 time_t t = time(0); 662 tm ts = *localtime(&t);661 tm ts = tmToKJStm(*localtime(&t)); 663 662 return jsString(formatDate(ts) + " " + formatTime(ts, false)); 664 663 } … … 700 699 t.tm_sec = (n >= 6) ? args[5]->toInt32(exec) : 0; 701 700 double ms = (n >= 7) ? roundValue(exec, args[6]) : 0; 702 return jsNumber(dateToM seconds(&t, ms, true));701 return jsNumber(dateToMS(t, ms, true)); 703 702 } 704 703 } … … 1067 1066 t.tm_hour = hour; 1068 1067 1069 // Use our dateToM seconds() rather than mktime() as the latter can't handle the full year range.1070 return dateToM seconds(&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); 1071 1070 } 1072 1071 -
trunk/JavaScriptCore/kjs/date_object.h
r15846 r17010 26 26 27 27 namespace KJS { 28 29 struct tm; 28 30 29 31 class FunctionPrototype;
Note:
See TracChangeset
for help on using the changeset viewer.