Changeset 100205 in webkit


Ignore:
Timestamp:
Nov 14, 2011 4:04:53 PM (12 years ago)
Author:
abarth@webkit.org
Message:

DateMath.cpp should not depend on JavaScriptCore
https://bugs.webkit.org/show_bug.cgi?id=71747

Reviewed by Darin Adler.

This patch moves the JSC-specific parts of DateMath into JSDateMath in
JavaScriptCore. There shouldn't be any behavior change.

  • CMakeLists.txt:
  • GNUmakefile.list.am:
  • JavaScriptCore.gypi:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • Target.pri:
  • runtime/DateConstructor.cpp:
  • runtime/DateConversion.cpp:
  • runtime/DateInstance.cpp:
  • runtime/DateInstanceCache.h:
  • runtime/DatePrototype.cpp:
  • runtime/InitializeThreading.cpp:
  • runtime/JSDateMath.cpp: Copied from Source/JavaScriptCore/wtf/DateMath.cpp.

(JSC::timeToMS):
(JSC::msToSeconds):

  • runtime/JSDateMath.h: Copied from Source/JavaScriptCore/wtf/DateMath.h.
  • wtf/DateMath.cpp:

(WTF::isLeapYear):
(WTF::msToDays):
(WTF::msToMinutes):
(WTF::msToHours):
(WTF::parseDateFromNullTerminatedCharacters):
(WTF::makeRFC2822DateString):

  • wtf/DateMath.h:
