Chromium Code Reviews| 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 { | |
| 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. | |
|
rossberg
2012/03/06 15:55:50
Why not just use MAX_INT?
ulan
2012/03/07 10:55:21
Done.
| |
| 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) { | |
|
rossberg
2012/03/06 15:55:50
If stamp_ is a Smi, this should better be Smi::Fro
ulan
2012/03/07 10:55:21
This would require including "objects-inl.h", is i
rossberg
2012/03/07 13:48:38
If that's how it has to be, then that's how it has
ulan
2012/03/07 14:39:19
Tried, but it introduces circular includes.
| |
| 63 ResetDateCache(); | |
| 64 counter_ = 0; | |
|
rossberg
2012/03/06 15:55:50
You could put this in the initializer list. (Or re
ulan
2012/03/07 10:55:21
Done.
| |
| 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 int result = (days + 4) % 7; | |
| 92 return result >= 0 ? result : result + 7; | |
| 93 } | |
| 94 | |
| 95 | |
| 96 bool IsLeap(int year) { | |
| 97 return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); | |
| 98 } | |
| 99 | |
| 100 | |
| 101 // ECMA 262 - 15.9.1.7. | |
| 102 int LocalOffsetInMs() { | |
| 103 if (local_offset_ms_ == kInvalidLocalOffsetInMs) { | |
| 104 local_offset_ms_ = GetLocalOffsetFromOS(); | |
| 105 } | |
| 106 return local_offset_ms_; | |
| 107 } | |
| 108 | |
| 109 | |
| 110 const char* LocalTimezone(int64_t time_ms) { | |
| 111 if (time_ms < 0 || time_ms > kMaxEpochTimeInMs) { | |
| 112 time_ms = EquivalentTime(time_ms); | |
|
rossberg
2012/03/06 15:55:50
Nit: wrong indentation.
ulan
2012/03/07 10:55:21
Done.
| |
| 113 } | |
| 114 return OS::LocalTimezone(static_cast<double>(time_ms)); | |
| 115 } | |
| 116 | |
| 117 // ECMA 262 - 15.9.5.26 | |
| 118 int TimezoneOffset(int64_t time_ms) { | |
| 119 int64_t local_ms = ToLocal(time_ms); | |
| 120 return static_cast<int>((time_ms - local_ms) / kMsPerMin); | |
| 121 } | |
| 122 | |
| 123 // ECMA 262 - 15.9.1.9 | |
| 124 int64_t ToLocal(int64_t time_ms) { | |
| 125 int64_t result = | |
| 126 time_ms + LocalOffsetInMs() + DaylightSavingsOffsetInMs(time_ms); | |
| 127 return result; | |
| 128 } | |
| 129 | |
| 130 // ECMA 262 - 15.9.1.9 | |
| 131 int64_t ToUTC(int64_t time_ms) { | |
| 132 time_ms -= LocalOffsetInMs(); | |
| 133 return time_ms - DaylightSavingsOffsetInMs(time_ms); | |
| 134 } | |
| 135 | |
| 136 | |
| 137 // Computes a time equivalent to the given time according | |
| 138 // to ECMA 262 - 15.9.1.9. | |
| 139 // The issue here is that some library calls don't work right for dates | |
| 140 // that cannot be represented using a non-negative signed 32 bit integer | |
| 141 // (measured in whole seconds based on the 1970 epoch). | |
| 142 // We solve this by mapping the time to a year with same leap-year-ness | |
| 143 // and same starting day for the year. The ECMAscript specification says | |
| 144 // we must do this, but for compatibility with other browsers, we use | |
| 145 // the actual year if it is in the range 1970..2037 | |
| 146 int64_t EquivalentTime(int64_t time_ms) { | |
| 147 int days = DaysFromTime(time_ms); | |
| 148 int time_within_day_ms = static_cast<int>(time_ms - days * kMsPerDay); | |
| 149 int year, month, day; | |
| 150 YearMonthDayFromDays(days, &year, &month, &day); | |
| 151 int new_days = DaysFromYearMonth(EquivalentYear(year), month) + day - 1; | |
| 152 return static_cast<int64_t>(new_days) * kMsPerDay + time_within_day_ms; | |
| 153 } | |
| 154 | |
| 155 // Returns an equivalent year in the range [2008-2035] matching | |
| 156 // - leap year, | |
| 157 // - week day of first day. | |
| 158 // ECMA 262 - 15.9.1.9. | |
| 159 int EquivalentYear(int year) { | |
| 160 int week_day = Weekday(DaysFromYearMonth(year, 0)); | |
| 161 int recent_year = ((IsLeap(year) ? 1956 : 1967) + week_day * 12) % 28; | |
| 162 // Find the year in the range 2008..2037 that is equivalent mod 28. | |
| 163 // Add 3*28 to give a positive argument to the modulus operator. | |
| 164 return 2008 + (recent_year + 3 * 28 - 2008) % 28; | |
| 165 } | |
| 166 | |
| 167 // Given the number of days since the epoch, computes | |
| 168 // the corresponding year, month, and day. | |
| 169 void YearMonthDayFromDays(int days, int* year, int* month, int* day); | |
| 170 | |
| 171 // Computes the number of days since the epoch for | |
| 172 // the first day of the given month in the given year. | |
| 173 int DaysFromYearMonth(int year, int month); | |
| 174 | |
| 175 // Cache stamp is used for invalidating caches in JSDate. | |
| 176 // We increment the stamp each time when the timezone information changes. | |
| 177 // JSDate objects perform stamp check and invalidate their caches if | |
| 178 // their saved stamp is not equal to the current stamp. | |
| 179 Smi* stamp() { return stamp_; } | |
| 180 void* stamp_address() { return &stamp_; } | |
| 181 | |
| 182 // These functions are virtual so that we can override them when testing. | |
| 183 virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) { | |
| 184 int result = static_cast<int>(OS::DaylightSavingsOffset(time_sec * 1000)); | |
| 185 counter_++; | |
| 186 // printf("counter %d\n", counter_); | |
|
rossberg
2012/03/06 15:55:50
Leftover?
ulan
2012/03/07 10:55:21
Yep. Done.
| |
| 187 return result; | |
| 188 } | |
| 189 | |
| 190 virtual int GetLocalOffsetFromOS() { | |
| 191 double offset = OS::LocalTimeOffset(); | |
| 192 ASSERT(offset < kInvalidLocalOffsetInMs); | |
| 193 return static_cast<int>(offset); | |
| 194 } | |
| 195 | |
| 196 private: | |
| 197 // The implementation relies on the fact that no time zones have | |
| 198 // more than one daylight savings offset change per 19 days. | |
| 199 // In Egypt in 2010 they decided to suspend DST during Ramadan. This | |
| 200 // led to a short interval where DST is in effect from September 10 to | |
| 201 // September 30. | |
| 202 static const int kDefaultDSTDeltaInSec = 19 * kSecPerDay; | |
| 203 | |
| 204 // Size of the Daylight Savings Time cache. | |
| 205 static const int kDSTSize = 32; | |
| 206 | |
| 207 // Daylight Savings Time segment stores a segment of time where | |
| 208 // daylight savings offset does not change. | |
| 209 struct DST { | |
| 210 int start_sec; | |
| 211 int end_sec; | |
| 212 int offset_ms; | |
| 213 int last_used; | |
| 214 }; | |
| 215 | |
| 216 // Computes the daylight savings offset for the given time. | |
| 217 // ECMA 262 - 15.9.1.8 | |
| 218 int DaylightSavingsOffsetInMs(int64_t time_ms); | |
| 219 | |
| 220 // Sets the before_ and the after_ segments from the DST cache such that | |
| 221 // the before_ segment starts earlier than the given time and | |
| 222 // the after_ segment start later than the given time. | |
| 223 // Both segments might be invalid. | |
|
rossberg
2012/03/06 15:55:50
Add that last_used counters get updated by this fu
ulan
2012/03/07 10:55:21
Done.
| |
| 224 void ProbeDST(int time_sec); | |
| 225 | |
| 226 // Finds the least recently used segment from the DST cache that is not | |
| 227 // equal to the given 'skip' segment. | |
| 228 DST* LeastRecentlyUsedDST(DST* skip); | |
| 229 | |
| 230 // Extends the after_ segment with the given point or resets it | |
| 231 // if it starts later than the given time + kDefaultDSTDeltaInSec. | |
| 232 inline void ExtendTheAfterSegment(int time_sec, int offset_ms); | |
| 233 | |
| 234 // Makes the given segment invalid. | |
| 235 inline void ClearSegment(DST* segment); | |
| 236 | |
| 237 bool InvalidSegment(DST* segment) { | |
| 238 return segment->start_sec > segment->end_sec; | |
| 239 } | |
| 240 | |
| 241 Smi* stamp_; | |
| 242 | |
| 243 // Daylight Saving Time cache. | |
| 244 DST dst_[kDSTSize]; | |
| 245 int dst_usage_counter_; | |
| 246 DST* before_; | |
| 247 DST* after_; | |
| 248 | |
| 249 int local_offset_ms_; | |
| 250 | |
| 251 // Year/Month/Day cache. | |
| 252 bool ymd_valid_; | |
| 253 int ymd_days_; | |
| 254 int ymd_year_; | |
| 255 int ymd_month_; | |
| 256 int ymd_day_; | |
| 257 int counter_; | |
|
rossberg
2012/03/06 15:55:50
What is counter_? Was that just a debugging device
ulan
2012/03/07 10:55:21
Done.
| |
| 258 }; | |
| 259 | |
| 260 } } // namespace v8::internal | |
| 261 | |
| 262 #endif | |
| OLD | NEW |