OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 position++; | 286 position++; |
287 } | 287 } |
288 if (month == 1) { | 288 if (month == 1) { |
289 four_year_cycle_table[leap_position++] = month_bits + 29; | 289 four_year_cycle_table[leap_position++] = month_bits + 29; |
290 } | 290 } |
291 } | 291 } |
292 return four_year_cycle_table; | 292 return four_year_cycle_table; |
293 } | 293 } |
294 | 294 |
295 | 295 |
296 // Constructor for creating objects holding year, month, and date. | 296 var ymd_from_time_cache = [$NaN, $NaN, $NaN]; |
297 // Introduced to ensure the two return points in FromJulianDay match same map. | 297 var ymd_from_time_cached_time = $NaN; |
298 function DayTriplet(year, month, date) { | 298 |
299 this.year = year; | 299 function YearFromTime(t) { |
300 this.month = month; | 300 if (t !== ymd_from_time_cached_time) { |
301 this.date = date; | 301 // Limits according to ECMA 262 15.9.1.1 |
| 302 if (!$isFinite(t) || t < -8640000000000000 || t > 8640000000000000) { |
| 303 return $NaN; |
| 304 } |
| 305 |
| 306 %DateYMDFromTime(t, ymd_from_time_cache); |
| 307 ymd_from_time_cached_time = t |
| 308 } |
| 309 |
| 310 return ymd_from_time_cache[0]; |
302 } | 311 } |
303 | 312 |
304 var julian_day_cache_triplet; | 313 function MonthFromTime(t) { |
305 var julian_day_cache_day = $NaN; | 314 if (t !== ymd_from_time_cached_time) { |
| 315 // Limits according to ECMA 262 15.9.1.1 |
| 316 if (!$isFinite(t) || t < -8640000000000000 || t > 8640000000000000) { |
| 317 return $NaN; |
| 318 } |
| 319 %DateYMDFromTime(t, ymd_from_time_cache); |
| 320 ymd_from_time_cached_time = t |
| 321 } |
306 | 322 |
307 // Compute year, month, and day from modified Julian day. | 323 return ymd_from_time_cache[1]; |
308 // The missing days in 1582 are ignored for JavaScript compatibility. | 324 } |
309 function FromJulianDay(julian) { | 325 |
310 if (julian_day_cache_day == julian) { | 326 function DateFromTime(t) { |
311 return julian_day_cache_triplet; | 327 if (t !== ymd_from_time_cached_time) { |
| 328 // Limits according to ECMA 262 15.9.1.1 |
| 329 if (!$isFinite(t) || t < -8640000000000000 || t > 8640000000000000) { |
| 330 return $NaN; |
| 331 } |
| 332 |
| 333 %DateYMDFromTime(t, ymd_from_time_cache); |
| 334 ymd_from_time_cached_time = t |
312 } | 335 } |
313 var result; | 336 |
314 // Avoid floating point and non-Smi maths in common case. This is also a peri
od of | 337 return ymd_from_time_cache[2]; |
315 // time where leap years are very regular. The range is not too large to avoi
d overflow | |
316 // when doing the multiply-to-divide trick. | |
317 if (julian > kDayZeroInJulianDay && | |
318 (julian - kDayZeroInJulianDay) < 40177) { // 1970 - 2080 | |
319 var jsimple = (julian - kDayZeroInJulianDay) + 731; // Day 0 is 1st January
1968 | |
320 var y = 1968; | |
321 // Divide by 1461 by multiplying with 22967 and shifting down by 25! | |
322 var after_1968 = (jsimple * 22967) >> 25; | |
323 y += after_1968 << 2; | |
324 jsimple -= 1461 * after_1968; | |
325 var four_year_cycle = four_year_cycle_table[jsimple]; | |
326 result = new DayTriplet(y + (four_year_cycle >> kYearShift), | |
327 (four_year_cycle & kMonthMask) >> kMonthShift, | |
328 four_year_cycle & kDayMask); | |
329 } else { | |
330 var jalpha = FLOOR((julian - 1867216.25) / 36524.25); | |
331 var jb = julian + 1 + jalpha - FLOOR(0.25 * jalpha) + 1524; | |
332 var jc = FLOOR(6680.0 + ((jb-2439870) - 122.1)/365.25); | |
333 var jd = FLOOR(365 * jc + (0.25 * jc)); | |
334 var je = FLOOR((jb - jd)/30.6001); | |
335 var m = je - 1; | |
336 if (m > 12) m -= 13; | |
337 var y = jc - 4715; | |
338 if (m > 2) { --y; --m; } | |
339 var d = jb - jd - FLOOR(30.6001 * je); | |
340 result = new DayTriplet(y, m, d); | |
341 } | |
342 julian_day_cache_day = julian; | |
343 julian_day_cache_triplet = result; | |
344 return result; | |
345 } | 338 } |
346 | 339 |
347 | 340 |
348 // Compute number of days given a year, month, date. | 341 // Compute number of days given a year, month, date. |
349 // Note that month and date can lie outside the normal range. | 342 // Note that month and date can lie outside the normal range. |
350 // For example: | 343 // For example: |
351 // MakeDay(2007, -4, 20) --> MakeDay(2006, 8, 20) | 344 // MakeDay(2007, -4, 20) --> MakeDay(2006, 8, 20) |
352 // MakeDay(2007, -33, 1) --> MakeDay(2004, 3, 1) | 345 // MakeDay(2007, -33, 1) --> MakeDay(2004, 3, 1) |
353 // MakeDay(2007, 14, -50) --> MakeDay(2007, 8, 11) | 346 // MakeDay(2007, 14, -50) --> MakeDay(2007, 8, 11) |
354 function MakeDay(year, month, date) { | 347 function MakeDay(year, month, date) { |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 var WeekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; | 563 var WeekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; |
571 var Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oc
t', 'Nov', 'Dec']; | 564 var Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oc
t', 'Nov', 'Dec']; |
572 | 565 |
573 | 566 |
574 function TwoDigitString(value) { | 567 function TwoDigitString(value) { |
575 return value < 10 ? "0" + value : "" + value; | 568 return value < 10 ? "0" + value : "" + value; |
576 } | 569 } |
577 | 570 |
578 | 571 |
579 function DateString(time) { | 572 function DateString(time) { |
580 var YMD = FromJulianDay(DAY(time) + kDayZeroInJulianDay); | |
581 return WeekDays[WeekDay(time)] + ' ' | 573 return WeekDays[WeekDay(time)] + ' ' |
582 + Months[YMD.month] + ' ' | 574 + Months[MonthFromTime(time)] + ' ' |
583 + TwoDigitString(YMD.date) + ' ' | 575 + TwoDigitString(DateFromTime(time)) + ' ' |
584 + YMD.year; | 576 + YearFromTime(time); |
585 } | 577 } |
586 | 578 |
587 | 579 |
588 var LongWeekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Fri
day', 'Saturday']; | 580 var LongWeekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Fri
day', 'Saturday']; |
589 var LongMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July'
, 'August', 'September', 'October', 'November', 'December']; | 581 var LongMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July'
, 'August', 'September', 'October', 'November', 'December']; |
590 | 582 |
591 | 583 |
592 function LongDateString(time) { | 584 function LongDateString(time) { |
593 var YMD = FromJulianDay(DAY(time) + kDayZeroInJulianDay); | |
594 return LongWeekDays[WeekDay(time)] + ', ' | 585 return LongWeekDays[WeekDay(time)] + ', ' |
595 + LongMonths[YMD.month] + ' ' | 586 + LongMonths[MonthFromTime(time)] + ' ' |
596 + TwoDigitString(YMD.date) + ', ' | 587 + TwoDigitString(DateFromTime(time)) + ', ' |
597 + YMD.year; | 588 + YearFromTime(time); |
598 } | 589 } |
599 | 590 |
600 | 591 |
601 function TimeString(time) { | 592 function TimeString(time) { |
602 return TwoDigitString(HOUR_FROM_TIME(time)) + ':' | 593 return TwoDigitString(HOUR_FROM_TIME(time)) + ':' |
603 + TwoDigitString(MIN_FROM_TIME(time)) + ':' | 594 + TwoDigitString(MIN_FROM_TIME(time)) + ':' |
604 + TwoDigitString(SEC_FROM_TIME(time)); | 595 + TwoDigitString(SEC_FROM_TIME(time)); |
605 } | 596 } |
606 | 597 |
607 | 598 |
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1122 "toGMTString", DateToGMTString, | 1113 "toGMTString", DateToGMTString, |
1123 "toUTCString", DateToUTCString, | 1114 "toUTCString", DateToUTCString, |
1124 "getYear", DateGetYear, | 1115 "getYear", DateGetYear, |
1125 "setYear", DateSetYear, | 1116 "setYear", DateSetYear, |
1126 "toISOString", DateToISOString, | 1117 "toISOString", DateToISOString, |
1127 "toJSON", DateToJSON | 1118 "toJSON", DateToJSON |
1128 )); | 1119 )); |
1129 } | 1120 } |
1130 | 1121 |
1131 SetupDate(); | 1122 SetupDate(); |
OLD | NEW |