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

Side by Side Diff: src/date.cc

Issue 9572008: Implement date library functions in C++. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Add DST test and fix bugs. 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
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 #include "date.h"
29
30 #include "v8.h"
31
32 #include "objects.h"
33 #include "objects-inl.h"
34
35 namespace v8 {
36 namespace internal {
37
38
39 static const int kDays4Years[] = {0, 365, 2 * 365, 3 * 365 + 1};
40 static const int kDaysIn4Years = 4 * 365 + 1;
41 static const int kDaysIn100Years = 25 * kDaysIn4Years - 1;
42 static const int kDaysIn400Years = 4 * kDaysIn100Years + 1;
43 static const int kDays1970to2000 = 30 * 365 + 7;
44 static const int kDaysOffset = 1000 * kDaysIn400Years + 5 * kDaysIn400Years -
45 kDays1970to2000;
46 static const int kYearsOffset = 400000;
rossberg 2012/03/06 15:55:50 Not your change, but I don't understand the last t
ulan 2012/03/07 10:55:21 It's complicated :) See comment in DaysFromYearMon
47 static const char kDaysInMonths[] =
48 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
49
50
51 void DateCache::ResetDateCache() {
52 static const int kMaxStamp = Smi::kMaxValue;
53 stamp_ = Smi::FromInt(stamp_->value() + 1);
54 if (stamp_->value() > kMaxStamp) {
55 stamp_ = Smi::FromInt(0);
56 }
57 ASSERT(stamp_ != Smi::FromInt(kInvalidStamp));
58 for (int i = 0; i < kDSTSize; i++) {
rossberg 2012/03/06 15:55:50 Nit: Google style guide wants ++i.
ulan 2012/03/07 10:55:21 Done.
59 ClearSegment(&dst_[i]);
60 }
61 dst_usage_counter_ = 0;
62 before_ = &dst_[0];
63 after_ = &dst_[1];
64 local_offset_ms_ = kInvalidLocalOffsetInMs;
65 ymd_valid_ = false;
66 }
67
68
69 void DateCache::ClearSegment(DST* segment) {
70 segment->start_sec = kMaxEpochTimeInSec;
71 segment->end_sec = -kMaxEpochTimeInSec;
72 segment->offset_ms = 0;
73 segment->last_used = 0;
74 }
75
76
77 void DateCache::YearMonthDayFromDays(
78 int days, int* year, int* month, int* day) {
79 if (ymd_valid_) {
80 // Check conservatively if the given 'days' has
81 // the same year and month as the cached 'days'.
82 int new_day = ymd_day_ + (days - ymd_days_);
83 if (new_day >= 1 && new_day <= 28) {
84 ymd_day_ = new_day;
85 ymd_days_ = days;
86 *year = ymd_year_;
87 *month = ymd_month_;
88 *day = new_day;
89 return;
90 }
91 }
92 int save_days = days;
93
94 days += kDaysOffset;
95 *year = 400 * (days / kDaysIn400Years) - kYearsOffset;
96 days %= kDaysIn400Years;
97
98 ASSERT(DaysFromYearMonth(*year, 0) + days == save_days);
99
100 days--;
101 int yd1 = days / kDaysIn100Years;
102 days %= kDaysIn100Years;
103 *year += 100 * yd1;
104
105 days++;
106 int yd2 = days / kDaysIn4Years;
107 days %= kDaysIn4Years;
108 *year += 4 * yd2;
109
110 days--;
111 int yd3 = days / 365;
112 days %= 365;
113 *year += yd3;
114
115
116 bool is_leap = (!yd1 || yd2) && !yd3;
117
118 ASSERT(days >= -1);
119 ASSERT(is_leap || (days >= 0));
120 ASSERT((days < 365) || (is_leap && (days < 366)));
121 ASSERT(is_leap == ((*year % 4 == 0) && (*year % 100 || (*year % 400 == 0))));
122 ASSERT(is_leap || ((DaysFromYearMonth(*year, 0) + days) == save_days));
123 ASSERT(!is_leap || ((DaysFromYearMonth(*year, 0) + days + 1) == save_days));
124
125 days += is_leap;
126
127 // Check if the date is after February.
128 if (days >= 31 + 28 + is_leap) {
129 days -= 31 + 28 + is_leap;
130 // Find the date starting from March.
131 for (int i = 2; i < 12; i++) {
132 if (days < kDaysInMonths[i]) {
133 *month = i;
134 *day = days + 1;
135 break;
136 }
137 days -= kDaysInMonths[i];
138 }
139 } else {
140 // Check January and February.
141 if (days < 31) {
142 *month = 0;
143 *day = days + 1;
144 } else {
145 *month = 1;
146 *day = days - 31 + 1;
147 }
148 }
149 ASSERT(DaysFromYearMonth(*year, *month) + *day - 1 == save_days);
150 ymd_valid_ = true;
151 ymd_year_ = *year;
152 ymd_month_ = *month;
153 ymd_day_ = *day;
154 ymd_days_ = save_days;
155 }
156
157
158 int DateCache::DaysFromYearMonth(int year, int month) {
159 static const int day_from_month[] = {0, 31, 59, 90, 120, 151,
160 181, 212, 243, 273, 304, 334};
161 static const int day_from_month_leap[] = {0, 31, 60, 91, 121, 152,
162 182, 213, 244, 274, 305, 335};
163
164 year += month / 12;
165 month %= 12;
166 if (month < 0) {
167 year--;
168 month += 12;
169 }
170
171 ASSERT(month >= 0);
172 ASSERT(month < 12);
173
174 // year_delta is an arbitrary number such that:
175 // a) year_delta = -1 (mod 400)
176 // b) year + year_delta > 0 for years in the range defined by
177 // ECMA 262 - 15.9.1.1, i.e. upto 100,000,000 days on either side of
178 // Jan 1 1970. This is required so that we don't run into integer
179 // division of negative numbers.
180 // c) there shouldn't be an overflow for 32-bit integers in the following
181 // operations.
182 static const int year_delta = 399999;
183 static const int base_day = 365 * (1970 + year_delta) +
184 (1970 + year_delta) / 4 -
185 (1970 + year_delta) / 100 +
186 (1970 + year_delta) / 400;
187
188 int year1 = year + year_delta;
189 int day_from_year = 365 * year1 +
190 year1 / 4 -
191 year1 / 100 +
192 year1 / 400 -
193 base_day;
194
195 if ((year % 4 != 0) || (year % 100 == 0 && year % 400 != 0)) {
196 return day_from_year + day_from_month[month];
197 }
198 return day_from_year + day_from_month_leap[month];
199 }
200
201
202 void DateCache::ExtendTheAfterSegment(int time_sec, int offset_ms) {
203 if (after_->offset_ms == offset_ms &&
204 after_->start_sec <= time_sec + kDefaultDSTDeltaInSec &&
205 time_sec <= after_->end_sec) {
206 // Extend the after_ segment.
207 after_->start_sec = time_sec;
208 } else {
209 // The after_ segment is either invalid or starts too late.
210 if (after_->start_sec <= after_->end_sec) {
211 // If the after_ segment is valid, replace it with a new segment.
212 after_ = LeastRecentlyUsedDST(before_);
213 after_->last_used = before_->last_used;
214 }
215 after_->start_sec = time_sec;
216 after_->end_sec = time_sec;
217 after_->offset_ms = offset_ms;
218 }
219 }
220
221
222 int DateCache::DaylightSavingsOffsetInMs(int64_t time_ms) {
223 int time_sec = (time_ms >= 0 && time_ms <= kMaxEpochTimeInMs)
224 ? static_cast<int>(time_ms / 1000)
225 : static_cast<int>(EquivalentTime(time_ms) / 1000);
226 // Optimistic fast check.
227 if (before_->start_sec <= time_sec &&
228 time_sec <= before_->end_sec) {
229 // Cache hit.
230 before_->last_used = ++dst_usage_counter_;
231 return before_->offset_ms;
232 }
233
234 ProbeDST(time_sec);
235
236 ASSERT(InvalidSegment(before_) || before_->start_sec <= time_sec);
237 ASSERT(InvalidSegment(after_) || time_sec < after_->start_sec);
238
239 if (InvalidSegment(before_)) {
240 // Cache miss.
241 before_->start_sec = time_sec;
242 before_->end_sec = time_sec;
243 before_->offset_ms = GetDaylightSavingsOffsetFromOS(time_sec);
244 return before_->offset_ms;
245 }
246
247 if (time_sec <= before_->end_sec) {
248 // Cache hit.
249 return before_->offset_ms;
250 }
251
252 if (time_sec > before_->end_sec + kDefaultDSTDeltaInSec) {
253 // If the before_ segment ends too early, then just
254 // query for the offset of the time_sec
255 int offset_ms = GetDaylightSavingsOffsetFromOS(time_sec);
256 ExtendTheAfterSegment(time_sec, offset_ms);
257 // This swap helps the optimistic fast check in subsequent invokations.
rossberg 2012/03/06 15:55:50 Typo: invocations.
ulan 2012/03/07 10:55:21 Done.
258 DST* temp = before_;
259 before_ = after_;
260 after_ = temp;
261 return offset_ms;
262 }
263
264 // Now the time_sec is between
265 // before_->end_sec and before_->end_sec + default DST delta.
266
267 if (before_->end_sec + kDefaultDSTDeltaInSec <= after_->start_sec) {
268 // If the after_ segment starts too late, then set a new start
269 // point for it.
270 int new_after_start_sec = before_->end_sec + kDefaultDSTDeltaInSec;
271 int offset_ms = GetDaylightSavingsOffsetFromOS(new_after_start_sec);
272 ExtendTheAfterSegment(new_after_start_sec, offset_ms);
rossberg 2012/03/06 15:55:50 IIUC, then at this point, you already know the res
ulan 2012/03/07 10:55:21 If the next condition holds then the result offset
rossberg 2012/03/07 13:48:38 Yes, but my question was about the case where the
ulan 2012/03/07 14:39:19 As discussed offline, the binary search is not red
273 }
274
275 // Now the time_sec is between before_->end_sec and after_->start_sec.
276 // Only one daylight savings offset change can occur in this interval.
277
278 if (before_->offset_ms == after_->offset_ms) {
279 // Merge two segments if they have the same offset.
280 before_->end_sec = after_->end_sec;
281 ClearSegment(after_);
282 return before_->offset_ms;
283 }
284
285 // Binary search for daylight savings offset change point,
286 // but limit it to four iterations.
rossberg 2012/03/06 15:55:50 Hm, I don't understand, why only 4? Don't you need
ulan 2012/03/07 10:55:21 I updated the comment. We need about 30 = log(19 *
rossberg 2012/03/07 13:48:38 I don't understand. Don't you run into UNREACHABLE
ulan 2012/03/07 14:39:19 As discussed offline, the last iteration computes
287 for (int i = 4; i >= 0; i--) {
rossberg 2012/03/06 15:55:50 Nit: --i
ulan 2012/03/07 10:55:21 Done.
288 int delta = after_->start_sec - before_->end_sec;
289 int middle_sec = (i == 0) ? time_sec : before_->end_sec + delta / 2;
290 int offset_ms = GetDaylightSavingsOffsetFromOS(middle_sec);
291 if (before_->offset_ms == offset_ms) {
292 before_->end_sec = middle_sec;
293 if (time_sec <= before_->end_sec) {
294 return offset_ms;
295 }
296 } else {
297 ASSERT(after_->offset_ms == offset_ms);
298 after_->start_sec = middle_sec;
299 if (time_sec >= after_->start_sec) {
300 // This swap helps the optimistic fast check in subsequent invokations.
301 DST* temp = before_;
302 before_ = after_;
303 after_ = temp;
304 return offset_ms;
305 }
306 }
307 }
308 UNREACHABLE();
309 return 0;
310 }
311
312
313 void DateCache::ProbeDST(int time_sec) {
314 DST* before = NULL;
315 DST* after = NULL;
316 ASSERT(before_ != after_);
317
318 // Invalidate cache if the usage counter overflows.
319 if (dst_usage_counter_ == kMaxInt) {
320 dst_usage_counter_ = 0;
321 for (int i = 0; i < kDSTSize; i++) {
322 ClearSegment(&dst_[i]);
323 }
rossberg 2012/03/06 15:55:50 Can't you return here directly? Both before_ and a
ulan 2012/03/07 10:55:21 Yes, but I would prefer to have one exit from this
324 }
325
326 for (int i = 0; i < kDSTSize; i++) {
rossberg 2012/03/06 15:55:50 Nit: ++i again.
ulan 2012/03/07 10:55:21 Done.
327 if (dst_[i].start_sec <= time_sec) {
328 if (before == NULL || before->start_sec < dst_[i].start_sec) {
329 before = &dst_[i];
330 }
331 } else if (time_sec < dst_[i].end_sec) {
332 if (after == NULL || after->end_sec > dst_[i].end_sec) {
333 after = &dst_[i];
334 }
335 }
336 }
337
338 // If before or after segments were not found,
339 // then set them to any invalid segment.
340 if (before == NULL) {
341 before = InvalidSegment(before_) ? before_ : LeastRecentlyUsedDST(after);
342 }
343 if (after == NULL) {
344 after = InvalidSegment(after_) && before != after_
345 ? after_ : LeastRecentlyUsedDST(before);
346 }
347
348 ASSERT(before != NULL);
349 ASSERT(after != NULL);
350 ASSERT(before != after);
351 ASSERT(InvalidSegment(before) || before->start_sec <= time_sec);
352 ASSERT(InvalidSegment(after) || after->start_sec > time_sec);
353 ASSERT(InvalidSegment(before) || InvalidSegment(after) ||
354 after->start_sec > before->end_sec);
rossberg 2012/03/06 15:55:50 Nit: Maybe turn around the comparison.
ulan 2012/03/07 10:55:21 Done.
355
356 before->last_used = ++dst_usage_counter_;
rossberg 2012/03/06 15:55:50 Is it intentional that you update the usage counte
ulan 2012/03/07 10:55:21 Yes, it simplifies the code of DaylightSavingsOffs
rossberg 2012/03/07 13:48:38 How? I would have expected that you never count in
ulan 2012/03/07 14:39:19 Moved last usage update into the caller function.
357 after->last_used = before->last_used;
358
359 before_ = before;
360 after_ = after;
361 }
362
363
364 DateCache::DST* DateCache::LeastRecentlyUsedDST(DST* skip) {
365 DST* result = NULL;
366 for (int i = 0; i < kDSTSize; i++) {
367 if (&dst_[i] == skip) continue;
368 if (result == NULL || result->last_used > dst_[i].last_used) {
369 result = &dst_[i];
370 }
371 }
372 ClearSegment(result);
373 return result;
374 }
375
376 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698