Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: src/date.h

Issue 9572008: Implement date library functions in C++. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Rebase Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/assembler.cc ('k') | src/date.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 = kMaxInt;
47 static const int64_t kMaxEpochTimeInMs =
48 static_cast<int64_t>(kMaxInt) * 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 static const int kInvalidLocalOffsetInMs = kMaxInt;
56 // Sentinel that denotes an invalid cache stamp.
57 // It is an invariant of DateCache that cache stamp is non-negative.
58 static const int kInvalidStamp = -1;
59
60 DateCache() : stamp_(0) {
61 ResetDateCache();
62 }
63
64 virtual ~DateCache() {}
65
66
67 // Clears cached timezone information and increments the cache stamp.
68 void ResetDateCache();
69
70
71 // Computes floor(time_ms / kMsPerDay).
72 static int DaysFromTime(int64_t time_ms) {
73 if (time_ms < 0) time_ms -= (kMsPerDay - 1);
74 return static_cast<int>(time_ms / kMsPerDay);
75 }
76
77
78 // Computes modulo(time_ms, kMsPerDay) given that
79 // days = floor(time_ms / kMsPerDay).
80 static int TimeInDay(int64_t time_ms, int days) {
81 return static_cast<int>(time_ms - days * kMsPerDay);
82 }
83
84
85 // Given the number of days since the epoch, computes the weekday.
86 // ECMA 262 - 15.9.1.6.
87 int Weekday(int days) {
88 int result = (days + 4) % 7;
89 return result >= 0 ? result : result + 7;
90 }
91
92
93 bool IsLeap(int year) {
94 return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
95 }
96
97
98 // ECMA 262 - 15.9.1.7.
99 int LocalOffsetInMs() {
100 if (local_offset_ms_ == kInvalidLocalOffsetInMs) {
101 local_offset_ms_ = GetLocalOffsetFromOS();
102 }
103 return local_offset_ms_;
104 }
105
106
107 const char* LocalTimezone(int64_t time_ms) {
108 if (time_ms < 0 || time_ms > kMaxEpochTimeInMs) {
109 time_ms = EquivalentTime(time_ms);
110 }
111 return OS::LocalTimezone(static_cast<double>(time_ms));
112 }
113
114 // ECMA 262 - 15.9.5.26
115 int TimezoneOffset(int64_t time_ms) {
116 int64_t local_ms = ToLocal(time_ms);
117 return static_cast<int>((time_ms - local_ms) / kMsPerMin);
118 }
119
120 // ECMA 262 - 15.9.1.9
121 int64_t ToLocal(int64_t time_ms) {
122 return time_ms + LocalOffsetInMs() + DaylightSavingsOffsetInMs(time_ms);
123 }
124
125 // ECMA 262 - 15.9.1.9
126 int64_t ToUTC(int64_t time_ms) {
127 time_ms -= LocalOffsetInMs();
128 return time_ms - DaylightSavingsOffsetInMs(time_ms);
129 }
130
131
132 // Computes a time equivalent to the given time according
133 // to ECMA 262 - 15.9.1.9.
134 // The issue here is that some library calls don't work right for dates
135 // that cannot be represented using a non-negative signed 32 bit integer
136 // (measured in whole seconds based on the 1970 epoch).
137 // We solve this by mapping the time to a year with same leap-year-ness
138 // and same starting day for the year. The ECMAscript specification says
139 // we must do this, but for compatibility with other browsers, we use
140 // the actual year if it is in the range 1970..2037
141 int64_t EquivalentTime(int64_t time_ms) {
142 int days = DaysFromTime(time_ms);
143 int time_within_day_ms = static_cast<int>(time_ms - days * kMsPerDay);
144 int year, month, day;
145 YearMonthDayFromDays(days, &year, &month, &day);
146 int new_days = DaysFromYearMonth(EquivalentYear(year), month) + day - 1;
147 return static_cast<int64_t>(new_days) * kMsPerDay + time_within_day_ms;
148 }
149
150 // Returns an equivalent year in the range [2008-2035] matching
151 // - leap year,
152 // - week day of first day.
153 // ECMA 262 - 15.9.1.9.
154 int EquivalentYear(int year) {
155 int week_day = Weekday(DaysFromYearMonth(year, 0));
156 int recent_year = (IsLeap(year) ? 1956 : 1967) + (week_day * 12) % 28;
157 // Find the year in the range 2008..2037 that is equivalent mod 28.
158 // Add 3*28 to give a positive argument to the modulus operator.
159 return 2008 + (recent_year + 3 * 28 - 2008) % 28;
160 }
161
162 // Given the number of days since the epoch, computes
163 // the corresponding year, month, and day.
164 void YearMonthDayFromDays(int days, int* year, int* month, int* day);
165
166 // Computes the number of days since the epoch for
167 // the first day of the given month in the given year.
168 int DaysFromYearMonth(int year, int month);
169
170 // Cache stamp is used for invalidating caches in JSDate.
171 // We increment the stamp each time when the timezone information changes.
172 // JSDate objects perform stamp check and invalidate their caches if
173 // their saved stamp is not equal to the current stamp.
174 Smi* stamp() { return stamp_; }
175 void* stamp_address() { return &stamp_; }
176
177 // These functions are virtual so that we can override them when testing.
178 virtual int GetDaylightSavingsOffsetFromOS(int64_t time_sec) {
179 return static_cast<int>(OS::DaylightSavingsOffset(time_sec * 1000));
180 }
181
182 virtual int GetLocalOffsetFromOS() {
183 double offset = OS::LocalTimeOffset();
184 ASSERT(offset < kInvalidLocalOffsetInMs);
185 return static_cast<int>(offset);
186 }
187
188 private:
189 // The implementation relies on the fact that no time zones have
190 // more than one daylight savings offset change per 19 days.
191 // In Egypt in 2010 they decided to suspend DST during Ramadan. This
192 // led to a short interval where DST is in effect from September 10 to
193 // September 30.
194 static const int kDefaultDSTDeltaInSec = 19 * kSecPerDay;
195
196 // Size of the Daylight Savings Time cache.
197 static const int kDSTSize = 32;
198
199 // Daylight Savings Time segment stores a segment of time where
200 // daylight savings offset does not change.
201 struct DST {
202 int start_sec;
203 int end_sec;
204 int offset_ms;
205 int last_used;
206 };
207
208 // Computes the daylight savings offset for the given time.
209 // ECMA 262 - 15.9.1.8
210 int DaylightSavingsOffsetInMs(int64_t time_ms);
211
212 // Sets the before_ and the after_ segments from the DST cache such that
213 // the before_ segment starts earlier than the given time and
214 // the after_ segment start later than the given time.
215 // Both segments might be invalid.
216 // The last_used counters of the before_ and after_ are updated.
217 void ProbeDST(int time_sec);
218
219 // Finds the least recently used segment from the DST cache that is not
220 // equal to the given 'skip' segment.
221 DST* LeastRecentlyUsedDST(DST* skip);
222
223 // Extends the after_ segment with the given point or resets it
224 // if it starts later than the given time + kDefaultDSTDeltaInSec.
225 inline void ExtendTheAfterSegment(int time_sec, int offset_ms);
226
227 // Makes the given segment invalid.
228 inline void ClearSegment(DST* segment);
229
230 bool InvalidSegment(DST* segment) {
231 return segment->start_sec > segment->end_sec;
232 }
233
234 Smi* stamp_;
235
236 // Daylight Saving Time cache.
237 DST dst_[kDSTSize];
238 int dst_usage_counter_;
239 DST* before_;
240 DST* after_;
241
242 int local_offset_ms_;
243
244 // Year/Month/Day cache.
245 bool ymd_valid_;
246 int ymd_days_;
247 int ymd_year_;
248 int ymd_month_;
249 int ymd_day_;
250 };
251
252 } } // namespace v8::internal
253
254 #endif
OLDNEW
« no previous file with comments | « src/assembler.cc ('k') | src/date.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698