Ignore:
Timestamp:
Nov 5, 2009, 10:52:10 PM (16 years ago)
Author:
[email protected]
Message:

Rolled out r50590 because it doesn't build on Windows.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/DateMath.cpp

    r50590 r50591  
    5151#include "StringExtras.h"
    5252
    53 #include "CallFrame.h"
    54 
    5553#include <algorithm>
    5654#include <limits.h>
     
    6462#endif
    6563
     64#if PLATFORM(DARWIN)
     65#include <notify.h>
     66#endif
     67
    6668#if PLATFORM(WINCE)
    6769extern "C" size_t strftime(char * const s, const size_t maxsize, const char * const format, const struct tm * const t);
     
    8082
    8183namespace WTF {
    82 
    83 double getCurrentUTCTime()
    84 {
    85     return floor(getCurrentUTCTimeWithMicroseconds());
    86 }
    87 
    88 // Returns current time in milliseconds since 1 Jan 1970.
    89 double getCurrentUTCTimeWithMicroseconds()
    90 {
    91     return currentTime() * 1000.0;
    92 }
    93 
    94 void getLocalTime(const time_t* localTime, struct tm* localTM)
    95 {
    96 #if COMPILER(MSVC7) || COMPILER(MINGW) || PLATFORM(WINCE)
    97     *localTM = *localtime(localTime);
    98 #elif COMPILER(MSVC)
    99     localtime_s(localTM, localTime);
    100 #else
    101     localtime_r(localTime, localTM);
    102 #endif
    103 }
    104 
    105 } // namespace WTF
    106 
    107 using namespace WTF;
    108 
    109 namespace JSC {
    11084
    11185/* Constants */
     
    320294}
    321295
     296double getCurrentUTCTime()
     297{
     298    return floor(getCurrentUTCTimeWithMicroseconds());
     299}
     300
     301// Returns current time in milliseconds since 1 Jan 1970.
     302double getCurrentUTCTimeWithMicroseconds()
     303{
     304    return currentTime() * 1000.0;
     305}
     306
     307void getLocalTime(const time_t* localTime, struct tm* localTM)
     308{
     309#if COMPILER(MSVC7) || COMPILER(MINGW) || PLATFORM(WINCE)
     310    *localTM = *localtime(localTime);
     311#elif COMPILER(MSVC)
     312    localtime_s(localTM, localTime);
     313#else
     314    localtime_r(localTime, localTM);
     315#endif
     316}
     317
    322318// There is a hard limit at 2038 that we currently do not have a workaround
    323319// for (rdar://problem/5052975).
     
    404400}
    405401
     402#if PLATFORM(DARWIN)
     403static int32_t s_cachedUTCOffset; // In milliseconds. An assumption here is that access to an int32_t variable is atomic on platforms that take this code path.
     404static bool s_haveCachedUTCOffset;
     405static int s_notificationToken;
     406#endif
     407
    406408/*
    407409 * Get the difference in milliseconds between this time zone and UTC (GMT)
    408410 * NOT including DST.
    409411 */
    410 double getUTCOffset(ExecState* exec)
    411 {
    412     double utcOffset = exec->globalData().cachedUTCOffset;
    413     if (!isnan(utcOffset))
    414         return utcOffset;
    415     exec->globalData().cachedUTCOffset = calculateUTCOffset();
    416     return exec->globalData().cachedUTCOffset;
     412double getUTCOffset()
     413{
     414#if PLATFORM(DARWIN)
     415    if (s_haveCachedUTCOffset) {
     416        int notified;
     417        uint32_t status = notify_check(s_notificationToken, &notified);
     418        if (status == NOTIFY_STATUS_OK && !notified)
     419            return s_cachedUTCOffset;
     420    }
     421#endif
     422
     423    int32_t utcOffset = calculateUTCOffset();
     424
     425#if PLATFORM(DARWIN)
     426    // Theoretically, it is possible that several threads will be executing this code at once, in which case we will have a race condition,
     427    // and a newer value may be overwritten. In practice, time zones don't change that often.
     428    s_cachedUTCOffset = utcOffset;
     429#endif
     430
     431    return utcOffset;
    417432}
    418433
     
    472487}
    473488
    474 double gregorianDateTimeToMS(ExecState* exec, const GregorianDateTime& t, double milliSeconds, bool inputIsUTC)
     489double gregorianDateTimeToMS(const GregorianDateTime& t, double milliSeconds, bool inputIsUTC)
    475490{
    476491    int day = dateToDayInYear(t.year + 1900, t.month, t.monthDay);
     
    479494
    480495    if (!inputIsUTC) { // convert to UTC
    481         double utcOffset = getUTCOffset(exec);
     496        double utcOffset = getUTCOffset();
    482497        result -= utcOffset;
    483498        result -= getDSTOffset(result, utcOffset);
     
    488503
    489504// input is UTC
    490 void msToGregorianDateTime(ExecState* exec, double ms, bool outputIsUTC, GregorianDateTime& tm)
     505void msToGregorianDateTime(double ms, bool outputIsUTC, GregorianDateTime& tm)
    491506{
    492507    double dstOff = 0.0;
    493508    double utcOff = 0.0;
    494509    if (!outputIsUTC) {
    495         utcOff = getUTCOffset(exec);
     510        utcOff = getUTCOffset();
    496511        dstOff = getDSTOffset(ms, utcOff);
    497512        ms += dstOff + utcOff;
     
    520535
    521536    equivalentYearForDST(2000); // Need to call once to initialize a static used in this function.
     537#if PLATFORM(DARWIN)
     538    // Register for a notification whenever the time zone changes.
     539    uint32_t status = notify_register_check("com.apple.system.timezone", &s_notificationToken);
     540    if (status == NOTIFY_STATUS_OK) {
     541        s_cachedUTCOffset = calculateUTCOffset();
     542        s_haveCachedUTCOffset = true;
     543    }
     544#endif
    522545}
    523546
     
    600623}
    601624
    602 // Odd case where 'exec' is allowed to be 0, to accomodate a caller in WebCore.
    603 double parseDateFromNullTerminatedCharacters(const char* dateString, ExecState* exec)
     625double parseDateFromNullTerminatedCharacters(const char* dateString)
    604626{
    605627    // This parses a date in the form:
     
    868890            year += 1900;
    869891    }
    870    
    871     double ms = ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSecond;
     892
    872893    // fall back to local timezone
    873894    if (!haveTZ) {
    874         if (exec) {
    875             double utcOffset = getUTCOffset(exec);
    876             double dstOffset = getDSTOffset(ms, utcOffset);
    877             offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
    878         } else {
    879             double utcOffset = calculateUTCOffset();
    880             double dstOffset = getDSTOffset(ms, utcOffset);
    881             offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
    882         }
    883     }
    884 
    885     return ms - (offset * msPerMinute);
     895        GregorianDateTime t;
     896        t.monthDay = day;
     897        t.month = month;
     898        t.year = year - 1900;
     899        t.isDST = -1;
     900        t.second = second;
     901        t.minute = minute;
     902        t.hour = hour;
     903
     904        // Use our gregorianDateTimeToMS() rather than mktime() as the latter can't handle the full year range.
     905        return gregorianDateTimeToMS(t, 0, false);
     906    }
     907
     908    return (ymdhmsToSeconds(year, month + 1, day, hour, minute, second) - (offset * 60.0)) * msPerSecond;
    886909}
    887910
     
    896919
    897920
    898 } // namespace JSC
     921} // namespace WTF
Note: See TracChangeset for help on using the changeset viewer.