OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_DATE_H_ | 5 #ifndef V8_DATE_H_ |
6 #define V8_DATE_H_ | 6 #define V8_DATE_H_ |
7 | 7 |
8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
| 9 #include "src/base/platform/platform.h" |
9 #include "src/globals.h" | 10 #include "src/globals.h" |
10 #include "src/platform.h" | |
11 | 11 |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
15 | 15 |
16 class DateCache { | 16 class DateCache { |
17 public: | 17 public: |
18 static const int kMsPerMin = 60 * 1000; | 18 static const int kMsPerMin = 60 * 1000; |
19 static const int kSecPerDay = 24 * 60 * 60; | 19 static const int kSecPerDay = 24 * 60 * 60; |
20 static const int64_t kMsPerDay = kSecPerDay * 1000; | 20 static const int64_t kMsPerDay = kSecPerDay * 1000; |
(...skipping 11 matching lines...) Expand all Loading... |
32 // before UTC conversion. | 32 // before UTC conversion. |
33 static const int64_t kMaxTimeBeforeUTCInMs = | 33 static const int64_t kMaxTimeBeforeUTCInMs = |
34 kMaxTimeInMs + 10 * kMsPerDay; | 34 kMaxTimeInMs + 10 * kMsPerDay; |
35 | 35 |
36 // Sentinel that denotes an invalid local offset. | 36 // Sentinel that denotes an invalid local offset. |
37 static const int kInvalidLocalOffsetInMs = kMaxInt; | 37 static const int kInvalidLocalOffsetInMs = kMaxInt; |
38 // Sentinel that denotes an invalid cache stamp. | 38 // Sentinel that denotes an invalid cache stamp. |
39 // It is an invariant of DateCache that cache stamp is non-negative. | 39 // It is an invariant of DateCache that cache stamp is non-negative. |
40 static const int kInvalidStamp = -1; | 40 static const int kInvalidStamp = -1; |
41 | 41 |
42 DateCache() : stamp_(0), tz_cache_(OS::CreateTimezoneCache()) { | 42 DateCache() : stamp_(0), tz_cache_(base::OS::CreateTimezoneCache()) { |
43 ResetDateCache(); | 43 ResetDateCache(); |
44 } | 44 } |
45 | 45 |
46 virtual ~DateCache() { | 46 virtual ~DateCache() { |
47 OS::DisposeTimezoneCache(tz_cache_); | 47 base::OS::DisposeTimezoneCache(tz_cache_); |
48 tz_cache_ = NULL; | 48 tz_cache_ = NULL; |
49 } | 49 } |
50 | 50 |
51 | 51 |
52 // Clears cached timezone information and increments the cache stamp. | 52 // Clears cached timezone information and increments the cache stamp. |
53 void ResetDateCache(); | 53 void ResetDateCache(); |
54 | 54 |
55 | 55 |
56 // Computes floor(time_ms / kMsPerDay). | 56 // Computes floor(time_ms / kMsPerDay). |
57 static int DaysFromTime(int64_t time_ms) { | 57 static int DaysFromTime(int64_t time_ms) { |
(...skipping 28 matching lines...) Expand all Loading... |
86 local_offset_ms_ = GetLocalOffsetFromOS(); | 86 local_offset_ms_ = GetLocalOffsetFromOS(); |
87 } | 87 } |
88 return local_offset_ms_; | 88 return local_offset_ms_; |
89 } | 89 } |
90 | 90 |
91 | 91 |
92 const char* LocalTimezone(int64_t time_ms) { | 92 const char* LocalTimezone(int64_t time_ms) { |
93 if (time_ms < 0 || time_ms > kMaxEpochTimeInMs) { | 93 if (time_ms < 0 || time_ms > kMaxEpochTimeInMs) { |
94 time_ms = EquivalentTime(time_ms); | 94 time_ms = EquivalentTime(time_ms); |
95 } | 95 } |
96 return OS::LocalTimezone(static_cast<double>(time_ms), tz_cache_); | 96 return base::OS::LocalTimezone(static_cast<double>(time_ms), tz_cache_); |
97 } | 97 } |
98 | 98 |
99 // ECMA 262 - 15.9.5.26 | 99 // ECMA 262 - 15.9.5.26 |
100 int TimezoneOffset(int64_t time_ms) { | 100 int TimezoneOffset(int64_t time_ms) { |
101 int64_t local_ms = ToLocal(time_ms); | 101 int64_t local_ms = ToLocal(time_ms); |
102 return static_cast<int>((time_ms - local_ms) / kMsPerMin); | 102 return static_cast<int>((time_ms - local_ms) / kMsPerMin); |
103 } | 103 } |
104 | 104 |
105 // ECMA 262 - 15.9.1.9 | 105 // ECMA 262 - 15.9.1.9 |
106 int64_t ToLocal(int64_t time_ms) { | 106 int64_t ToLocal(int64_t time_ms) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 // Cache stamp is used for invalidating caches in JSDate. | 155 // Cache stamp is used for invalidating caches in JSDate. |
156 // We increment the stamp each time when the timezone information changes. | 156 // We increment the stamp each time when the timezone information changes. |
157 // JSDate objects perform stamp check and invalidate their caches if | 157 // JSDate objects perform stamp check and invalidate their caches if |
158 // their saved stamp is not equal to the current stamp. | 158 // their saved stamp is not equal to the current stamp. |
159 Smi* stamp() { return stamp_; } | 159 Smi* stamp() { return stamp_; } |
160 void* stamp_address() { return &stamp_; } | 160 void* stamp_address() { return &stamp_; } |
161 | 161 |
162 // These functions are virtual so that we can override them when testing. | 162 // These functions are virtual so that we can override them when testing. |
163 virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) { | 163 virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) { |
164 double time_ms = static_cast<double>(time_sec * 1000); | 164 double time_ms = static_cast<double>(time_sec * 1000); |
165 return static_cast<int>(OS::DaylightSavingsOffset(time_ms, tz_cache_)); | 165 return static_cast<int>( |
| 166 base::OS::DaylightSavingsOffset(time_ms, tz_cache_)); |
166 } | 167 } |
167 | 168 |
168 virtual int GetLocalOffsetFromOS() { | 169 virtual int GetLocalOffsetFromOS() { |
169 double offset = OS::LocalTimeOffset(tz_cache_); | 170 double offset = base::OS::LocalTimeOffset(tz_cache_); |
170 ASSERT(offset < kInvalidLocalOffsetInMs); | 171 ASSERT(offset < kInvalidLocalOffsetInMs); |
171 return static_cast<int>(offset); | 172 return static_cast<int>(offset); |
172 } | 173 } |
173 | 174 |
174 private: | 175 private: |
175 // The implementation relies on the fact that no time zones have | 176 // The implementation relies on the fact that no time zones have |
176 // more than one daylight savings offset change per 19 days. | 177 // more than one daylight savings offset change per 19 days. |
177 // In Egypt in 2010 they decided to suspend DST during Ramadan. This | 178 // In Egypt in 2010 they decided to suspend DST during Ramadan. This |
178 // led to a short interval where DST is in effect from September 10 to | 179 // led to a short interval where DST is in effect from September 10 to |
179 // September 30. | 180 // September 30. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 | 228 |
228 int local_offset_ms_; | 229 int local_offset_ms_; |
229 | 230 |
230 // Year/Month/Day cache. | 231 // Year/Month/Day cache. |
231 bool ymd_valid_; | 232 bool ymd_valid_; |
232 int ymd_days_; | 233 int ymd_days_; |
233 int ymd_year_; | 234 int ymd_year_; |
234 int ymd_month_; | 235 int ymd_month_; |
235 int ymd_day_; | 236 int ymd_day_; |
236 | 237 |
237 TimezoneCache* tz_cache_; | 238 base::TimezoneCache* tz_cache_; |
238 }; | 239 }; |
239 | 240 |
240 } } // namespace v8::internal | 241 } } // namespace v8::internal |
241 | 242 |
242 #endif | 243 #endif |
OLD | NEW |