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

Side by Side Diff: src/js/date.js

Issue 1579613002: [builtins] Refactor the remaining Date builtins. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: As per offline discussion, remove the weird test, which does not add a lot of value. Created 4 years, 11 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
« no previous file with comments | « src/full-codegen/x87/full-codegen-x87.cc ('k') | src/js/json.js » ('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 2006-2008 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // -------------------------------------------------------------------
6
7 (function(global, utils) {
8
9 "use strict";
10
11 %CheckIsBootstrapping();
12
13 // -------------------------------------------------------------------
14 // Imports
15
16 var GlobalDate = global.Date;
17 var GlobalObject = global.Object;
18 var InternalArray = utils.InternalArray;
19 var IsFinite;
20 var MakeRangeError;
21 var MathAbs;
22 var MathFloor;
23 var NaN = %GetRootNaN();
24
25 utils.Import(function(from) {
26 IsFinite = from.IsFinite;
27 MakeRangeError = from.MakeRangeError;
28 MathAbs = from.MathAbs;
29 MathFloor = from.MathFloor;
30 });
31
32 // -------------------------------------------------------------------
33
34 // This file contains date support implemented in JavaScript.
35
36 var timezone_cache_time = NaN;
37 var timezone_cache_timezone;
38
39 function LocalTimezone(t) {
40 if (NUMBER_IS_NAN(t)) return "";
41 CheckDateCacheCurrent();
42 if (t == timezone_cache_time) {
43 return timezone_cache_timezone;
44 }
45 var timezone = %DateLocalTimezone(t);
46 timezone_cache_time = t;
47 timezone_cache_timezone = timezone;
48 return timezone;
49 }
50
51
52 // ECMA 262 - 15.9.1.11
53 function MakeTime(hour, min, sec, ms) {
54 if (!IsFinite(hour)) return NaN;
55 if (!IsFinite(min)) return NaN;
56 if (!IsFinite(sec)) return NaN;
57 if (!IsFinite(ms)) return NaN;
58 return TO_INTEGER(hour) * msPerHour
59 + TO_INTEGER(min) * msPerMinute
60 + TO_INTEGER(sec) * msPerSecond
61 + TO_INTEGER(ms);
62 }
63
64
65 // Compute number of days given a year, month, date.
66 // Note that month and date can lie outside the normal range.
67 // For example:
68 // MakeDay(2007, -4, 20) --> MakeDay(2006, 8, 20)
69 // MakeDay(2007, -33, 1) --> MakeDay(2004, 3, 1)
70 // MakeDay(2007, 14, -50) --> MakeDay(2007, 8, 11)
71 function MakeDay(year, month, date) {
72 if (!IsFinite(year) || !IsFinite(month) || !IsFinite(date)) return NaN;
73
74 // Convert to integer and map -0 to 0.
75 year = TO_INTEGER_MAP_MINUS_ZERO(year);
76 month = TO_INTEGER_MAP_MINUS_ZERO(month);
77 date = TO_INTEGER_MAP_MINUS_ZERO(date);
78
79 if (year < kMinYear || year > kMaxYear ||
80 month < kMinMonth || month > kMaxMonth) {
81 return NaN;
82 }
83
84 // Now we rely on year and month being SMIs.
85 return %DateMakeDay(year | 0, month | 0) + date - 1;
86 }
87
88
89 // ECMA 262 - 15.9.1.13
90 function MakeDate(day, time) {
91 var time = day * msPerDay + time;
92 // Some of our runtime funtions for computing UTC(time) rely on
93 // times not being significantly larger than MAX_TIME_MS. If there
94 // is no way that the time can be within range even after UTC
95 // conversion we return NaN immediately instead of relying on
96 // TimeClip to do it.
97 if (MathAbs(time) > MAX_TIME_BEFORE_UTC) return NaN;
98 return time;
99 }
100
101
102 // ECMA 262 - 15.9.1.14
103 function TimeClip(time) {
104 if (!IsFinite(time)) return NaN;
105 if (MathAbs(time) > MAX_TIME_MS) return NaN;
106 return TO_INTEGER(time) + 0;
107 }
108
109
110 var WeekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
111 var Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
112 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
113
114
115 function TwoDigitString(value) {
116 return value < 10 ? "0" + value : "" + value;
117 }
118
119
120 function DateString(date) {
121 CHECK_DATE(date);
122 return WeekDays[LOCAL_WEEKDAY(date)] + ' '
123 + Months[LOCAL_MONTH(date)] + ' '
124 + TwoDigitString(LOCAL_DAY(date)) + ' '
125 + LOCAL_YEAR(date);
126 }
127
128
129 var LongWeekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
130 'Thursday', 'Friday', 'Saturday'];
131 var LongMonths = ['January', 'February', 'March', 'April', 'May', 'June',
132 'July', 'August', 'September', 'October', 'November', 'December'];
133
134
135 function LongDateString(date) {
136 CHECK_DATE(date);
137 return LongWeekDays[LOCAL_WEEKDAY(date)] + ', '
138 + LongMonths[LOCAL_MONTH(date)] + ' '
139 + TwoDigitString(LOCAL_DAY(date)) + ', '
140 + LOCAL_YEAR(date);
141 }
142
143
144 function TimeString(date) {
145 CHECK_DATE(date);
146 return TwoDigitString(LOCAL_HOUR(date)) + ':'
147 + TwoDigitString(LOCAL_MIN(date)) + ':'
148 + TwoDigitString(LOCAL_SEC(date));
149 }
150
151
152 function TimeStringUTC(date) {
153 CHECK_DATE(date);
154 return TwoDigitString(UTC_HOUR(date)) + ':'
155 + TwoDigitString(UTC_MIN(date)) + ':'
156 + TwoDigitString(UTC_SEC(date));
157 }
158
159
160 function LocalTimezoneString(date) {
161 CHECK_DATE(date);
162 var timezone = LocalTimezone(UTC_DATE_VALUE(date));
163
164 var timezoneOffset = -TIMEZONE_OFFSET(date);
165 var sign = (timezoneOffset >= 0) ? 1 : -1;
166 var hours = MathFloor((sign * timezoneOffset)/60);
167 var min = MathFloor((sign * timezoneOffset)%60);
168 var gmt = ' GMT' + ((sign == 1) ? '+' : '-') +
169 TwoDigitString(hours) + TwoDigitString(min);
170 return gmt + ' (' + timezone + ')';
171 }
172
173
174 function DatePrintString(date) {
175 CHECK_DATE(date);
176 return DateString(date) + ' ' + TimeString(date);
177 }
178
179 // -------------------------------------------------------------------
180
181 // ECMA 262 - 15.9.5.2
182 function DateToString() {
183 CHECK_DATE(this);
184 var t = UTC_DATE_VALUE(this)
185 if (NUMBER_IS_NAN(t)) return kInvalidDate;
186 var time_zone_string = LocalTimezoneString(this)
187 return DatePrintString(this) + time_zone_string;
188 }
189
190
191 // ECMA 262 - 15.9.5.3
192 function DateToDateString() {
193 CHECK_DATE(this);
194 var t = UTC_DATE_VALUE(this);
195 if (NUMBER_IS_NAN(t)) return kInvalidDate;
196 return DateString(this);
197 }
198
199
200 // ECMA 262 - 15.9.5.4
201 function DateToTimeString() {
202 CHECK_DATE(this);
203 var t = UTC_DATE_VALUE(this);
204 if (NUMBER_IS_NAN(t)) return kInvalidDate;
205 var time_zone_string = LocalTimezoneString(this);
206 return TimeString(this) + time_zone_string;
207 }
208
209
210 // ECMA 262 - 15.9.5.5
211 function DateToLocaleString() {
212 CHECK_DATE(this);
213 return %_Call(DateToString, this);
214 }
215
216
217 // ECMA 262 - 15.9.5.6
218 function DateToLocaleDateString() {
219 CHECK_DATE(this);
220 var t = UTC_DATE_VALUE(this);
221 if (NUMBER_IS_NAN(t)) return kInvalidDate;
222 return LongDateString(this);
223 }
224
225
226 // ECMA 262 - 15.9.5.7
227 function DateToLocaleTimeString() {
228 CHECK_DATE(this);
229 var t = UTC_DATE_VALUE(this);
230 if (NUMBER_IS_NAN(t)) return kInvalidDate;
231 return TimeString(this);
232 }
233
234
235 // ECMA 262 - 15.9.5.27
236 function DateSetTime(ms) {
237 CHECK_DATE(this);
238 SET_UTC_DATE_VALUE(this, TO_NUMBER(ms));
239 return UTC_DATE_VALUE(this);
240 }
241
242
243 // ECMA 262 - 15.9.5.28
244 function DateSetMilliseconds(ms) {
245 CHECK_DATE(this);
246 var t = LOCAL_DATE_VALUE(this);
247 ms = TO_NUMBER(ms);
248 var time = MakeTime(LOCAL_HOUR(this), LOCAL_MIN(this), LOCAL_SEC(this), ms);
249 return SET_LOCAL_DATE_VALUE(this, MakeDate(LOCAL_DAYS(this), time));
250 }
251
252
253 // ECMA 262 - 15.9.5.29
254 function DateSetUTCMilliseconds(ms) {
255 CHECK_DATE(this);
256 var t = UTC_DATE_VALUE(this);
257 ms = TO_NUMBER(ms);
258 var time = MakeTime(UTC_HOUR(this),
259 UTC_MIN(this),
260 UTC_SEC(this),
261 ms);
262 return SET_UTC_DATE_VALUE(this, MakeDate(UTC_DAYS(this), time));
263 }
264
265
266 // ECMA 262 - 15.9.5.30
267 function DateSetSeconds(sec, ms) {
268 CHECK_DATE(this);
269 var t = LOCAL_DATE_VALUE(this);
270 sec = TO_NUMBER(sec);
271 ms = %_ArgumentsLength() < 2 ? LOCAL_MS(this) : TO_NUMBER(ms);
272 var time = MakeTime(LOCAL_HOUR(this), LOCAL_MIN(this), sec, ms);
273 return SET_LOCAL_DATE_VALUE(this, MakeDate(LOCAL_DAYS(this), time));
274 }
275
276
277 // ECMA 262 - 15.9.5.31
278 function DateSetUTCSeconds(sec, ms) {
279 CHECK_DATE(this);
280 var t = UTC_DATE_VALUE(this);
281 sec = TO_NUMBER(sec);
282 ms = %_ArgumentsLength() < 2 ? UTC_MS(this) : TO_NUMBER(ms);
283 var time = MakeTime(UTC_HOUR(this), UTC_MIN(this), sec, ms);
284 return SET_UTC_DATE_VALUE(this, MakeDate(UTC_DAYS(this), time));
285 }
286
287
288 // ECMA 262 - 15.9.5.33
289 function DateSetMinutes(min, sec, ms) {
290 CHECK_DATE(this);
291 var t = LOCAL_DATE_VALUE(this);
292 min = TO_NUMBER(min);
293 var argc = %_ArgumentsLength();
294 sec = argc < 2 ? LOCAL_SEC(this) : TO_NUMBER(sec);
295 ms = argc < 3 ? LOCAL_MS(this) : TO_NUMBER(ms);
296 var time = MakeTime(LOCAL_HOUR(this), min, sec, ms);
297 return SET_LOCAL_DATE_VALUE(this, MakeDate(LOCAL_DAYS(this), time));
298 }
299
300
301 // ECMA 262 - 15.9.5.34
302 function DateSetUTCMinutes(min, sec, ms) {
303 CHECK_DATE(this);
304 var t = UTC_DATE_VALUE(this);
305 min = TO_NUMBER(min);
306 var argc = %_ArgumentsLength();
307 sec = argc < 2 ? UTC_SEC(this) : TO_NUMBER(sec);
308 ms = argc < 3 ? UTC_MS(this) : TO_NUMBER(ms);
309 var time = MakeTime(UTC_HOUR(this), min, sec, ms);
310 return SET_UTC_DATE_VALUE(this, MakeDate(UTC_DAYS(this), time));
311 }
312
313
314 // ECMA 262 - 15.9.5.35
315 function DateSetHours(hour, min, sec, ms) {
316 CHECK_DATE(this);
317 var t = LOCAL_DATE_VALUE(this);
318 hour = TO_NUMBER(hour);
319 var argc = %_ArgumentsLength();
320 min = argc < 2 ? LOCAL_MIN(this) : TO_NUMBER(min);
321 sec = argc < 3 ? LOCAL_SEC(this) : TO_NUMBER(sec);
322 ms = argc < 4 ? LOCAL_MS(this) : TO_NUMBER(ms);
323 var time = MakeTime(hour, min, sec, ms);
324 return SET_LOCAL_DATE_VALUE(this, MakeDate(LOCAL_DAYS(this), time));
325 }
326
327
328 // ECMA 262 - 15.9.5.34
329 function DateSetUTCHours(hour, min, sec, ms) {
330 CHECK_DATE(this);
331 var t = UTC_DATE_VALUE(this);
332 hour = TO_NUMBER(hour);
333 var argc = %_ArgumentsLength();
334 min = argc < 2 ? UTC_MIN(this) : TO_NUMBER(min);
335 sec = argc < 3 ? UTC_SEC(this) : TO_NUMBER(sec);
336 ms = argc < 4 ? UTC_MS(this) : TO_NUMBER(ms);
337 var time = MakeTime(hour, min, sec, ms);
338 return SET_UTC_DATE_VALUE(this, MakeDate(UTC_DAYS(this), time));
339 }
340
341
342 // ECMA 262 - 15.9.5.36
343 function DateSetDate(date) {
344 CHECK_DATE(this);
345 var t = LOCAL_DATE_VALUE(this);
346 date = TO_NUMBER(date);
347 var day = MakeDay(LOCAL_YEAR(this), LOCAL_MONTH(this), date);
348 return SET_LOCAL_DATE_VALUE(this, MakeDate(day, LOCAL_TIME_IN_DAY(this)));
349 }
350
351
352 // ECMA 262 - 15.9.5.37
353 function DateSetUTCDate(date) {
354 CHECK_DATE(this);
355 var t = UTC_DATE_VALUE(this);
356 date = TO_NUMBER(date);
357 var day = MakeDay(UTC_YEAR(this), UTC_MONTH(this), date);
358 return SET_UTC_DATE_VALUE(this, MakeDate(day, UTC_TIME_IN_DAY(this)));
359 }
360
361
362 // ECMA 262 - 15.9.5.38
363 function DateSetMonth(month, date) {
364 CHECK_DATE(this);
365 var t = LOCAL_DATE_VALUE(this);
366 month = TO_NUMBER(month);
367 date = %_ArgumentsLength() < 2 ? LOCAL_DAY(this) : TO_NUMBER(date);
368 var day = MakeDay(LOCAL_YEAR(this), month, date);
369 return SET_LOCAL_DATE_VALUE(this, MakeDate(day, LOCAL_TIME_IN_DAY(this)));
370 }
371
372
373 // ECMA 262 - 15.9.5.39
374 function DateSetUTCMonth(month, date) {
375 CHECK_DATE(this);
376 var t = UTC_DATE_VALUE(this);
377 month = TO_NUMBER(month);
378 date = %_ArgumentsLength() < 2 ? UTC_DAY(this) : TO_NUMBER(date);
379 var day = MakeDay(UTC_YEAR(this), month, date);
380 return SET_UTC_DATE_VALUE(this, MakeDate(day, UTC_TIME_IN_DAY(this)));
381 }
382
383
384 // ECMA 262 - 15.9.5.40
385 function DateSetFullYear(year, month, date) {
386 CHECK_DATE(this);
387 var t = LOCAL_DATE_VALUE(this);
388 year = TO_NUMBER(year);
389 var argc = %_ArgumentsLength();
390 var time ;
391 if (NUMBER_IS_NAN(t)) {
392 month = argc < 2 ? 0 : TO_NUMBER(month);
393 date = argc < 3 ? 1 : TO_NUMBER(date);
394 time = 0;
395 } else {
396 month = argc < 2 ? LOCAL_MONTH(this) : TO_NUMBER(month);
397 date = argc < 3 ? LOCAL_DAY(this) : TO_NUMBER(date);
398 time = LOCAL_TIME_IN_DAY(this);
399 }
400 var day = MakeDay(year, month, date);
401 return SET_LOCAL_DATE_VALUE(this, MakeDate(day, time));
402 }
403
404
405 // ECMA 262 - 15.9.5.41
406 function DateSetUTCFullYear(year, month, date) {
407 CHECK_DATE(this);
408 var t = UTC_DATE_VALUE(this);
409 year = TO_NUMBER(year);
410 var argc = %_ArgumentsLength();
411 var time ;
412 if (NUMBER_IS_NAN(t)) {
413 month = argc < 2 ? 0 : TO_NUMBER(month);
414 date = argc < 3 ? 1 : TO_NUMBER(date);
415 time = 0;
416 } else {
417 month = argc < 2 ? UTC_MONTH(this) : TO_NUMBER(month);
418 date = argc < 3 ? UTC_DAY(this) : TO_NUMBER(date);
419 time = UTC_TIME_IN_DAY(this);
420 }
421 var day = MakeDay(year, month, date);
422 return SET_UTC_DATE_VALUE(this, MakeDate(day, time));
423 }
424
425
426 // ECMA 262 - 15.9.5.42
427 function DateToUTCString() {
428 CHECK_DATE(this);
429 var t = UTC_DATE_VALUE(this);
430 if (NUMBER_IS_NAN(t)) return kInvalidDate;
431 // Return UTC string of the form: Sat, 31 Jan 1970 23:00:00 GMT
432 return WeekDays[UTC_WEEKDAY(this)] + ', '
433 + TwoDigitString(UTC_DAY(this)) + ' '
434 + Months[UTC_MONTH(this)] + ' '
435 + UTC_YEAR(this) + ' '
436 + TimeStringUTC(this) + ' GMT';
437 }
438
439
440 // ECMA 262 - B.2.4
441 function DateGetYear() {
442 CHECK_DATE(this);
443 return LOCAL_YEAR(this) - 1900;
444 }
445
446
447 // ECMA 262 - B.2.5
448 function DateSetYear(year) {
449 CHECK_DATE(this);
450 year = TO_NUMBER(year);
451 if (NUMBER_IS_NAN(year)) return SET_UTC_DATE_VALUE(this, NaN);
452 year = (0 <= TO_INTEGER(year) && TO_INTEGER(year) <= 99)
453 ? 1900 + TO_INTEGER(year) : year;
454 var t = LOCAL_DATE_VALUE(this);
455 var month, date, time;
456 if (NUMBER_IS_NAN(t)) {
457 month = 0;
458 date = 1;
459 time = 0;
460 } else {
461 month = LOCAL_MONTH(this);
462 date = LOCAL_DAY(this);
463 time = LOCAL_TIME_IN_DAY(this);
464 }
465 var day = MakeDay(year, month, date);
466 return SET_LOCAL_DATE_VALUE(this, MakeDate(day, time));
467 }
468
469
470 // ECMA 262 - B.2.6
471 //
472 // Notice that this does not follow ECMA 262 completely. ECMA 262
473 // says that toGMTString should be the same Function object as
474 // toUTCString. JSC does not do this, so for compatibility we do not
475 // do that either. Instead, we create a new function whose name
476 // property will return toGMTString.
477 function DateToGMTString() {
478 return %_Call(DateToUTCString, this);
479 }
480
481
482 // 20.3.4.37 Date.prototype.toJSON ( key )
483 function DateToJSON(key) {
484 var o = TO_OBJECT(this);
485 var tv = TO_PRIMITIVE_NUMBER(o);
486 if (IS_NUMBER(tv) && !NUMBER_IS_FINITE(tv)) {
487 return null;
488 }
489 return o.toISOString();
490 }
491
492
493 var date_cache_version_holder;
494 var date_cache_version = NaN;
495
496
497 function CheckDateCacheCurrent() {
498 if (!date_cache_version_holder) {
499 date_cache_version_holder = %DateCacheVersion();
500 if (!date_cache_version_holder) return;
501 }
502 if (date_cache_version_holder[0] == date_cache_version) {
503 return;
504 }
505 date_cache_version = date_cache_version_holder[0];
506
507 // Reset the timezone cache:
508 timezone_cache_time = NaN;
509 timezone_cache_timezone = UNDEFINED;
510 }
511
512 // -------------------------------------------------------------------
513
514 %FunctionSetPrototype(GlobalDate, new GlobalObject());
515
516 // Set up non-enumerable functions of the Date prototype object and
517 // set their names.
518 utils.InstallFunctions(GlobalDate.prototype, DONT_ENUM, [
519 "toString", DateToString,
520 "toDateString", DateToDateString,
521 "toTimeString", DateToTimeString,
522 "toLocaleString", DateToLocaleString,
523 "toLocaleDateString", DateToLocaleDateString,
524 "toLocaleTimeString", DateToLocaleTimeString,
525 "setTime", DateSetTime,
526 "setMilliseconds", DateSetMilliseconds,
527 "setUTCMilliseconds", DateSetUTCMilliseconds,
528 "setSeconds", DateSetSeconds,
529 "setUTCSeconds", DateSetUTCSeconds,
530 "setMinutes", DateSetMinutes,
531 "setUTCMinutes", DateSetUTCMinutes,
532 "setHours", DateSetHours,
533 "setUTCHours", DateSetUTCHours,
534 "setDate", DateSetDate,
535 "setUTCDate", DateSetUTCDate,
536 "setMonth", DateSetMonth,
537 "setUTCMonth", DateSetUTCMonth,
538 "setFullYear", DateSetFullYear,
539 "setUTCFullYear", DateSetUTCFullYear,
540 "toGMTString", DateToGMTString,
541 "toUTCString", DateToUTCString,
542 "getYear", DateGetYear,
543 "setYear", DateSetYear,
544 "toJSON", DateToJSON
545 ]);
546
547 })
OLDNEW
« no previous file with comments | « src/full-codegen/x87/full-codegen-x87.cc ('k') | src/js/json.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698