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/base/platform/platform.h" |
| 10 #include "src/base/timezone-cache.h" |
10 #include "src/globals.h" | 11 #include "src/globals.h" |
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; |
21 static const int64_t kMsPerMonth = kMsPerDay * 30; | 21 static const int64_t kMsPerMonth = kMsPerDay * 30; |
22 | 22 |
(...skipping 14 matching lines...) Expand all Loading... |
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_(base::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 base::OS::DisposeTimezoneCache(tz_cache_); | 47 delete 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 base::OS::LocalTimezone(static_cast<double>(time_ms), tz_cache_); | 96 return tz_cache_->LocalTimezone(static_cast<double>(time_ms)); |
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 // LocalTime(t) = t + LocalTZA + DaylightSavingTA(t) | 106 // LocalTime(t) = t + LocalTZA + DaylightSavingTA(t) |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 // Cache stamp is used for invalidating caches in JSDate. | 197 // Cache stamp is used for invalidating caches in JSDate. |
198 // We increment the stamp each time when the timezone information changes. | 198 // We increment the stamp each time when the timezone information changes. |
199 // JSDate objects perform stamp check and invalidate their caches if | 199 // JSDate objects perform stamp check and invalidate their caches if |
200 // their saved stamp is not equal to the current stamp. | 200 // their saved stamp is not equal to the current stamp. |
201 Smi* stamp() { return stamp_; } | 201 Smi* stamp() { return stamp_; } |
202 void* stamp_address() { return &stamp_; } | 202 void* stamp_address() { return &stamp_; } |
203 | 203 |
204 // These functions are virtual so that we can override them when testing. | 204 // These functions are virtual so that we can override them when testing. |
205 virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) { | 205 virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) { |
206 double time_ms = static_cast<double>(time_sec * 1000); | 206 double time_ms = static_cast<double>(time_sec * 1000); |
207 return static_cast<int>( | 207 return static_cast<int>(tz_cache_->DaylightSavingsOffset(time_ms)); |
208 base::OS::DaylightSavingsOffset(time_ms, tz_cache_)); | |
209 } | 208 } |
210 | 209 |
211 virtual int GetLocalOffsetFromOS() { | 210 virtual int GetLocalOffsetFromOS() { |
212 double offset = base::OS::LocalTimeOffset(tz_cache_); | 211 double offset = tz_cache_->LocalTimeOffset(); |
213 DCHECK(offset < kInvalidLocalOffsetInMs); | 212 DCHECK(offset < kInvalidLocalOffsetInMs); |
214 return static_cast<int>(offset); | 213 return static_cast<int>(offset); |
215 } | 214 } |
216 | 215 |
217 private: | 216 private: |
218 // The implementation relies on the fact that no time zones have | 217 // The implementation relies on the fact that no time zones have |
219 // more than one daylight savings offset change per 19 days. | 218 // more than one daylight savings offset change per 19 days. |
220 // In Egypt in 2010 they decided to suspend DST during Ramadan. This | 219 // In Egypt in 2010 they decided to suspend DST during Ramadan. This |
221 // led to a short interval where DST is in effect from September 10 to | 220 // led to a short interval where DST is in effect from September 10 to |
222 // September 30. | 221 // September 30. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 int ymd_month_; | 276 int ymd_month_; |
278 int ymd_day_; | 277 int ymd_day_; |
279 | 278 |
280 base::TimezoneCache* tz_cache_; | 279 base::TimezoneCache* tz_cache_; |
281 }; | 280 }; |
282 | 281 |
283 } // namespace internal | 282 } // namespace internal |
284 } // namespace v8 | 283 } // namespace v8 |
285 | 284 |
286 #endif | 285 #endif |
OLD | NEW |