OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #ifndef V8_DATE_H_ |
| 29 #define V8_DATE_H_ |
| 30 |
| 31 #include "allocation.h" |
| 32 #include "globals.h" |
| 33 #include "platform.h" |
| 34 |
| 35 |
| 36 namespace v8 { |
| 37 namespace internal { |
| 38 |
| 39 class DateCache BASE_EMBEDDED { |
| 40 public: |
| 41 static const int kMsPerMin = 60 * 1000; |
| 42 static const int kSecPerDay = 24 * 60 * 60; |
| 43 static const int64_t kMsPerDay = kSecPerDay * 1000; |
| 44 |
| 45 // The largest time that can be passed to OS date-time library functions. |
| 46 static const int kMaxEpochTimeInSec = 2147483647; |
| 47 static const int64_t kMaxEpochTimeInMs = |
| 48 static_cast<int64_t>(2147483647) * 1000; |
| 49 |
| 50 // The largest time that can be stored in JSDate. |
| 51 static const int64_t kMaxTimeInMs = |
| 52 static_cast<int64_t>(864000000) * 10000000; |
| 53 |
| 54 // Sentinel that denotes an invalid local offset. |
| 55 // Assume that no local offset exceeds ten days. |
| 56 |
| 57 static const int kInvalidLocalOffsetInMs = 10 * kMsPerDay; |
| 58 // Sentinel that denotes an invalid cache stamp. |
| 59 // It is an invariant of DateCache that cache stamp is non-negative. |
| 60 static const int kInvalidStamp = -1; |
| 61 |
| 62 DateCache() : stamp_(0) { |
| 63 ResetDateCache(); |
| 64 counter_ = 0; |
| 65 } |
| 66 |
| 67 virtual ~DateCache() {} |
| 68 |
| 69 |
| 70 // Clears cached timezone information and increments the cache stamp. |
| 71 void ResetDateCache(); |
| 72 |
| 73 |
| 74 // Computes floor(time_ms / kMsPerDay). |
| 75 static int DaysFromTime(int64_t time_ms) { |
| 76 if (time_ms < 0) time_ms -= (kMsPerDay - 1); |
| 77 return static_cast<int>(time_ms / kMsPerDay); |
| 78 } |
| 79 |
| 80 |
| 81 // Computes modulo(time_ms, kMsPerDay) given that |
| 82 // days = floor(time_ms / kMsPerDay). |
| 83 static int TimeInDay(int64_t time_ms, int days) { |
| 84 return static_cast<int>(time_ms - days * kMsPerDay); |
| 85 } |
| 86 |
| 87 |
| 88 // Given the number of days since the epoch, computes the weekday. |
| 89 // ECMA 262 - 15.9.1.6. |
| 90 int Weekday(int days) { |
| 91 return (days + 4 + 7) % 7; |
| 92 } |
| 93 |
| 94 |
| 95 bool IsLeap(int year) { |
| 96 return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); |
| 97 } |
| 98 |
| 99 |
| 100 // ECMA 262 - 15.9.1.7. |
| 101 int LocalOffsetInMs() { |
| 102 if (local_offset_ms_ == kInvalidLocalOffsetInMs) { |
| 103 local_offset_ms_ = GetLocalOffsetFromOS(); |
| 104 } |
| 105 return local_offset_ms_; |
| 106 } |
| 107 |
| 108 |
| 109 const char* LocalTimezone(int64_t time_ms) { |
| 110 if (time_ms < 0 || time_ms > kMaxEpochTimeInMs) { |
| 111 time_ms = EquivalentTime(time_ms); |
| 112 } |
| 113 return OS::LocalTimezone(static_cast<double>(time_ms)); |
| 114 } |
| 115 |
| 116 // ECMA 262 - 15.9.5.26 |
| 117 int TimezoneOffset(int64_t time_ms) { |
| 118 return (LocalOffsetInMs() + DaylightSavingsOffsetInMs(time_ms)) / kMsPerMin; |
| 119 } |
| 120 |
| 121 // ECMA 262 - 15.9.1.9 |
| 122 int64_t ToLocal(int64_t time_ms) { |
| 123 return time_ms + LocalOffsetInMs() + DaylightSavingsOffsetInMs(time_ms); |
| 124 } |
| 125 |
| 126 // ECMA 262 - 15.9.1.9 |
| 127 int64_t ToUTC(int64_t time_ms) { |
| 128 time_ms -= LocalOffsetInMs(); |
| 129 return time_ms - DaylightSavingsOffsetInMs(time_ms); |
| 130 } |
| 131 |
| 132 |
| 133 // Computes a time equivalent to the given time according |
| 134 // to ECMA 262 - 15.9.1.9. |
| 135 // The issue here is that some library calls don't work right for dates |
| 136 // that cannot be represented using a non-negative signed 32 bit integer |
| 137 // (measured in whole seconds based on the 1970 epoch). |
| 138 // We solve this by mapping the time to a year with same leap-year-ness |
| 139 // and same starting day for the year. The ECMAscript specification says |
| 140 // we must do this, but for compatibility with other browsers, we use |
| 141 // the actual year if it is in the range 1970..2037 |
| 142 int64_t EquivalentTime(int64_t time_ms) { |
| 143 int days = DaysFromTime(time_ms); |
| 144 int time_within_day_ms = static_cast<int>(time_ms - days * kMsPerDay); |
| 145 int year, month, day; |
| 146 YearMonthDayFromDays(days, &year, &month, &day); |
| 147 int new_days = DaysFromYearMonth(EquivalentYear(year), month) + day - 1; |
| 148 return static_cast<int64_t>(new_days) * kMsPerDay + time_within_day_ms; |
| 149 } |
| 150 |
| 151 // Returns an equivalent year in the range [2008-2035] matching |
| 152 // - leap year, |
| 153 // - week day of first day. |
| 154 // ECMA 262 - 15.9.1.9. |
| 155 int EquivalentYear(int year) { |
| 156 int week_day = Weekday(DaysFromYearMonth(year, 0)); |
| 157 int recent_year = ((IsLeap(year) ? 1956 : 1967) + week_day * 12) % 28; |
| 158 // Find the year in the range 2008..2037 that is equivalent mod 28. |
| 159 // Add 3*28 to give a positive argument to the modulus operator. |
| 160 return 2008 + (recent_year + 3 * 28 - 2008) % 28; |
| 161 } |
| 162 |
| 163 // Given the number of days since the epoch, computes |
| 164 // the corresponding year, month, and day. |
| 165 void YearMonthDayFromDays(int days, int* year, int* month, int* day); |
| 166 |
| 167 // Computes the number of days since the epoch for |
| 168 // the first day of the given month in the given year. |
| 169 int DaysFromYearMonth(int year, int month); |
| 170 |
| 171 // Cache stamp is used for invalidating caches in JSDate. |
| 172 // We increment the stamp each time when the timezone information changes. |
| 173 // JSDate objects perform stamp check and invalidate their caches if |
| 174 // their saved stamp is not equal to the current stamp. |
| 175 Smi* stamp() { return stamp_; } |
| 176 void* stamp_address() { return &stamp_; } |
| 177 |
| 178 protected: |
| 179 // These functions are virtual so that we can override them when testing. |
| 180 virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) { |
| 181 int result = static_cast<int>(OS::DaylightSavingsOffset(time_sec * 1000)); |
| 182 counter_++; |
| 183 // printf("counter %d\n", counter_); |
| 184 return result; |
| 185 } |
| 186 |
| 187 virtual int GetLocalOffsetFromOS() { |
| 188 double offset = OS::LocalTimeOffset(); |
| 189 ASSERT(offset < kInvalidLocalOffsetInMs); |
| 190 return static_cast<int>(offset); |
| 191 } |
| 192 |
| 193 private: |
| 194 // The implementation relies on the fact that no time zones have |
| 195 // more than one daylight savings offset change per 19 days. |
| 196 // In Egypt in 2010 they decided to suspend DST during Ramadan. This |
| 197 // led to a short interval where DST is in effect from September 10 to |
| 198 // September 30. |
| 199 static const int kDefaultDSTDeltaInSec = 19 * kSecPerDay; |
| 200 |
| 201 // Size of the Daylight Savings Time cache. |
| 202 static const int kDSTSize = 32; |
| 203 |
| 204 // Daylight Savings Time segment stores a segment of time where |
| 205 // daylight savings offset does not change. |
| 206 struct DST { |
| 207 int start_sec; |
| 208 int end_sec; |
| 209 int offset_ms; |
| 210 int last_used; |
| 211 }; |
| 212 |
| 213 // Computes the daylight savings offset for the given time. |
| 214 // ECMA 262 - 15.9.1.8 |
| 215 int DaylightSavingsOffsetInMs(int64_t time_ms); |
| 216 |
| 217 // Sets the before_ and the after_ segments from the DST cache such that |
| 218 // the before_ segment starts earlier than the given time and |
| 219 // the after_ segment start later than the given time. |
| 220 // Both segments might be invalid. |
| 221 void ProbeDST(int time_sec); |
| 222 |
| 223 // Finds the least recently used segment from the DST cache that is not |
| 224 // equal to the given 'skip' segment. |
| 225 DST* LeastRecentlyUsedDST(DST* skip); |
| 226 |
| 227 // Extends the after_ segment with the given point or resets it |
| 228 // if it starts later than the given time + kDefaultDSTDeltaInSec. |
| 229 inline void ExtendTheAfterSegment(int time_sec, int offset_ms); |
| 230 |
| 231 // Makes the given segment invalid. |
| 232 inline void ClearSegment(DST* segment); |
| 233 |
| 234 bool InvalidSegment(DST* segment) { |
| 235 return segment->start_sec > segment->end_sec; |
| 236 } |
| 237 |
| 238 Smi* stamp_; |
| 239 |
| 240 // Daylight Saving Time cache. |
| 241 DST dst_[kDSTSize]; |
| 242 int dst_usage_counter_; |
| 243 DST* before_; |
| 244 DST* after_; |
| 245 |
| 246 int local_offset_ms_; |
| 247 |
| 248 // Year/Month/Day cache. |
| 249 bool ymd_valid_; |
| 250 int ymd_days_; |
| 251 int ymd_year_; |
| 252 int ymd_month_; |
| 253 int ymd_day_; |
| 254 int counter_; |
| 255 }; |
| 256 |
| 257 } } // namespace v8::internal |
| 258 |
| 259 #endif |
OLD | NEW |