Location:
trunk/Source/JavaScriptCore
Files:
1 added
15 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/CMakeLists.txt

    r100081 r100205  
    147147    runtime/JSByteArray.cpp
    148148    runtime/JSCell.cpp
     149    runtime/JSDateMath.cpp
    149150    runtime/JSFunction.cpp
    150151    runtime/JSBoundFunction.cpp
  • trunk/Source/JavaScriptCore/ChangeLog

    r100202 r100205  
     12011-11-14  Adam Barth  <abarth@webkit.org>
     2
     3        DateMath.cpp should not depend on JavaScriptCore
     4        https://bugs.webkit.org/show_bug.cgi?id=71747
     5
     6        Reviewed by Darin Adler.
     7
     8        This patch moves the JSC-specific parts of DateMath into JSDateMath in
     9        JavaScriptCore.  There shouldn't be any behavior change.
     10
     11        * CMakeLists.txt:
     12        * GNUmakefile.list.am:
     13        * JavaScriptCore.gypi:
     14        * JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
     15        * JavaScriptCore.xcodeproj/project.pbxproj:
     16        * Target.pri:
     17        * runtime/DateConstructor.cpp:
     18        * runtime/DateConversion.cpp:
     19        * runtime/DateInstance.cpp:
     20        * runtime/DateInstanceCache.h:
     21        * runtime/DatePrototype.cpp:
     22        * runtime/InitializeThreading.cpp:
     23        * runtime/JSDateMath.cpp: Copied from Source/JavaScriptCore/wtf/DateMath.cpp.
     24        (JSC::timeToMS):
     25        (JSC::msToSeconds):
     26        * runtime/JSDateMath.h: Copied from Source/JavaScriptCore/wtf/DateMath.h.
     27        * wtf/DateMath.cpp:
     28        (WTF::isLeapYear):
     29        (WTF::msToDays):
     30        (WTF::msToMinutes):
     31        (WTF::msToHours):
     32        (WTF::parseDateFromNullTerminatedCharacters):
     33        (WTF::makeRFC2822DateString):
     34        * wtf/DateMath.h:
     35
    1362011-11-14  Michael Saboff  <msaboff@apple.com>
    237
  • trunk/Source/JavaScriptCore/GNUmakefile.list.am

    r100115 r100205  
    381381        Source/JavaScriptCore/runtime/JSCell.cpp \
    382382        Source/JavaScriptCore/runtime/JSCell.h \
     383        Source/JavaScriptCore/runtime/JSDateMath.cpp \
     384        Source/JavaScriptCore/runtime/JSDateMath.h \
    383385        Source/JavaScriptCore/runtime/JSFunction.cpp \
    384386        Source/JavaScriptCore/runtime/JSFunction.h \
  • trunk/Source/JavaScriptCore/JavaScriptCore.gypi

    r98909 r100205  
    8484            'runtime/JSByteArray.h',
    8585            'runtime/JSCell.h',
     86            'runtime/JSDateMath.h',
    8687            'runtime/JSFunction.h',
    8788            'runtime/JSBoundFunction.h',
     
    478479            'runtime/JSByteArray.cpp',
    479480            'runtime/JSCell.cpp',
     481            'runtime/JSDateMath.cpp',
    480482            'runtime/JSFunction.cpp',
    481483            'runtime/JSBoundFunction.cpp',
  • trunk/Source/JavaScriptCore/JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj

    r98909 r100205  
    799799                        </File>
    800800                        <File
     801                                RelativePath="..\..\runtime\JSDateMath.cpp"
     802                                >
     803                        </File>
     804                        <File
     805                                RelativePath="..\..\runtime\JSDateMath.h"
     806                                >
     807                        </File>
     808                        <File
    801809                                RelativePath="..\..\runtime\JSFunction.cpp"
    802810                                >
  • trunk/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj

    r100197 r100205  
    439439                96DD73790F9DA3100027FBCC /* VMTags.h in Headers */ = {isa = PBXBuildFile; fileRef = 96DD73780F9DA3100027FBCC /* VMTags.h */; settings = {ATTRIBUTES = (Private, ); }; };
    440440                971EDEA61169E0D3005E4262 /* Terminator.h in Headers */ = {isa = PBXBuildFile; fileRef = 97F6903A1169DF7F00A6BB46 /* Terminator.h */; settings = {ATTRIBUTES = (Private, ); }; };
     441                978801401471AD920041B016 /* JSDateMath.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9788FC221471AD0C0068CE2D /* JSDateMath.cpp */; };
     442                978801411471AD920041B016 /* JSDateMath.h in Headers */ = {isa = PBXBuildFile; fileRef = 9788FC231471AD0C0068CE2D /* JSDateMath.h */; settings = {ATTRIBUTES = (Private, ); }; };
    441443                97941A5713029AAB004A3447 /* OSRandomSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 97941A3F130299DB004A3447 /* OSRandomSource.cpp */; };
    442444                97941A5A13029ACC004A3447 /* OSRandomSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 97941A40130299DB004A3447 /* OSRandomSource.h */; };
     
    12481250                969A09220ED1E09C00F1F681 /* Completion.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Completion.cpp; sourceTree = "<group>"; };
    12491251                96DD73780F9DA3100027FBCC /* VMTags.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VMTags.h; sourceTree = "<group>"; };
     1252                9788FC221471AD0C0068CE2D /* JSDateMath.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSDateMath.cpp; sourceTree = "<group>"; };
     1253                9788FC231471AD0C0068CE2D /* JSDateMath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSDateMath.h; sourceTree = "<group>"; };
    12501254                97941A3F130299DB004A3447 /* OSRandomSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = OSRandomSource.cpp; sourceTree = "<group>"; };
    12511255                97941A40130299DB004A3447 /* OSRandomSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OSRandomSource.h; sourceTree = "<group>"; };
     
    21412145                                BC7F8FBA0E19D1EF008632C0 /* JSCell.cpp */,
    21422146                                BC1167D80E19BCC9008066DD /* JSCell.h */,
     2147                                9788FC221471AD0C0068CE2D /* JSDateMath.cpp */,
     2148                                9788FC231471AD0C0068CE2D /* JSDateMath.h */,
    21432149                                F692A85E0255597D01FF60F7 /* JSFunction.cpp */,
    21442150                                F692A85F0255597D01FF60F7 /* JSFunction.h */,
     
    29342940                                0F431738146BAC69007E3890 /* ListableHandler.h in Headers */,
    29352941                                0F5F08CF146C7633000472A9 /* UnconditionalFinalizer.h in Headers */,
     2942                                978801411471AD920041B016 /* JSDateMath.h in Headers */,
    29362943                        );
    29372944                        runOnlyForDeploymentPostprocessing = 0;
     
    34713478                                0FC0979E146B272100CF2442 /* DFGCorrectableJumpPoint.cpp in Sources */,
    34723479                                0FC097A1146B28CA00CF2442 /* DFGThunks.cpp in Sources */,
     3480                                978801401471AD920041B016 /* JSDateMath.cpp in Sources */,
    34733481                        );
    34743482                        runOnlyForDeploymentPostprocessing = 0;
  • trunk/Source/JavaScriptCore/Target.pri

    r99787 r100205  
    165165    runtime/JSByteArray.cpp \
    166166    runtime/JSCell.cpp \
     167    runtime/JSDateMath.cpp \
    167168    runtime/JSFunction.cpp \
    168169    runtime/JSBoundFunction.cpp \
  • trunk/Source/JavaScriptCore/runtime/DateConstructor.cpp

    r100006 r100205  
    2626#include "DateInstance.h"
    2727#include "DatePrototype.h"
     28#include "JSDateMath.h"
    2829#include "JSFunction.h"
    2930#include "JSGlobalObject.h"
     
    3334#include <math.h>
    3435#include <time.h>
    35 #include <wtf/DateMath.h>
    3636#include <wtf/MathExtras.h>
    3737
  • trunk/Source/JavaScriptCore/runtime/DateConversion.cpp

    r81040 r100205  
    4545
    4646#include "CallFrame.h"
     47#include "JSDateMath.h"
    4748#include "JSObject.h"
    4849#include "ScopeChain.h"
    4950#include "UString.h"
    50 #include <wtf/DateMath.h>
    5151#include <wtf/StringExtras.h>
    5252#include <wtf/text/CString.h>
  • trunk/Source/JavaScriptCore/runtime/DateInstance.cpp

    r95936 r100205  
    2323#include "DateInstance.h"
    2424
     25#include "JSDateMath.h"
    2526#include "JSGlobalObject.h"
    2627
    2728#include <math.h>
    28 #include <wtf/DateMath.h>
    2929#include <wtf/MathExtras.h>
    3030
  • trunk/Source/JavaScriptCore/runtime/DateInstanceCache.h

    r95901 r100205  
    2727#define DateInstanceCache_h
    2828
    29 #include <wtf/DateMath.h>
     29#include "JSDateMath.h"
    3030#include <wtf/FixedArray.h>
    3131#include <wtf/HashFunctions.h>
  • trunk/Source/JavaScriptCore/runtime/DatePrototype.cpp

    r100037 r100205  
    2828#include "DateInstance.h"
    2929#include "Error.h"
     30#include "JSDateMath.h"
    3031#include "JSGlobalObject.h"
    3132#include "JSString.h"
     
    4445#include <time.h>
    4546#include <wtf/Assertions.h>
    46 #include <wtf/DateMath.h>
    4747#include <wtf/MathExtras.h>
    4848#include <wtf/StringExtras.h>
  • trunk/Source/JavaScriptCore/runtime/InitializeThreading.cpp

    r96463 r100205  
    3434#include "Heuristics.h"
    3535#include "Identifier.h"
     36#include "JSDateMath.h"
    3637#include "JSGlobalObject.h"
    3738#include "UString.h"
    3839#include "WriteBarrier.h"
    3940#include "dtoa.h"
    40 #include <wtf/DateMath.h>
    4141#include <wtf/Threading.h>
    4242#include <wtf/dtoa/cached-powers.h>
  • trunk/Source/JavaScriptCore/runtime/JSDateMath.h

    r100204 r100205  
    4141 */
    4242
    43 #ifndef DateMath_h
    44 #define DateMath_h
     43#ifndef JSDateMath_h
     44#define JSDateMath_h
    4545
    46 #include <math.h>
    47 #include <stdint.h>
    48 #include <string.h>
    49 #include <time.h>
    50 #include <wtf/CurrentTime.h>
    51 #include <wtf/Noncopyable.h>
    52 #include <wtf/OwnArrayPtr.h>
    53 #include <wtf/PassOwnArrayPtr.h>
    54 #include <wtf/text/WTFString.h>
    55 #include <wtf/UnusedParam.h>
     46#include <wtf/DateMath.h>
    5647
    57 namespace WTF {
    58 void initializeDates();
    59 int equivalentYearForDST(int year);
     48namespace JSC {
    6049
    61 // Not really math related, but this is currently the only shared place to put these.
    62 double parseES5DateFromNullTerminatedCharacters(const char* dateString);
    63 double parseDateFromNullTerminatedCharacters(const char* dateString);
    64 double timeClip(double);
    65 // dayOfWeek: [0, 6] 0 being Monday, day: [1, 31], month: [0, 11], year: ex: 2011, hours: [0, 23], minutes: [0, 59], seconds: [0, 59], utcOffset: [-720,720].
    66 String makeRFC2822DateString(unsigned dayOfWeek, unsigned day, unsigned month, unsigned year, unsigned hours, unsigned minutes, unsigned seconds, int utcOffset);
    67 
    68 inline double jsCurrentTime()
    69 {
    70     // JavaScript doesn't recognize fractions of a millisecond.
    71     return floor(WTF::currentTimeMS());
    72 }
    73 
    74 const char * const weekdayName[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
    75 const char * const monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    76 
    77 const double hoursPerDay = 24.0;
    78 const double minutesPerHour = 60.0;
    79 const double secondsPerHour = 60.0 * 60.0;
    80 const double secondsPerMinute = 60.0;
    81 const double msPerSecond = 1000.0;
    82 const double msPerMinute = 60.0 * 1000.0;
    83 const double msPerHour = 60.0 * 60.0 * 1000.0;
    84 const double msPerDay = 24.0 * 60.0 * 60.0 * 1000.0;
    85 const double msPerMonth = 2592000000.0;
    86 
    87 // Returns the number of days from 1970-01-01 to the specified date.
    88 double dateToDaysFrom1970(int year, int month, int day);
    89 int msToYear(double ms);
    90 int dayInYear(double ms, int year);
    91 int monthFromDayInYear(int dayInYear, bool leapYear);
    92 int dayInMonthFromDayInYear(int dayInYear, bool leapYear);
    93 
    94 // Returns offset milliseconds for UTC and DST.
    95 int32_t calculateUTCOffset();
    96 double calculateDSTOffset(double ms, double utcOffset);
    97 
    98 } // namespace WTF
    99 
    100 using WTF::adoptArrayPtr;
    101 using WTF::dateToDaysFrom1970;
    102 using WTF::dayInMonthFromDayInYear;
    103 using WTF::dayInYear;
    104 using WTF::minutesPerHour;
    105 using WTF::monthFromDayInYear;
    106 using WTF::msPerDay;
    107 using WTF::msPerMinute;
    108 using WTF::msPerSecond;
    109 using WTF::msToYear;
    110 using WTF::secondsPerMinute;
    111 using WTF::parseDateFromNullTerminatedCharacters;
    112 using WTF::makeRFC2822DateString;
    113 using WTF::calculateUTCOffset;
    114 using WTF::calculateDSTOffset;
    115 
    116 #if USE(JSC)
    117 namespace JSC {
    11850class ExecState;
    11951struct GregorianDateTime;
     
    232164    return t.utcOffset;
    233165}
     166
    234167} // namespace JSC
    235 #endif // USE(JSC)
    236168
    237 #endif // DateMath_h
     169#endif // JSDateMath_h
  • trunk/Source/JavaScriptCore/wtf/DateMath.cpp

    r95555 r100205  
    7676#include "ASCIICType.h"
    7777#include "CurrentTime.h"
    78 #if USE(JSC)
    79 #include "JSObject.h"
    80 #endif
    8178#include "MathExtras.h"
    82 #if USE(JSC)
    83 #include "ScopeChain.h"
    84 #endif
    8579#include "StdLibExtras.h"
    8680#include "StringExtras.h"
     
    110104#endif
    111105
    112 #if USE(JSC)
    113 #include "CallFrame.h"
    114 #endif
    115 
    116106using namespace WTF;
    117107
     
    139129};
    140130
    141 static inline bool isLeapYear(int year)
     131bool isLeapYear(int year)
    142132{
    143133    if (year % 4 != 0)
     
    174164}
    175165
    176 static inline double msToDays(double ms)
     166double msToDays(double ms)
    177167{
    178168    return floor(ms / msPerDay);
     
    211201}
    212202
    213 // 0: Sunday, 1: Monday, etc.
    214 static inline int msToWeekDay(double ms)
    215 {
    216     int wd = (static_cast<int>(msToDays(ms)) + 4) % 7;
    217     if (wd < 0)
    218         wd += 7;
    219     return wd;
    220 }
    221 
    222 static inline int msToSeconds(double ms)
    223 {
    224     double result = fmod(floor(ms / msPerSecond), secondsPerMinute);
    225     if (result < 0)
    226         result += secondsPerMinute;
    227     return static_cast<int>(result);
    228 }
    229 
    230 static inline int msToMinutes(double ms)
     203int msToMinutes(double ms)
    231204{
    232205    double result = fmod(floor(ms / msPerMinute), minutesPerHour);
     
    236209}
    237210
    238 static inline int msToHours(double ms)
     211int msToHours(double ms)
    239212{
    240213    double result = fmod(floor(ms/msPerHour), hoursPerDay);
     
    320293}
    321294
    322 static inline double timeToMS(double hour, double min, double sec, double ms)
    323 {
    324     return (((hour * minutesPerHour + min) * secondsPerMinute + sec) * msPerSecond + ms);
    325 }
    326 
    327295double dateToDaysFrom1970(int year, int month, int day)
    328296{
     
    725693
    726694// Odd case where 'exec' is allowed to be 0, to accomodate a caller in WebCore.
    727 static double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveTZ, int& offset)
     695double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveTZ, int& offset)
    728696{
    729697    haveTZ = false;
     
    10491017    return stringBuilder.toString();
    10501018}
     1019
    10511020} // namespace WTF
    1052 
    1053 #if USE(JSC)
    1054 namespace JSC {
    1055 
    1056 // Get the DST offset for the time passed in.
    1057 //
    1058 // NOTE: The implementation relies on the fact that no time zones have
    1059 // more than one daylight savings offset change per month.
    1060 // If this function is called with NaN it returns NaN.
    1061 static double getDSTOffset(ExecState* exec, double ms, double utcOffset)
    1062 {
    1063     DSTOffsetCache& cache = exec->globalData().dstOffsetCache;
    1064     double start = cache.start;
    1065     double end = cache.end;
    1066 
    1067     if (start <= ms) {
    1068         // If the time fits in the cached interval, return the cached offset.
    1069         if (ms <= end) return cache.offset;
    1070 
    1071         // Compute a possible new interval end.
    1072         double newEnd = end + cache.increment;
    1073 
    1074         if (ms <= newEnd) {
    1075             double endOffset = calculateDSTOffset(newEnd, utcOffset);
    1076             if (cache.offset == endOffset) {
    1077                 // If the offset at the end of the new interval still matches
    1078                 // the offset in the cache, we grow the cached time interval
    1079                 // and return the offset.
    1080                 cache.end = newEnd;
    1081                 cache.increment = msPerMonth;
    1082                 return endOffset;
    1083             } else {
    1084                 double offset = calculateDSTOffset(ms, utcOffset);
    1085                 if (offset == endOffset) {
    1086                     // The offset at the given time is equal to the offset at the
    1087                     // new end of the interval, so that means that we've just skipped
    1088                     // the point in time where the DST offset change occurred. Updated
    1089                     // the interval to reflect this and reset the increment.
    1090                     cache.start = ms;
    1091                     cache.end = newEnd;
    1092                     cache.increment = msPerMonth;
    1093                 } else {
    1094                     // The interval contains a DST offset change and the given time is
    1095                     // before it. Adjust the increment to avoid a linear search for
    1096                     // the offset change point and change the end of the interval.
    1097                     cache.increment /= 3;
    1098                     cache.end = ms;
    1099                 }
    1100                 // Update the offset in the cache and return it.
    1101                 cache.offset = offset;
    1102                 return offset;
    1103             }
    1104         }
    1105     }
    1106 
    1107     // Compute the DST offset for the time and shrink the cache interval
    1108     // to only contain the time. This allows fast repeated DST offset
    1109     // computations for the same time.
    1110     double offset = calculateDSTOffset(ms, utcOffset);
    1111     cache.offset = offset;
    1112     cache.start = ms;
    1113     cache.end = ms;
    1114     cache.increment = msPerMonth;
    1115     return offset;
    1116 }
    1117 
    1118 /*
    1119  * Get the difference in milliseconds between this time zone and UTC (GMT)
    1120  * NOT including DST.
    1121  */
    1122 double getUTCOffset(ExecState* exec)
    1123 {
    1124     double utcOffset = exec->globalData().cachedUTCOffset;
    1125     if (!isnan(utcOffset))
    1126         return utcOffset;
    1127     exec->globalData().cachedUTCOffset = calculateUTCOffset();
    1128     return exec->globalData().cachedUTCOffset;
    1129 }
    1130 
    1131 double gregorianDateTimeToMS(ExecState* exec, const GregorianDateTime& t, double milliSeconds, bool inputIsUTC)
    1132 {
    1133     double day = dateToDaysFrom1970(t.year + 1900, t.month, t.monthDay);
    1134     double ms = timeToMS(t.hour, t.minute, t.second, milliSeconds);
    1135     double result = (day * WTF::msPerDay) + ms;
    1136 
    1137     if (!inputIsUTC) { // convert to UTC
    1138         double utcOffset = getUTCOffset(exec);
    1139         result -= utcOffset;
    1140         result -= getDSTOffset(exec, result, utcOffset);
    1141     }
    1142 
    1143     return result;
    1144 }
    1145 
    1146 // input is UTC
    1147 void msToGregorianDateTime(ExecState* exec, double ms, bool outputIsUTC, GregorianDateTime& tm)
    1148 {
    1149     double dstOff = 0.0;
    1150     double utcOff = 0.0;
    1151     if (!outputIsUTC) {
    1152         utcOff = getUTCOffset(exec);
    1153         dstOff = getDSTOffset(exec, ms, utcOff);
    1154         ms += dstOff + utcOff;
    1155     }
    1156 
    1157     const int year = msToYear(ms);
    1158     tm.second   =  msToSeconds(ms);
    1159     tm.minute   =  msToMinutes(ms);
    1160     tm.hour     =  msToHours(ms);
    1161     tm.weekDay  =  msToWeekDay(ms);
    1162     tm.yearDay  =  dayInYear(ms, year);
    1163     tm.monthDay =  dayInMonthFromDayInYear(tm.yearDay, isLeapYear(year));
    1164     tm.month    =  monthFromDayInYear(tm.yearDay, isLeapYear(year));
    1165     tm.year     =  year - 1900;
    1166     tm.isDST    =  dstOff != 0.0;
    1167     tm.utcOffset = static_cast<long>((dstOff + utcOff) / WTF::msPerSecond);
    1168     tm.timeZone = nullptr;
    1169 }
    1170 
    1171 double parseDateFromNullTerminatedCharacters(ExecState* exec, const char* dateString)
    1172 {
    1173     ASSERT(exec);
    1174     bool haveTZ;
    1175     int offset;
    1176     double ms = WTF::parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset);
    1177     if (isnan(ms))
    1178         return std::numeric_limits<double>::quiet_NaN();
    1179 
    1180     // fall back to local timezone
    1181     if (!haveTZ) {
    1182         double utcOffset = getUTCOffset(exec);
    1183         double dstOffset = getDSTOffset(exec, ms, utcOffset);
    1184         offset = static_cast<int>((utcOffset + dstOffset) / WTF::msPerMinute);
    1185     }
    1186     return ms - (offset * WTF::msPerMinute);
    1187 }
    1188 
    1189 } // namespace JSC
    1190 #endif // USE(JSC)
  • trunk/Source/JavaScriptCore/wtf/DateMath.h

    r87958 r100205  
    5656
    5757namespace WTF {
     58
    5859void initializeDates();
    5960int equivalentYearForDST(int year);
     
    6263double parseES5DateFromNullTerminatedCharacters(const char* dateString);
    6364double parseDateFromNullTerminatedCharacters(const char* dateString);
     65double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveTZ, int& offset);
    6466double timeClip(double);
    6567// dayOfWeek: [0, 6] 0 being Monday, day: [1, 31], month: [0, 11], year: ex: 2011, hours: [0, 23], minutes: [0, 59], seconds: [0, 59], utcOffset: [-720,720].
     
    7274}
    7375
    74 const char * const weekdayName[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
    75 const char * const monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
     76const char* const weekdayName[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
     77const char* const monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    7678
    7779const double hoursPerDay = 24.0;
     
    8587const double msPerMonth = 2592000000.0;
    8688
     89bool isLeapYear(int year);
     90
    8791// Returns the number of days from 1970-01-01 to the specified date.
    8892double dateToDaysFrom1970(int year, int month, int day);
    8993int msToYear(double ms);
     94double msToDays(double ms);
     95int msToMinutes(double ms);
     96int msToHours(double ms);
    9097int dayInYear(double ms, int year);
    9198int monthFromDayInYear(int dayInYear, bool leapYear);
     
    98105} // namespace WTF
    99106
    100 using WTF::adoptArrayPtr;
     107using WTF::isLeapYear;
    101108using WTF::dateToDaysFrom1970;
    102109using WTF::dayInMonthFromDayInYear;
     
    108115using WTF::msPerSecond;
    109116using WTF::msToYear;
     117using WTF::msToDays;
     118using WTF::msToMinutes;
     119using WTF::msToHours;
    110120using WTF::secondsPerMinute;
    111121using WTF::parseDateFromNullTerminatedCharacters;
     
    114124using WTF::calculateDSTOffset;
    115125
    116 #if USE(JSC)
    117 namespace JSC {
    118 class ExecState;
    119 struct GregorianDateTime;
    120 
    121 void msToGregorianDateTime(ExecState*, double, bool outputIsUTC, GregorianDateTime&);
    122 double gregorianDateTimeToMS(ExecState*, const GregorianDateTime&, double, bool inputIsUTC);
    123 double getUTCOffset(ExecState*);
    124 double parseDateFromNullTerminatedCharacters(ExecState*, const char* dateString);
    125 
    126 // Intentionally overridding the default tm of the system.
    127 // The members of tm differ on various operating systems.
    128 struct GregorianDateTime {
    129     WTF_MAKE_NONCOPYABLE(GregorianDateTime);
    130 public:
    131     GregorianDateTime()
    132         : second(0)
    133         , minute(0)
    134         , hour(0)
    135         , weekDay(0)
    136         , monthDay(0)
    137         , yearDay(0)
    138         , month(0)
    139         , year(0)
    140         , isDST(0)
    141         , utcOffset(0)
    142     {
    143     }
    144 
    145     GregorianDateTime(ExecState* exec, const tm& inTm)
    146         : second(inTm.tm_sec)
    147         , minute(inTm.tm_min)
    148         , hour(inTm.tm_hour)
    149         , weekDay(inTm.tm_wday)
    150         , monthDay(inTm.tm_mday)
    151         , yearDay(inTm.tm_yday)
    152         , month(inTm.tm_mon)
    153         , year(inTm.tm_year)
    154         , isDST(inTm.tm_isdst)
    155     {
    156         UNUSED_PARAM(exec);
    157 #if HAVE(TM_GMTOFF)
    158         utcOffset = static_cast<int>(inTm.tm_gmtoff);
    159 #else
    160         utcOffset = static_cast<int>(getUTCOffset(exec) / WTF::msPerSecond + (isDST ? WTF::secondsPerHour : 0));
    161 #endif
    162 
    163 #if HAVE(TM_ZONE)
    164         int inZoneSize = strlen(inTm.tm_zone) + 1;
    165         timeZone = adoptArrayPtr(new char[inZoneSize]);
    166         strncpy(timeZone.get(), inTm.tm_zone, inZoneSize);
    167 #else
    168         timeZone = nullptr;
    169 #endif
    170     }
    171 
    172     operator tm() const
    173     {
    174         tm ret;
    175         memset(&ret, 0, sizeof(ret));
    176 
    177         ret.tm_sec   =  second;
    178         ret.tm_min   =  minute;
    179         ret.tm_hour  =  hour;
    180         ret.tm_wday  =  weekDay;
    181         ret.tm_mday  =  monthDay;
    182         ret.tm_yday  =  yearDay;
    183         ret.tm_mon   =  month;
    184         ret.tm_year  =  year;
    185         ret.tm_isdst =  isDST;
    186 
    187 #if HAVE(TM_GMTOFF)
    188         ret.tm_gmtoff = static_cast<long>(utcOffset);
    189 #endif
    190 #if HAVE(TM_ZONE)
    191         ret.tm_zone = timeZone.get();
    192 #endif
    193 
    194         return ret;
    195     }
    196 
    197     void copyFrom(const GregorianDateTime& rhs)
    198     {
    199         second = rhs.second;
    200         minute = rhs.minute;
    201         hour = rhs.hour;
    202         weekDay = rhs.weekDay;
    203         monthDay = rhs.monthDay;
    204         yearDay = rhs.yearDay;
    205         month = rhs.month;
    206         year = rhs.year;
    207         isDST = rhs.isDST;
    208         utcOffset = rhs.utcOffset;
    209         if (rhs.timeZone) {
    210             int inZoneSize = strlen(rhs.timeZone.get()) + 1;
    211             timeZone = adoptArrayPtr(new char[inZoneSize]);
    212             strncpy(timeZone.get(), rhs.timeZone.get(), inZoneSize);
    213         } else
    214             timeZone = nullptr;
    215     }
    216 
    217     int second;
    218     int minute;
    219     int hour;
    220     int weekDay;
    221     int monthDay;
    222     int yearDay;
    223     int month;
    224     int year;
    225     int isDST;
    226     int utcOffset;
    227     OwnArrayPtr<char> timeZone;
    228 };
    229 
    230 static inline int gmtoffset(const GregorianDateTime& t)
    231 {
    232     return t.utcOffset;
    233 }
    234 } // namespace JSC
    235 #endif // USE(JSC)
    236 
    237126#endif // DateMath_h
Note: See TracChangeset for help on using the changeset viewer.