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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 | 106 |
107 function EquivalentTime(t) { | 107 function EquivalentTime(t) { |
108 // The issue here is that some library calls don't work right for dates | 108 // The issue here is that some library calls don't work right for dates |
109 // that cannot be represented using a non-negative signed 32 bit integer | 109 // that cannot be represented using a non-negative signed 32 bit integer |
110 // (measured in whole seconds based on the 1970 epoch). | 110 // (measured in whole seconds based on the 1970 epoch). |
111 // We solve this by mapping the time to a year with same leap-year-ness | 111 // We solve this by mapping the time to a year with same leap-year-ness |
112 // and same starting day for the year. The ECMAscript specification says | 112 // and same starting day for the year. The ECMAscript specification says |
113 // we must do this, but for compatibility with other browsers, we use | 113 // we must do this, but for compatibility with other browsers, we use |
114 // the actual year if it is in the range 1970..2037 | 114 // the actual year if it is in the range 1970..2037 |
115 if (t >= 0 && t <= 2.1e12) return t; | 115 if (t >= 0 && t <= 2.1e12) return t; |
116 var day = MakeDay(EquivalentYear(YEAR_FROM_TIME(t)), MONTH_FROM_TIME(t), DATE_ FROM_TIME(t)); | 116 |
117 return TimeClip(MakeDate(day, TimeWithinDay(t))); | 117 // We call function from runtime.cc to avoid extra checks which are unneeded. |
118 var day = %DateMakeDay(EquivalentYear(YEAR_FROM_TIME(t)), | |
119 MONTH_FROM_TIME(t), | |
120 DATE_FROM_TIME(t)); | |
121 return MakeDate(day, TimeWithinDay(t)); | |
118 } | 122 } |
119 | 123 |
120 | 124 |
121 // Because computing the DST offset is a pretty expensive operation | 125 // Because computing the DST offset is a pretty expensive operation |
122 // we keep a cache of last computed offset along with a time interval | 126 // we keep a cache of last computed offset along with a time interval |
123 // where we know the cache is valid. | 127 // where we know the cache is valid. |
124 var DST_offset_cache = { | 128 var DST_offset_cache = { |
125 // Cached DST offset. | 129 // Cached DST offset. |
126 offset: 0, | 130 offset: 0, |
127 // Time interval where the cached offset is valid. | 131 // Time interval where the cached offset is valid. |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 + TO_INTEGER(ms); | 254 + TO_INTEGER(ms); |
251 } | 255 } |
252 | 256 |
253 | 257 |
254 // ECMA 262 - 15.9.1.12 | 258 // ECMA 262 - 15.9.1.12 |
255 function TimeInYear(year) { | 259 function TimeInYear(year) { |
256 return DaysInYear(year) * msPerDay; | 260 return DaysInYear(year) * msPerDay; |
257 } | 261 } |
258 | 262 |
259 | 263 |
260 // Compute modified Julian day from year, month, date. | |
261 function ToJulianDay(year, month, date) { | |
262 var jy = (month > 1) ? year : year - 1; | |
263 var jm = (month > 1) ? month + 2 : month + 14; | |
264 var ja = FLOOR(jy / 100); | |
265 return FLOOR(FLOOR(365.25*jy) + FLOOR(30.6001*jm) + date + 1720995) + 2 - ja + FLOOR(0.25*ja); | |
266 } | |
267 | |
268 var four_year_cycle_table = CalculateDateTable(); | 264 var four_year_cycle_table = CalculateDateTable(); |
269 | 265 |
270 | 266 |
271 function CalculateDateTable() { | 267 function CalculateDateTable() { |
272 var month_lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; | 268 var month_lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
273 var four_year_cycle_table = new $Array(1461); | 269 var four_year_cycle_table = new $Array(1461); |
274 | 270 |
275 var cumulative = 0; | 271 var cumulative = 0; |
276 var position = 0; | 272 var position = 0; |
277 var leap_position = 0; | 273 var leap_position = 0; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 | 348 |
353 // Compute number of days given a year, month, date. | 349 // Compute number of days given a year, month, date. |
354 // Note that month and date can lie outside the normal range. | 350 // Note that month and date can lie outside the normal range. |
355 // For example: | 351 // For example: |
356 // MakeDay(2007, -4, 20) --> MakeDay(2006, 8, 20) | 352 // MakeDay(2007, -4, 20) --> MakeDay(2006, 8, 20) |
357 // MakeDay(2007, -33, 1) --> MakeDay(2004, 3, 1) | 353 // MakeDay(2007, -33, 1) --> MakeDay(2004, 3, 1) |
358 // MakeDay(2007, 14, -50) --> MakeDay(2007, 8, 11) | 354 // MakeDay(2007, 14, -50) --> MakeDay(2007, 8, 11) |
359 function MakeDay(year, month, date) { | 355 function MakeDay(year, month, date) { |
360 if (!$isFinite(year) || !$isFinite(month) || !$isFinite(date)) return $NaN; | 356 if (!$isFinite(year) || !$isFinite(month) || !$isFinite(date)) return $NaN; |
361 | 357 |
362 // Conversion to integers. | |
363 year = TO_INTEGER(year); | 358 year = TO_INTEGER(year); |
364 month = TO_INTEGER(month); | 359 month = TO_INTEGER(month); |
365 date = TO_INTEGER(date); | 360 date = TO_INTEGER(date); |
366 | 361 |
367 // Overflow months into year. | 362 if (year < kMinYear || year > kMaxYear || |
368 year = year + FLOOR(month/12); | 363 month < kMinMonth || month > kMaxMonth || |
369 month = month % 12; | 364 date < kMinDate || date > kMaxDate) { |
370 if (month < 0) { | 365 return $NaN; |
371 month += 12; | |
372 } | 366 } |
373 | 367 |
374 // Return days relative to Jan 1 1970. | 368 // Now we presume that year, month and date are actually SMIs. |
Mads Ager (chromium)
2010/03/02 13:04:08
Since the runtime function probably relies on year
Oleg Eterevsky
2010/03/02 13:28:23
Done.
| |
375 return ToJulianDay(year, month, date) - kDayZeroInJulianDay; | 369 return %DateMakeDay(year, month, date); |
376 } | 370 } |
377 | 371 |
378 | 372 |
379 // ECMA 262 - 15.9.1.13 | 373 // ECMA 262 - 15.9.1.13 |
380 function MakeDate(day, time) { | 374 function MakeDate(day, time) { |
381 if (!$isFinite(day)) return $NaN; | 375 if (!$isFinite(day)) return $NaN; |
382 if (!$isFinite(time)) return $NaN; | 376 if (!$isFinite(time)) return $NaN; |
383 return day * msPerDay + time; | 377 return day * msPerDay + time; |
384 } | 378 } |
385 | 379 |
(...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1129 "toGMTString", DateToGMTString, | 1123 "toGMTString", DateToGMTString, |
1130 "toUTCString", DateToUTCString, | 1124 "toUTCString", DateToUTCString, |
1131 "getYear", DateGetYear, | 1125 "getYear", DateGetYear, |
1132 "setYear", DateSetYear, | 1126 "setYear", DateSetYear, |
1133 "toISOString", DateToISOString, | 1127 "toISOString", DateToISOString, |
1134 "toJSON", DateToJSON | 1128 "toJSON", DateToJSON |
1135 )); | 1129 )); |
1136 } | 1130 } |
1137 | 1131 |
1138 SetupDate(); | 1132 SetupDate(); |
OLD | NEW |