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 22 matching lines...) Expand all Loading... |
33 | 33 |
34 // Keep reference to original values of some global properties. This | 34 // Keep reference to original values of some global properties. This |
35 // has the added benefit that the code in this file is isolated from | 35 // has the added benefit that the code in this file is isolated from |
36 // changes to these properties. | 36 // changes to these properties. |
37 const $Date = global.Date; | 37 const $Date = global.Date; |
38 | 38 |
39 | 39 |
40 // ECMA 262 - 15.9.1.2 | 40 // ECMA 262 - 15.9.1.2 |
41 function Day(time) { | 41 function Day(time) { |
42 return $floor(time/msPerDay); | 42 return $floor(time/msPerDay); |
43 }; | 43 } |
44 | 44 |
45 | 45 |
46 // ECMA 262 - 5.2 | 46 // ECMA 262 - 5.2 |
47 function Modulo(value, remainder) { | 47 function Modulo(value, remainder) { |
48 var mod = value % remainder; | 48 var mod = value % remainder; |
49 return mod >= 0 ? mod : mod + remainder; | 49 return mod >= 0 ? mod : mod + remainder; |
50 }; | 50 } |
51 | 51 |
52 | 52 |
53 function TimeWithinDay(time) { | 53 function TimeWithinDay(time) { |
54 return Modulo(time, msPerDay); | 54 return Modulo(time, msPerDay); |
55 }; | 55 } |
56 | 56 |
57 | 57 |
58 // ECMA 262 - 15.9.1.3 | 58 // ECMA 262 - 15.9.1.3 |
59 function DaysInYear(year) { | 59 function DaysInYear(year) { |
60 if (year % 4 != 0) return 365; | 60 if (year % 4 != 0) return 365; |
61 if ((year % 100 == 0) && (year % 400 != 0)) return 365; | 61 if ((year % 100 == 0) && (year % 400 != 0)) return 365; |
62 return 366; | 62 return 366; |
63 }; | 63 } |
64 | 64 |
65 | 65 |
66 function DayFromYear(year) { | 66 function DayFromYear(year) { |
67 return 365 * (year-1970) | 67 return 365 * (year-1970) |
68 + $floor((year-1969)/4) | 68 + $floor((year-1969)/4) |
69 - $floor((year-1901)/100) | 69 - $floor((year-1901)/100) |
70 + $floor((year-1601)/400); | 70 + $floor((year-1601)/400); |
71 }; | 71 } |
72 | 72 |
73 | 73 |
74 function TimeFromYear(year) { | 74 function TimeFromYear(year) { |
75 return msPerDay * DayFromYear(year); | 75 return msPerDay * DayFromYear(year); |
76 }; | 76 } |
77 | 77 |
78 | 78 |
79 function YearFromTime(time) { | 79 function YearFromTime(time) { |
80 return FromJulianDay(Day(time) + kDayZeroInJulianDay).year; | 80 return FromJulianDay(Day(time) + kDayZeroInJulianDay).year; |
81 }; | 81 } |
82 | 82 |
83 | 83 |
84 function InLeapYear(time) { | 84 function InLeapYear(time) { |
85 return DaysInYear(YearFromTime(time)) == 366 ? 1 : 0; | 85 return DaysInYear(YearFromTime(time)) == 366 ? 1 : 0; |
86 }; | 86 } |
87 | 87 |
88 | 88 |
89 // ECMA 262 - 15.9.1.4 | 89 // ECMA 262 - 15.9.1.4 |
90 function MonthFromTime(time) { | 90 function MonthFromTime(time) { |
91 return FromJulianDay(Day(time) + kDayZeroInJulianDay).month; | 91 return FromJulianDay(Day(time) + kDayZeroInJulianDay).month; |
92 }; | 92 } |
93 | 93 |
94 | 94 |
95 function DayWithinYear(time) { | 95 function DayWithinYear(time) { |
96 return Day(time) - DayFromYear(YearFromTime(time)); | 96 return Day(time) - DayFromYear(YearFromTime(time)); |
97 }; | 97 } |
98 | 98 |
99 | 99 |
100 // ECMA 262 - 15.9.1.5 | 100 // ECMA 262 - 15.9.1.5 |
101 function DateFromTime(time) { | 101 function DateFromTime(time) { |
102 return FromJulianDay(Day(time) + kDayZeroInJulianDay).date; | 102 return FromJulianDay(Day(time) + kDayZeroInJulianDay).date; |
103 }; | 103 } |
104 | 104 |
105 | 105 |
106 // ECMA 262 - 15.9.1.9 | 106 // ECMA 262 - 15.9.1.9 |
107 function EquivalentYear(year) { | 107 function EquivalentYear(year) { |
108 // Returns an equivalent year in the range [1956-2000] matching | 108 // Returns an equivalent year in the range [1956-2000] matching |
109 // - leap year. | 109 // - leap year. |
110 // - week day of first day. | 110 // - week day of first day. |
111 var time = TimeFromYear(year); | 111 var time = TimeFromYear(year); |
112 return (InLeapYear(time) == 0 ? 1967 : 1956) + (WeekDay(time) * 12) % 28; | 112 return (InLeapYear(time) == 0 ? 1967 : 1956) + (WeekDay(time) * 12) % 28; |
113 }; | 113 } |
114 | 114 |
115 | 115 |
116 function EquivalentTime(t) { | 116 function EquivalentTime(t) { |
117 // The issue here is that some library calls don't work right for dates | 117 // The issue here is that some library calls don't work right for dates |
118 // that cannot be represented using a signed 32 bit integer (measured in | 118 // that cannot be represented using a signed 32 bit integer (measured in |
119 // whole seconds based on the 1970 epoch). | 119 // whole seconds based on the 1970 epoch). |
120 // We solve this by mapping the time to a year with same leap-year-ness | 120 // We solve this by mapping the time to a year with same leap-year-ness |
121 // and same starting day for the year. | 121 // and same starting day for the year. |
122 // As an optimization we avoid finding an equivalent year in the common | 122 // As an optimization we avoid finding an equivalent year in the common |
123 // case. We are measuring in ms here so the 32 bit signed integer range | 123 // case. We are measuring in ms here so the 32 bit signed integer range |
124 // is +-(1<<30)*1000 ie approximately +-2.1e20. | 124 // is +-(1<<30)*1000 ie approximately +-2.1e20. |
125 if (t >= -2.1e12 && t <= 2.1e12) return t; | 125 if (t >= -2.1e12 && t <= 2.1e12) return t; |
126 var day = MakeDay(EquivalentYear(YearFromTime(t)), MonthFromTime(t), DateFromT
ime(t)); | 126 var day = MakeDay(EquivalentYear(YearFromTime(t)), MonthFromTime(t), DateFromT
ime(t)); |
127 return TimeClip(MakeDate(day, TimeWithinDay(t))); | 127 return TimeClip(MakeDate(day, TimeWithinDay(t))); |
128 }; | 128 } |
129 | 129 |
130 | 130 |
131 var local_time_offset; | 131 var local_time_offset; |
132 | 132 |
133 function LocalTimeOffset() { | 133 function LocalTimeOffset() { |
134 if (IS_UNDEFINED(local_time_offset)) { | 134 if (IS_UNDEFINED(local_time_offset)) { |
135 local_time_offset = %DateLocalTimeOffset(); | 135 local_time_offset = %DateLocalTimeOffset(); |
136 } | 136 } |
137 return local_time_offset; | 137 return local_time_offset; |
138 }; | 138 } |
139 | 139 |
140 | 140 |
141 var daylight_cache_time = $NaN; | 141 var daylight_cache_time = $NaN; |
142 var daylight_cache_offset; | 142 var daylight_cache_offset; |
143 | 143 |
144 function DaylightSavingsOffset(t) { | 144 function DaylightSavingsOffset(t) { |
145 if (t == daylight_cache_time) { | 145 if (t == daylight_cache_time) { |
146 return daylight_cache_offset; | 146 return daylight_cache_offset; |
147 } | 147 } |
148 var offset = %DateDaylightSavingsOffset(EquivalentTime(t)); | 148 var offset = %DateDaylightSavingsOffset(EquivalentTime(t)); |
149 daylight_cache_time = t; | 149 daylight_cache_time = t; |
150 daylight_cache_offset = offset; | 150 daylight_cache_offset = offset; |
151 return offset; | 151 return offset; |
152 }; | 152 } |
153 | 153 |
154 | 154 |
155 var timezone_cache_time = $NaN; | 155 var timezone_cache_time = $NaN; |
156 var timezone_cache_timezone; | 156 var timezone_cache_timezone; |
157 | 157 |
158 function LocalTimezone(t) { | 158 function LocalTimezone(t) { |
159 if(t == timezone_cache_time) { | 159 if(t == timezone_cache_time) { |
160 return timezone_cache_timezone; | 160 return timezone_cache_timezone; |
161 } | 161 } |
162 var timezone = %DateLocalTimezone(EquivalentTime(t)); | 162 var timezone = %DateLocalTimezone(EquivalentTime(t)); |
163 timezone_cache_time = t; | 163 timezone_cache_time = t; |
164 timezone_cache_timezone = timezone; | 164 timezone_cache_timezone = timezone; |
165 return timezone; | 165 return timezone; |
166 }; | 166 } |
167 | 167 |
168 | 168 |
169 function WeekDay(time) { | 169 function WeekDay(time) { |
170 return Modulo(Day(time) + 4, 7); | 170 return Modulo(Day(time) + 4, 7); |
171 }; | 171 } |
172 | 172 |
173 | 173 |
174 function LocalTime(time) { | 174 function LocalTime(time) { |
175 if ($isNaN(time)) return time; | 175 if ($isNaN(time)) return time; |
176 return time + LocalTimeOffset() + DaylightSavingsOffset(time); | 176 return time + LocalTimeOffset() + DaylightSavingsOffset(time); |
177 }; | 177 } |
178 | 178 |
179 | 179 |
180 function UTC(time) { | 180 function UTC(time) { |
181 if ($isNaN(time)) return time; | 181 if ($isNaN(time)) return time; |
182 var tmp = time - LocalTimeOffset(); | 182 var tmp = time - LocalTimeOffset(); |
183 return tmp - DaylightSavingsOffset(tmp); | 183 return tmp - DaylightSavingsOffset(tmp); |
184 }; | 184 } |
185 | 185 |
186 | 186 |
187 // ECMA 262 - 15.9.1.10 | 187 // ECMA 262 - 15.9.1.10 |
188 function HourFromTime(time) { | 188 function HourFromTime(time) { |
189 return Modulo($floor(time / msPerHour), HoursPerDay); | 189 return Modulo($floor(time / msPerHour), HoursPerDay); |
190 }; | 190 } |
191 | 191 |
192 | 192 |
193 function MinFromTime(time) { | 193 function MinFromTime(time) { |
194 return Modulo($floor(time / msPerMinute), MinutesPerHour); | 194 return Modulo($floor(time / msPerMinute), MinutesPerHour); |
195 }; | 195 } |
196 | 196 |
197 | 197 |
198 function SecFromTime(time) { | 198 function SecFromTime(time) { |
199 return Modulo($floor(time / msPerSecond), SecondsPerMinute); | 199 return Modulo($floor(time / msPerSecond), SecondsPerMinute); |
200 }; | 200 } |
201 | 201 |
202 | 202 |
203 function msFromTime(time) { | 203 function msFromTime(time) { |
204 return Modulo(time, msPerSecond); | 204 return Modulo(time, msPerSecond); |
205 }; | 205 } |
206 | 206 |
207 | 207 |
208 // ECMA 262 - 15.9.1.11 | 208 // ECMA 262 - 15.9.1.11 |
209 function MakeTime(hour, min, sec, ms) { | 209 function MakeTime(hour, min, sec, ms) { |
210 if (!$isFinite(hour)) return $NaN; | 210 if (!$isFinite(hour)) return $NaN; |
211 if (!$isFinite(min)) return $NaN; | 211 if (!$isFinite(min)) return $NaN; |
212 if (!$isFinite(sec)) return $NaN; | 212 if (!$isFinite(sec)) return $NaN; |
213 if (!$isFinite(ms)) return $NaN; | 213 if (!$isFinite(ms)) return $NaN; |
214 return TO_INTEGER(hour) * msPerHour | 214 return TO_INTEGER(hour) * msPerHour |
215 + TO_INTEGER(min) * msPerMinute | 215 + TO_INTEGER(min) * msPerMinute |
216 + TO_INTEGER(sec) * msPerSecond | 216 + TO_INTEGER(sec) * msPerSecond |
217 + TO_INTEGER(ms); | 217 + TO_INTEGER(ms); |
218 }; | 218 } |
219 | 219 |
220 | 220 |
221 // ECMA 262 - 15.9.1.12 | 221 // ECMA 262 - 15.9.1.12 |
222 function TimeInYear(year) { | 222 function TimeInYear(year) { |
223 return DaysInYear(year) * msPerDay; | 223 return DaysInYear(year) * msPerDay; |
224 }; | 224 } |
225 | 225 |
226 | 226 |
227 // Compute modified Julian day from year, month, date. | 227 // Compute modified Julian day from year, month, date. |
228 // The missing days in 1582 are ignored for JavaScript compatibility. | 228 // The missing days in 1582 are ignored for JavaScript compatibility. |
229 function ToJulianDay(year, month, date) { | 229 function ToJulianDay(year, month, date) { |
230 var jy = (month > 1) ? year : year - 1; | 230 var jy = (month > 1) ? year : year - 1; |
231 var jm = (month > 1) ? month + 2 : month + 14; | 231 var jm = (month > 1) ? month + 2 : month + 14; |
232 var ja = $floor(0.01*jy); | 232 var ja = $floor(0.01*jy); |
233 return $floor($floor(365.25*jy) + $floor(30.6001*jm) + date + 1720995) + 2 - j
a + $floor(0.25*ja); | 233 return $floor($floor(365.25*jy) + $floor(30.6001*jm) + date + 1720995) + 2 - j
a + $floor(0.25*ja); |
234 }; | 234 } |
235 | 235 |
236 | 236 |
237 var four_year_cycle_table; | 237 var four_year_cycle_table; |
238 | 238 |
239 | 239 |
240 function CalculateDateTable() { | 240 function CalculateDateTable() { |
241 var month_lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; | 241 var month_lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
242 var four_year_cycle_table = new $Array(1461); | 242 var four_year_cycle_table = new $Array(1461); |
243 | 243 |
244 var cumulative = 0; | 244 var cumulative = 0; |
(...skipping 12 matching lines...) Expand all Loading... |
257 (3 << kYearShift) + (month << kMonthShift) + day; | 257 (3 << kYearShift) + (month << kMonthShift) + day; |
258 leap_position++; | 258 leap_position++; |
259 position++; | 259 position++; |
260 } | 260 } |
261 if (month == 1) { | 261 if (month == 1) { |
262 four_year_cycle_table[leap_position++] = | 262 four_year_cycle_table[leap_position++] = |
263 (month << kMonthShift) + 29; | 263 (month << kMonthShift) + 29; |
264 } | 264 } |
265 } | 265 } |
266 return four_year_cycle_table; | 266 return four_year_cycle_table; |
267 }; | 267 } |
268 | 268 |
269 | 269 |
270 | 270 |
271 // Constructor for creating objects holding year, month, and date. | 271 // Constructor for creating objects holding year, month, and date. |
272 // Introduced to ensure the two return points in FromJulianDay match same map. | 272 // Introduced to ensure the two return points in FromJulianDay match same map. |
273 function DayTriplet(year, month, date) { | 273 function DayTriplet(year, month, date) { |
274 this.year = year; | 274 this.year = year; |
275 this.month = month; | 275 this.month = month; |
276 this.date = date; | 276 this.date = date; |
277 } | 277 } |
278 | 278 |
| 279 |
279 // Compute year, month, and day from modified Julian day. | 280 // Compute year, month, and day from modified Julian day. |
280 // The missing days in 1582 are ignored for JavaScript compatibility. | 281 // The missing days in 1582 are ignored for JavaScript compatibility. |
281 function FromJulianDay(julian) { | 282 function FromJulianDay(julian) { |
282 // Avoid floating point and non-Smi maths in common case. This is also a peri
od of | 283 // Avoid floating point and non-Smi maths in common case. This is also a peri
od of |
283 // time where leap years are very regular. The range is not too large to avoi
d overflow | 284 // time where leap years are very regular. The range is not too large to avoi
d overflow |
284 // when doing the multiply-to-divide trick. | 285 // when doing the multiply-to-divide trick. |
285 if (julian > kDayZeroInJulianDay && | 286 if (julian > kDayZeroInJulianDay && |
286 (julian - kDayZeroInJulianDay) < 40177) { // 1970 - 2080 | 287 (julian - kDayZeroInJulianDay) < 40177) { // 1970 - 2080 |
287 if (!four_year_cycle_table) | 288 if (!four_year_cycle_table) |
288 four_year_cycle_table = CalculateDateTable(); | 289 four_year_cycle_table = CalculateDateTable(); |
(...skipping 12 matching lines...) Expand all Loading... |
301 var jb = julian + 1 + jalpha - $floor(0.25 * jalpha) + 1524; | 302 var jb = julian + 1 + jalpha - $floor(0.25 * jalpha) + 1524; |
302 var jc = $floor(6680.0 + ((jb-2439870) - 122.1)/365.25); | 303 var jc = $floor(6680.0 + ((jb-2439870) - 122.1)/365.25); |
303 var jd = $floor(365 * jc + (0.25 * jc)); | 304 var jd = $floor(365 * jc + (0.25 * jc)); |
304 var je = $floor((jb - jd)/30.6001); | 305 var je = $floor((jb - jd)/30.6001); |
305 var m = je - 1; | 306 var m = je - 1; |
306 if (m > 12) m -= 13; | 307 if (m > 12) m -= 13; |
307 var y = jc - 4715; | 308 var y = jc - 4715; |
308 if (m > 2) { --y; --m; } | 309 if (m > 2) { --y; --m; } |
309 var d = jb - jd - $floor(30.6001 * je); | 310 var d = jb - jd - $floor(30.6001 * je); |
310 return new DayTriplet(y, m, d); | 311 return new DayTriplet(y, m, d); |
311 }; | 312 } |
312 | 313 |
313 // Compute number of days given a year, month, date. | 314 // Compute number of days given a year, month, date. |
314 // Note that month and date can lie outside the normal range. | 315 // Note that month and date can lie outside the normal range. |
315 // For example: | 316 // For example: |
316 // MakeDay(2007, -4, 20) --> MakeDay(2006, 8, 20) | 317 // MakeDay(2007, -4, 20) --> MakeDay(2006, 8, 20) |
317 // MakeDay(2007, -33, 1) --> MakeDay(2004, 3, 1) | 318 // MakeDay(2007, -33, 1) --> MakeDay(2004, 3, 1) |
318 // MakeDay(2007, 14, -50) --> MakeDay(2007, 8, 11) | 319 // MakeDay(2007, 14, -50) --> MakeDay(2007, 8, 11) |
319 function MakeDay(year, month, date) { | 320 function MakeDay(year, month, date) { |
320 if (!$isFinite(year) || !$isFinite(month) || !$isFinite(date)) return $NaN; | 321 if (!$isFinite(year) || !$isFinite(month) || !$isFinite(date)) return $NaN; |
321 | 322 |
322 // Conversion to integers. | 323 // Conversion to integers. |
323 year = TO_INTEGER(year); | 324 year = TO_INTEGER(year); |
324 month = TO_INTEGER(month); | 325 month = TO_INTEGER(month); |
325 date = TO_INTEGER(date); | 326 date = TO_INTEGER(date); |
326 | 327 |
327 // Overflow months into year. | 328 // Overflow months into year. |
328 year = year + $floor(month/12); | 329 year = year + $floor(month/12); |
329 month = month % 12; | 330 month = month % 12; |
330 if (month < 0) { | 331 if (month < 0) { |
331 month += 12; | 332 month += 12; |
332 } | 333 } |
333 | 334 |
334 // Return days relative to Jan 1 1970. | 335 // Return days relative to Jan 1 1970. |
335 return ToJulianDay(year, month, date) - kDayZeroInJulianDay; | 336 return ToJulianDay(year, month, date) - kDayZeroInJulianDay; |
336 }; | 337 } |
337 | 338 |
338 | 339 |
339 // ECMA 262 - 15.9.1.13 | 340 // ECMA 262 - 15.9.1.13 |
340 function MakeDate(day, time) { | 341 function MakeDate(day, time) { |
341 if (!$isFinite(day)) return $NaN; | 342 if (!$isFinite(day)) return $NaN; |
342 if (!$isFinite(time)) return $NaN; | 343 if (!$isFinite(time)) return $NaN; |
343 return day * msPerDay + time; | 344 return day * msPerDay + time; |
344 }; | 345 } |
345 | 346 |
346 | 347 |
347 // ECMA 262 - 15.9.1.14 | 348 // ECMA 262 - 15.9.1.14 |
348 function TimeClip(time) { | 349 function TimeClip(time) { |
349 if (!$isFinite(time)) return $NaN; | 350 if (!$isFinite(time)) return $NaN; |
350 if ($abs(time) > 8.64E15) return $NaN; | 351 if ($abs(time) > 8.64E15) return $NaN; |
351 return TO_INTEGER(time); | 352 return TO_INTEGER(time); |
352 }; | 353 } |
353 | 354 |
354 | 355 |
355 %SetCode($Date, function(year, month, date, hours, minutes, seconds, ms) { | 356 %SetCode($Date, function(year, month, date, hours, minutes, seconds, ms) { |
356 if (%IsConstructCall()) { | 357 if (%IsConstructCall()) { |
357 // ECMA 262 - 15.9.3 | 358 // ECMA 262 - 15.9.3 |
358 var argc = %_ArgumentsLength(); | 359 var argc = %_ArgumentsLength(); |
359 if (argc == 0) { | 360 if (argc == 0) { |
360 %_SetValueOf(this, %DateCurrentTime()); | 361 %_SetValueOf(this, %DateCurrentTime()); |
361 return; | 362 return; |
362 } | 363 } |
(...skipping 29 matching lines...) Expand all Loading... |
392 // ECMA 262 - 15.9.2 | 393 // ECMA 262 - 15.9.2 |
393 return (new $Date()).toString(); | 394 return (new $Date()).toString(); |
394 } | 395 } |
395 }); | 396 }); |
396 | 397 |
397 | 398 |
398 // Helper functions. | 399 // Helper functions. |
399 function GetTimeFrom(aDate) { | 400 function GetTimeFrom(aDate) { |
400 if (IS_DATE(aDate)) return %_ValueOf(aDate); | 401 if (IS_DATE(aDate)) return %_ValueOf(aDate); |
401 throw new $TypeError('this is not a Date object.'); | 402 throw new $TypeError('this is not a Date object.'); |
402 }; | 403 } |
403 | 404 |
404 | 405 |
405 function GetMillisecondsFrom(aDate) { | 406 function GetMillisecondsFrom(aDate) { |
406 var t = GetTimeFrom(aDate); | 407 var t = GetTimeFrom(aDate); |
407 if ($isNaN(t)) return t; | 408 if ($isNaN(t)) return t; |
408 return msFromTime(LocalTime(t)); | 409 return msFromTime(LocalTime(t)); |
409 }; | 410 } |
410 | 411 |
411 | 412 |
412 function GetUTCMillisecondsFrom(aDate) { | 413 function GetUTCMillisecondsFrom(aDate) { |
413 var t = GetTimeFrom(aDate); | 414 var t = GetTimeFrom(aDate); |
414 if ($isNaN(t)) return t; | 415 if ($isNaN(t)) return t; |
415 return msFromTime(t); | 416 return msFromTime(t); |
416 }; | 417 } |
417 | 418 |
418 | 419 |
419 function GetSecondsFrom(aDate) { | 420 function GetSecondsFrom(aDate) { |
420 var t = GetTimeFrom(aDate); | 421 var t = GetTimeFrom(aDate); |
421 if ($isNaN(t)) return t; | 422 if ($isNaN(t)) return t; |
422 return SecFromTime(LocalTime(t)); | 423 return SecFromTime(LocalTime(t)); |
423 }; | 424 } |
424 | 425 |
425 | 426 |
426 function GetUTCSecondsFrom(aDate) { | 427 function GetUTCSecondsFrom(aDate) { |
427 var t = GetTimeFrom(aDate); | 428 var t = GetTimeFrom(aDate); |
428 if ($isNaN(t)) return t; | 429 if ($isNaN(t)) return t; |
429 return SecFromTime(t); | 430 return SecFromTime(t); |
430 }; | 431 } |
431 | 432 |
432 | 433 |
433 function GetMinutesFrom(aDate) { | 434 function GetMinutesFrom(aDate) { |
434 var t = GetTimeFrom(aDate); | 435 var t = GetTimeFrom(aDate); |
435 if ($isNaN(t)) return t; | 436 if ($isNaN(t)) return t; |
436 return MinFromTime(LocalTime(t)); | 437 return MinFromTime(LocalTime(t)); |
437 }; | 438 } |
438 | 439 |
439 | 440 |
440 function GetUTCMinutesFrom(aDate) { | 441 function GetUTCMinutesFrom(aDate) { |
441 var t = GetTimeFrom(aDate); | 442 var t = GetTimeFrom(aDate); |
442 if ($isNaN(t)) return t; | 443 if ($isNaN(t)) return t; |
443 return MinFromTime(t); | 444 return MinFromTime(t); |
444 }; | 445 } |
445 | 446 |
446 | 447 |
447 function GetHoursFrom(aDate) { | 448 function GetHoursFrom(aDate) { |
448 var t = GetTimeFrom(aDate); | 449 var t = GetTimeFrom(aDate); |
449 if ($isNaN(t)) return t; | 450 if ($isNaN(t)) return t; |
450 return HourFromTime(LocalTime(t)); | 451 return HourFromTime(LocalTime(t)); |
451 }; | 452 } |
452 | 453 |
453 | 454 |
454 function GetUTCHoursFrom(aDate) { | 455 function GetUTCHoursFrom(aDate) { |
455 var t = GetTimeFrom(aDate); | 456 var t = GetTimeFrom(aDate); |
456 if ($isNaN(t)) return t; | 457 if ($isNaN(t)) return t; |
457 return HourFromTime(t); | 458 return HourFromTime(t); |
458 }; | 459 } |
459 | 460 |
460 | 461 |
461 function GetFullYearFrom(aDate) { | 462 function GetFullYearFrom(aDate) { |
462 var t = GetTimeFrom(aDate); | 463 var t = GetTimeFrom(aDate); |
463 if ($isNaN(t)) return t; | 464 if ($isNaN(t)) return t; |
464 return YearFromTime(LocalTime(t)); | 465 return YearFromTime(LocalTime(t)); |
465 }; | 466 } |
466 | 467 |
467 | 468 |
468 function GetUTCFullYearFrom(aDate) { | 469 function GetUTCFullYearFrom(aDate) { |
469 var t = GetTimeFrom(aDate); | 470 var t = GetTimeFrom(aDate); |
470 if ($isNaN(t)) return t; | 471 if ($isNaN(t)) return t; |
471 return YearFromTime(t); | 472 return YearFromTime(t); |
472 }; | 473 } |
473 | 474 |
474 | 475 |
475 function GetMonthFrom(aDate) { | 476 function GetMonthFrom(aDate) { |
476 var t = GetTimeFrom(aDate); | 477 var t = GetTimeFrom(aDate); |
477 if ($isNaN(t)) return t; | 478 if ($isNaN(t)) return t; |
478 return MonthFromTime(LocalTime(t)); | 479 return MonthFromTime(LocalTime(t)); |
479 }; | 480 } |
480 | 481 |
481 | 482 |
482 function GetUTCMonthFrom(aDate) { | 483 function GetUTCMonthFrom(aDate) { |
483 var t = GetTimeFrom(aDate); | 484 var t = GetTimeFrom(aDate); |
484 if ($isNaN(t)) return t; | 485 if ($isNaN(t)) return t; |
485 return MonthFromTime(t); | 486 return MonthFromTime(t); |
486 }; | 487 } |
487 | 488 |
488 | 489 |
489 function GetDateFrom(aDate) { | 490 function GetDateFrom(aDate) { |
490 var t = GetTimeFrom(aDate); | 491 var t = GetTimeFrom(aDate); |
491 if ($isNaN(t)) return t; | 492 if ($isNaN(t)) return t; |
492 return DateFromTime(LocalTime(t)); | 493 return DateFromTime(LocalTime(t)); |
493 }; | 494 } |
494 | 495 |
495 | 496 |
496 function GetUTCDateFrom(aDate) { | 497 function GetUTCDateFrom(aDate) { |
497 var t = GetTimeFrom(aDate); | 498 var t = GetTimeFrom(aDate); |
498 if ($isNaN(t)) return t; | 499 if ($isNaN(t)) return t; |
499 return DateFromTime(t); | 500 return DateFromTime(t); |
500 }; | 501 } |
501 | 502 |
502 | 503 |
503 %FunctionSetPrototype($Date, new $Date($NaN)); | 504 %FunctionSetPrototype($Date, new $Date($NaN)); |
504 | 505 |
505 | 506 |
506 var WeekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; | 507 var WeekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; |
507 var Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oc
t', 'Nov', 'Dec']; | 508 var Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oc
t', 'Nov', 'Dec']; |
508 | 509 |
509 | 510 |
510 function TwoDigitString(value) { | 511 function TwoDigitString(value) { |
511 return value < 10 ? "0" + value : "" + value; | 512 return value < 10 ? "0" + value : "" + value; |
512 }; | 513 } |
513 | 514 |
514 | 515 |
515 function DateString(time) { | 516 function DateString(time) { |
516 var YMD = FromJulianDay(Day(time) + kDayZeroInJulianDay); | 517 var YMD = FromJulianDay(Day(time) + kDayZeroInJulianDay); |
517 return WeekDays[WeekDay(time)] + ' ' | 518 return WeekDays[WeekDay(time)] + ' ' |
518 + Months[YMD.month] + ' ' | 519 + Months[YMD.month] + ' ' |
519 + TwoDigitString(YMD.date) + ' ' | 520 + TwoDigitString(YMD.date) + ' ' |
520 + YMD.year; | 521 + YMD.year; |
521 }; | 522 } |
522 | 523 |
523 | 524 |
524 function TimeString(time) { | 525 function TimeString(time) { |
525 return TwoDigitString(HourFromTime(time)) + ':' | 526 return TwoDigitString(HourFromTime(time)) + ':' |
526 + TwoDigitString(MinFromTime(time)) + ':' | 527 + TwoDigitString(MinFromTime(time)) + ':' |
527 + TwoDigitString(SecFromTime(time)); | 528 + TwoDigitString(SecFromTime(time)); |
528 }; | 529 } |
529 | 530 |
530 | 531 |
531 function LocalTimezoneString(time) { | 532 function LocalTimezoneString(time) { |
532 var timezoneOffset = (LocalTimeOffset() + DaylightSavingsOffset(time)) / msPer
Minute; | 533 var timezoneOffset = (LocalTimeOffset() + DaylightSavingsOffset(time)) / msPer
Minute; |
533 var sign = (timezoneOffset >= 0) ? 1 : -1; | 534 var sign = (timezoneOffset >= 0) ? 1 : -1; |
534 var hours = $floor((sign * timezoneOffset)/60); | 535 var hours = $floor((sign * timezoneOffset)/60); |
535 var min = $floor((sign * timezoneOffset)%60); | 536 var min = $floor((sign * timezoneOffset)%60); |
536 var gmt = ' GMT' + ((sign == 1) ? '+' : '-') + TwoDigitString(hours) + TwoDigi
tString(min); | 537 var gmt = ' GMT' + ((sign == 1) ? '+' : '-') + TwoDigitString(hours) + TwoDigi
tString(min); |
537 return gmt + ' (' + LocalTimezone(time) + ')'; | 538 return gmt + ' (' + LocalTimezone(time) + ')'; |
538 }; | 539 } |
539 | 540 |
540 | 541 |
541 function DatePrintString(time) { | 542 function DatePrintString(time) { |
542 return DateString(time) + ' ' + TimeString(time); | 543 return DateString(time) + ' ' + TimeString(time); |
543 }; | 544 } |
544 | 545 |
545 // ------------------------------------------------------------------- | 546 // ------------------------------------------------------------------- |
546 | 547 |
547 | 548 |
548 // ECMA 262 - 15.9.4.2 | 549 // ECMA 262 - 15.9.4.2 |
549 function DateParse(string) { | 550 function DateParse(string) { |
550 var arr = %DateParseString(ToString(string)); | 551 var arr = %DateParseString(ToString(string)); |
551 if (IS_NULL(arr)) return $NaN; | 552 if (IS_NULL(arr)) return $NaN; |
552 | 553 |
553 var day = MakeDay(arr[0], arr[1], arr[2]); | 554 var day = MakeDay(arr[0], arr[1], arr[2]); |
554 var time = MakeTime(arr[3], arr[4], arr[5], 0); | 555 var time = MakeTime(arr[3], arr[4], arr[5], 0); |
555 var date = MakeDate(day, time); | 556 var date = MakeDate(day, time); |
556 | 557 |
557 if (IS_NULL(arr[6])) { | 558 if (IS_NULL(arr[6])) { |
558 return TimeClip(UTC(date)); | 559 return TimeClip(UTC(date)); |
559 } else { | 560 } else { |
560 return TimeClip(date - arr[6] * 1000); | 561 return TimeClip(date - arr[6] * 1000); |
561 } | 562 } |
562 }; | 563 } |
563 | 564 |
564 | 565 |
565 // ECMA 262 - 15.9.4.3 | 566 // ECMA 262 - 15.9.4.3 |
566 function DateUTC(year, month, date, hours, minutes, seconds, ms) { | 567 function DateUTC(year, month, date, hours, minutes, seconds, ms) { |
567 year = ToNumber(year); | 568 year = ToNumber(year); |
568 month = ToNumber(month); | 569 month = ToNumber(month); |
569 var argc = %_ArgumentsLength(); | 570 var argc = %_ArgumentsLength(); |
570 date = argc > 2 ? ToNumber(date) : 1; | 571 date = argc > 2 ? ToNumber(date) : 1; |
571 hours = argc > 3 ? ToNumber(hours) : 0; | 572 hours = argc > 3 ? ToNumber(hours) : 0; |
572 minutes = argc > 4 ? ToNumber(minutes) : 0; | 573 minutes = argc > 4 ? ToNumber(minutes) : 0; |
573 seconds = argc > 5 ? ToNumber(seconds) : 0; | 574 seconds = argc > 5 ? ToNumber(seconds) : 0; |
574 ms = argc > 6 ? ToNumber(ms) : 0; | 575 ms = argc > 6 ? ToNumber(ms) : 0; |
575 year = (!$isNaN(year) && 0 <= TO_INTEGER(year) && TO_INTEGER(year) <= 99) | 576 year = (!$isNaN(year) && 0 <= TO_INTEGER(year) && TO_INTEGER(year) <= 99) |
576 ? 1900 + TO_INTEGER(year) : year; | 577 ? 1900 + TO_INTEGER(year) : year; |
577 var day = MakeDay(year, month, date); | 578 var day = MakeDay(year, month, date); |
578 var time = MakeTime(hours, minutes, seconds, ms); | 579 var time = MakeTime(hours, minutes, seconds, ms); |
579 return %_SetValueOf(this, TimeClip(MakeDate(day, time))); | 580 return %_SetValueOf(this, TimeClip(MakeDate(day, time))); |
580 }; | 581 } |
581 | 582 |
582 | 583 |
583 // Mozilla-specific extension. Returns the number of milliseconds | 584 // Mozilla-specific extension. Returns the number of milliseconds |
584 // elapsed since 1 January 1970 00:00:00 UTC. | 585 // elapsed since 1 January 1970 00:00:00 UTC. |
585 function DateNow() { | 586 function DateNow() { |
586 return %DateCurrentTime(); | 587 return %DateCurrentTime(); |
587 }; | 588 } |
588 | 589 |
589 | 590 |
590 // ECMA 262 - 15.9.5.2 | 591 // ECMA 262 - 15.9.5.2 |
591 function DateToString() { | 592 function DateToString() { |
592 var t = GetTimeFrom(this); | 593 var t = GetTimeFrom(this); |
593 if ($isNaN(t)) return kInvalidDate; | 594 if ($isNaN(t)) return kInvalidDate; |
594 return DatePrintString(LocalTime(t)) + LocalTimezoneString(t); | 595 return DatePrintString(LocalTime(t)) + LocalTimezoneString(t); |
595 }; | 596 } |
596 | 597 |
597 | 598 |
598 // ECMA 262 - 15.9.5.3 | 599 // ECMA 262 - 15.9.5.3 |
599 function DateToDateString() { | 600 function DateToDateString() { |
600 var t = GetTimeFrom(this); | 601 var t = GetTimeFrom(this); |
601 if ($isNaN(t)) return kInvalidDate; | 602 if ($isNaN(t)) return kInvalidDate; |
602 return DateString(LocalTime(t)); | 603 return DateString(LocalTime(t)); |
603 }; | 604 } |
604 | 605 |
605 | 606 |
606 // ECMA 262 - 15.9.5.4 | 607 // ECMA 262 - 15.9.5.4 |
607 function DateToTimeString() { | 608 function DateToTimeString() { |
608 var t = GetTimeFrom(this); | 609 var t = GetTimeFrom(this); |
609 if ($isNaN(t)) return kInvalidDate; | 610 if ($isNaN(t)) return kInvalidDate; |
610 var lt = LocalTime(t); | 611 var lt = LocalTime(t); |
611 return TimeString(lt) + LocalTimezoneString(lt); | 612 return TimeString(lt) + LocalTimezoneString(lt); |
612 }; | 613 } |
| 614 |
| 615 |
| 616 // ECMA 262 - 15.9.5.5 |
| 617 function DateToLocaleString() { |
| 618 return DateToString.call(this); |
| 619 } |
| 620 |
| 621 |
| 622 // ECMA 262 - 15.9.5.6 |
| 623 function DateToLocaleDateString() { |
| 624 return DateToDateString.call(this); |
| 625 } |
613 | 626 |
614 | 627 |
615 // ECMA 262 - 15.9.5.7 | 628 // ECMA 262 - 15.9.5.7 |
616 function DateToLocaleTimeString() { | 629 function DateToLocaleTimeString() { |
617 var t = GetTimeFrom(this); | 630 var t = GetTimeFrom(this); |
618 if ($isNaN(t)) return kInvalidDate; | 631 if ($isNaN(t)) return kInvalidDate; |
619 var lt = LocalTime(t); | 632 var lt = LocalTime(t); |
620 return TimeString(lt); | 633 return TimeString(lt); |
621 }; | 634 } |
| 635 |
| 636 |
| 637 // ECMA 262 - 15.9.5.8 |
| 638 function DateValueOf() { |
| 639 return GetTimeFrom(this); |
| 640 } |
622 | 641 |
623 | 642 |
624 // ECMA 262 - 15.9.5.9 | 643 // ECMA 262 - 15.9.5.9 |
625 function DateGetTime() { | 644 function DateGetTime() { |
626 return GetTimeFrom(this); | 645 return GetTimeFrom(this); |
627 } | 646 } |
628 | 647 |
629 | 648 |
630 // ECMA 262 - 15.9.5.10 | 649 // ECMA 262 - 15.9.5.10 |
631 function DateGetFullYear() { | 650 function DateGetFullYear() { |
632 return GetFullYearFrom(this) | 651 return GetFullYearFrom(this) |
633 }; | 652 } |
634 | 653 |
635 | 654 |
636 // ECMA 262 - 15.9.5.11 | 655 // ECMA 262 - 15.9.5.11 |
637 function DateGetUTCFullYear() { | 656 function DateGetUTCFullYear() { |
638 return GetUTCFullYearFrom(this) | 657 return GetUTCFullYearFrom(this) |
639 }; | 658 } |
640 | 659 |
641 | 660 |
642 // ECMA 262 - 15.9.5.12 | 661 // ECMA 262 - 15.9.5.12 |
643 function DateGetMonth() { | 662 function DateGetMonth() { |
644 return GetMonthFrom(this); | 663 return GetMonthFrom(this); |
645 }; | 664 } |
646 | 665 |
647 | 666 |
648 // ECMA 262 - 15.9.5.13 | 667 // ECMA 262 - 15.9.5.13 |
649 function DateGetUTCMonth() { | 668 function DateGetUTCMonth() { |
650 return GetUTCMonthFrom(this); | 669 return GetUTCMonthFrom(this); |
651 }; | 670 } |
652 | 671 |
653 | 672 |
654 // ECMA 262 - 15.9.5.14 | 673 // ECMA 262 - 15.9.5.14 |
655 function DateGetDate() { | 674 function DateGetDate() { |
656 return GetDateFrom(this); | 675 return GetDateFrom(this); |
657 }; | 676 } |
658 | 677 |
659 | 678 |
660 // ECMA 262 - 15.9.5.15 | 679 // ECMA 262 - 15.9.5.15 |
661 function DateGetUTCDate() { | 680 function DateGetUTCDate() { |
662 return GetUTCDateFrom(this); | 681 return GetUTCDateFrom(this); |
663 }; | 682 } |
664 | 683 |
665 | 684 |
666 // ECMA 262 - 15.9.5.16 | 685 // ECMA 262 - 15.9.5.16 |
667 function DateGetDay() { | 686 function DateGetDay() { |
668 var t = GetTimeFrom(this); | 687 var t = GetTimeFrom(this); |
669 if ($isNaN(t)) return t; | 688 if ($isNaN(t)) return t; |
670 return WeekDay(LocalTime(t)); | 689 return WeekDay(LocalTime(t)); |
671 }; | 690 } |
672 | 691 |
673 | 692 |
674 // ECMA 262 - 15.9.5.17 | 693 // ECMA 262 - 15.9.5.17 |
675 function DateGetUTCDay() { | 694 function DateGetUTCDay() { |
676 var t = GetTimeFrom(this); | 695 var t = GetTimeFrom(this); |
677 if ($isNaN(t)) return t; | 696 if ($isNaN(t)) return t; |
678 return WeekDay(t); | 697 return WeekDay(t); |
679 }; | 698 } |
680 | 699 |
681 | 700 |
682 // ECMA 262 - 15.9.5.18 | 701 // ECMA 262 - 15.9.5.18 |
683 function DateGetHours() { | 702 function DateGetHours() { |
684 return GetHoursFrom(this); | 703 return GetHoursFrom(this); |
685 }; | 704 } |
686 | 705 |
687 | 706 |
688 // ECMA 262 - 15.9.5.19 | 707 // ECMA 262 - 15.9.5.19 |
689 function DateGetUTCHours() { | 708 function DateGetUTCHours() { |
690 return GetUTCHoursFrom(this); | 709 return GetUTCHoursFrom(this); |
691 }; | 710 } |
692 | 711 |
693 | 712 |
694 // ECMA 262 - 15.9.5.20 | 713 // ECMA 262 - 15.9.5.20 |
695 function DateGetMinutes() { | 714 function DateGetMinutes() { |
696 return GetMinutesFrom(this); | 715 return GetMinutesFrom(this); |
697 }; | 716 } |
698 | 717 |
699 | 718 |
700 // ECMA 262 - 15.9.5.21 | 719 // ECMA 262 - 15.9.5.21 |
701 function DateGetUTCMinutes() { | 720 function DateGetUTCMinutes() { |
702 return GetUTCMinutesFrom(this); | 721 return GetUTCMinutesFrom(this); |
703 }; | 722 } |
704 | 723 |
705 | 724 |
706 // ECMA 262 - 15.9.5.22 | 725 // ECMA 262 - 15.9.5.22 |
707 function DateGetSeconds() { | 726 function DateGetSeconds() { |
708 return GetSecondsFrom(this); | 727 return GetSecondsFrom(this); |
709 }; | 728 } |
710 | 729 |
711 | 730 |
712 // ECMA 262 - 15.9.5.23 | 731 // ECMA 262 - 15.9.5.23 |
713 function DateGetUTCSeconds() { | 732 function DateGetUTCSeconds() { |
714 return GetUTCSecondsFrom(this); | 733 return GetUTCSecondsFrom(this); |
715 }; | 734 } |
716 | 735 |
717 | 736 |
718 // ECMA 262 - 15.9.5.24 | 737 // ECMA 262 - 15.9.5.24 |
719 function DateGetMilliseconds() { | 738 function DateGetMilliseconds() { |
720 return GetMillisecondsFrom(this); | 739 return GetMillisecondsFrom(this); |
721 }; | 740 } |
722 | 741 |
723 | 742 |
724 // ECMA 262 - 15.9.5.25 | 743 // ECMA 262 - 15.9.5.25 |
725 function DateGetUTCMilliseconds() { | 744 function DateGetUTCMilliseconds() { |
726 return GetUTCMillisecondsFrom(this); | 745 return GetUTCMillisecondsFrom(this); |
727 }; | 746 } |
728 | 747 |
729 | 748 |
730 // ECMA 262 - 15.9.5.26 | 749 // ECMA 262 - 15.9.5.26 |
731 function DateGetTimezoneOffset() { | 750 function DateGetTimezoneOffset() { |
732 var t = GetTimeFrom(this); | 751 var t = GetTimeFrom(this); |
733 if ($isNaN(t)) return t; | 752 if ($isNaN(t)) return t; |
734 return (t - LocalTime(t)) / msPerMinute; | 753 return (t - LocalTime(t)) / msPerMinute; |
735 }; | 754 } |
736 | 755 |
737 | 756 |
738 // ECMA 262 - 15.9.5.27 | 757 // ECMA 262 - 15.9.5.27 |
739 function DateSetTime(ms) { | 758 function DateSetTime(ms) { |
740 if (!IS_DATE(this)) throw new $TypeError('this is not a Date object.'); | 759 if (!IS_DATE(this)) throw new $TypeError('this is not a Date object.'); |
741 return %_SetValueOf(this, TimeClip(ToNumber(ms))); | 760 return %_SetValueOf(this, TimeClip(ToNumber(ms))); |
742 }; | 761 } |
743 | 762 |
744 | 763 |
745 // ECMA 262 - 15.9.5.28 | 764 // ECMA 262 - 15.9.5.28 |
746 function DateSetMilliseconds(ms) { | 765 function DateSetMilliseconds(ms) { |
747 var t = LocalTime(GetTimeFrom(this)); | 766 var t = LocalTime(GetTimeFrom(this)); |
748 ms = ToNumber(ms); | 767 ms = ToNumber(ms); |
749 var time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), ms); | 768 var time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), ms); |
750 return %_SetValueOf(this, TimeClip(UTC(MakeDate(Day(t), time)))); | 769 return %_SetValueOf(this, TimeClip(UTC(MakeDate(Day(t), time)))); |
751 }; | 770 } |
752 | 771 |
753 | 772 |
754 // ECMA 262 - 15.9.5.29 | 773 // ECMA 262 - 15.9.5.29 |
755 function DateSetUTCMilliseconds(ms) { | 774 function DateSetUTCMilliseconds(ms) { |
756 var t = GetTimeFrom(this); | 775 var t = GetTimeFrom(this); |
757 ms = ToNumber(ms); | 776 ms = ToNumber(ms); |
758 var time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), ms); | 777 var time = MakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), ms); |
759 return %_SetValueOf(this, TimeClip(MakeDate(Day(t), time))); | 778 return %_SetValueOf(this, TimeClip(MakeDate(Day(t), time))); |
760 }; | 779 } |
761 | 780 |
762 | 781 |
763 // ECMA 262 - 15.9.5.30 | 782 // ECMA 262 - 15.9.5.30 |
764 function DateSetSeconds(sec, ms) { | 783 function DateSetSeconds(sec, ms) { |
765 var t = LocalTime(GetTimeFrom(this)); | 784 var t = LocalTime(GetTimeFrom(this)); |
766 sec = ToNumber(sec); | 785 sec = ToNumber(sec); |
767 ms = %_ArgumentsLength() < 2 ? GetMillisecondsFrom(this) : ToNumber(ms); | 786 ms = %_ArgumentsLength() < 2 ? GetMillisecondsFrom(this) : ToNumber(ms); |
768 var time = MakeTime(HourFromTime(t), MinFromTime(t), sec, ms); | 787 var time = MakeTime(HourFromTime(t), MinFromTime(t), sec, ms); |
769 return %_SetValueOf(this, TimeClip(UTC(MakeDate(Day(t), time)))); | 788 return %_SetValueOf(this, TimeClip(UTC(MakeDate(Day(t), time)))); |
770 }; | 789 } |
771 | 790 |
772 | 791 |
773 // ECMA 262 - 15.9.5.31 | 792 // ECMA 262 - 15.9.5.31 |
774 function DateSetUTCSeconds(sec, ms) { | 793 function DateSetUTCSeconds(sec, ms) { |
775 var t = GetTimeFrom(this); | 794 var t = GetTimeFrom(this); |
776 sec = ToNumber(sec); | 795 sec = ToNumber(sec); |
777 ms = %_ArgumentsLength() < 2 ? GetUTCMillisecondsFrom(this) : ToNumber(ms); | 796 ms = %_ArgumentsLength() < 2 ? GetUTCMillisecondsFrom(this) : ToNumber(ms); |
778 var time = MakeTime(HourFromTime(t), MinFromTime(t), sec, ms); | 797 var time = MakeTime(HourFromTime(t), MinFromTime(t), sec, ms); |
779 return %_SetValueOf(this, TimeClip(MakeDate(Day(t), time))); | 798 return %_SetValueOf(this, TimeClip(MakeDate(Day(t), time))); |
780 }; | 799 } |
781 | 800 |
782 | 801 |
783 // ECMA 262 - 15.9.5.33 | 802 // ECMA 262 - 15.9.5.33 |
784 function DateSetMinutes(min, sec, ms) { | 803 function DateSetMinutes(min, sec, ms) { |
785 var t = LocalTime(GetTimeFrom(this)); | 804 var t = LocalTime(GetTimeFrom(this)); |
786 min = ToNumber(min); | 805 min = ToNumber(min); |
787 var argc = %_ArgumentsLength(); | 806 var argc = %_ArgumentsLength(); |
788 sec = argc < 2 ? GetSecondsFrom(this) : ToNumber(sec); | 807 sec = argc < 2 ? GetSecondsFrom(this) : ToNumber(sec); |
789 ms = argc < 3 ? GetMillisecondsFrom(this) : ToNumber(ms); | 808 ms = argc < 3 ? GetMillisecondsFrom(this) : ToNumber(ms); |
790 var time = MakeTime(HourFromTime(t), min, sec, ms); | 809 var time = MakeTime(HourFromTime(t), min, sec, ms); |
791 return %_SetValueOf(this, TimeClip(UTC(MakeDate(Day(t), time)))); | 810 return %_SetValueOf(this, TimeClip(UTC(MakeDate(Day(t), time)))); |
792 }; | 811 } |
793 | 812 |
794 | 813 |
795 // ECMA 262 - 15.9.5.34 | 814 // ECMA 262 - 15.9.5.34 |
796 function DateSetUTCMinutes(min, sec, ms) { | 815 function DateSetUTCMinutes(min, sec, ms) { |
797 var t = GetTimeFrom(this); | 816 var t = GetTimeFrom(this); |
798 min = ToNumber(min); | 817 min = ToNumber(min); |
799 var argc = %_ArgumentsLength(); | 818 var argc = %_ArgumentsLength(); |
800 sec = argc < 2 ? GetUTCSecondsFrom(this) : ToNumber(sec); | 819 sec = argc < 2 ? GetUTCSecondsFrom(this) : ToNumber(sec); |
801 ms = argc < 3 ? GetUTCMillisecondsFrom(this) : ToNumber(ms); | 820 ms = argc < 3 ? GetUTCMillisecondsFrom(this) : ToNumber(ms); |
802 var time = MakeTime(HourFromTime(t), min, sec, ms); | 821 var time = MakeTime(HourFromTime(t), min, sec, ms); |
803 return %_SetValueOf(this, TimeClip(MakeDate(Day(t), time))); | 822 return %_SetValueOf(this, TimeClip(MakeDate(Day(t), time))); |
804 }; | 823 } |
805 | 824 |
806 | 825 |
807 // ECMA 262 - 15.9.5.35 | 826 // ECMA 262 - 15.9.5.35 |
808 function DateSetHours(hour, min, sec, ms) { | 827 function DateSetHours(hour, min, sec, ms) { |
809 var t = LocalTime(GetTimeFrom(this)); | 828 var t = LocalTime(GetTimeFrom(this)); |
810 hour = ToNumber(hour); | 829 hour = ToNumber(hour); |
811 var argc = %_ArgumentsLength(); | 830 var argc = %_ArgumentsLength(); |
812 min = argc < 2 ? GetMinutesFrom(this) : ToNumber(min); | 831 min = argc < 2 ? GetMinutesFrom(this) : ToNumber(min); |
813 sec = argc < 3 ? GetSecondsFrom(this) : ToNumber(sec); | 832 sec = argc < 3 ? GetSecondsFrom(this) : ToNumber(sec); |
814 ms = argc < 4 ? GetMillisecondsFrom(this) : ToNumber(ms); | 833 ms = argc < 4 ? GetMillisecondsFrom(this) : ToNumber(ms); |
815 var time = MakeTime(hour, min, sec, ms); | 834 var time = MakeTime(hour, min, sec, ms); |
816 return %_SetValueOf(this, TimeClip(UTC(MakeDate(Day(t), time)))); | 835 return %_SetValueOf(this, TimeClip(UTC(MakeDate(Day(t), time)))); |
817 }; | 836 } |
818 | 837 |
819 | 838 |
820 // ECMA 262 - 15.9.5.34 | 839 // ECMA 262 - 15.9.5.34 |
821 function DateSetUTCHours(hour, min, sec, ms) { | 840 function DateSetUTCHours(hour, min, sec, ms) { |
822 var t = GetTimeFrom(this); | 841 var t = GetTimeFrom(this); |
823 hour = ToNumber(hour); | 842 hour = ToNumber(hour); |
824 var argc = %_ArgumentsLength(); | 843 var argc = %_ArgumentsLength(); |
825 min = argc < 2 ? GetUTCMinutesFrom(this) : ToNumber(min); | 844 min = argc < 2 ? GetUTCMinutesFrom(this) : ToNumber(min); |
826 sec = argc < 3 ? GetUTCSecondsFrom(this) : ToNumber(sec); | 845 sec = argc < 3 ? GetUTCSecondsFrom(this) : ToNumber(sec); |
827 ms = argc < 4 ? GetUTCMillisecondsFrom(this) : ToNumber(ms); | 846 ms = argc < 4 ? GetUTCMillisecondsFrom(this) : ToNumber(ms); |
828 var time = MakeTime(hour, min, sec, ms); | 847 var time = MakeTime(hour, min, sec, ms); |
829 return %_SetValueOf(this, TimeClip(MakeDate(Day(t), time))); | 848 return %_SetValueOf(this, TimeClip(MakeDate(Day(t), time))); |
830 }; | 849 } |
831 | 850 |
832 | 851 |
833 // ECMA 262 - 15.9.5.36 | 852 // ECMA 262 - 15.9.5.36 |
834 function DateSetDate(date) { | 853 function DateSetDate(date) { |
835 var t = LocalTime(GetTimeFrom(this)); | 854 var t = LocalTime(GetTimeFrom(this)); |
836 date = ToNumber(date); | 855 date = ToNumber(date); |
837 var day = MakeDay(YearFromTime(t), MonthFromTime(t), date); | 856 var day = MakeDay(YearFromTime(t), MonthFromTime(t), date); |
838 return %_SetValueOf(this, TimeClip(UTC(MakeDate(day, TimeWithinDay(t))))); | 857 return %_SetValueOf(this, TimeClip(UTC(MakeDate(day, TimeWithinDay(t))))); |
839 }; | 858 } |
840 | 859 |
841 | 860 |
842 // ECMA 262 - 15.9.5.37 | 861 // ECMA 262 - 15.9.5.37 |
843 function DateSetUTCDate(date) { | 862 function DateSetUTCDate(date) { |
844 var t = GetTimeFrom(this); | 863 var t = GetTimeFrom(this); |
845 date = ToNumber(date); | 864 date = ToNumber(date); |
846 var day = MakeDay(YearFromTime(t), MonthFromTime(t), date); | 865 var day = MakeDay(YearFromTime(t), MonthFromTime(t), date); |
847 return %_SetValueOf(this, TimeClip(MakeDate(day, TimeWithinDay(t)))); | 866 return %_SetValueOf(this, TimeClip(MakeDate(day, TimeWithinDay(t)))); |
848 }; | 867 } |
849 | 868 |
850 | 869 |
851 // ECMA 262 - 15.9.5.38 | 870 // ECMA 262 - 15.9.5.38 |
852 function DateSetMonth(month, date) { | 871 function DateSetMonth(month, date) { |
853 var t = LocalTime(GetTimeFrom(this)); | 872 var t = LocalTime(GetTimeFrom(this)); |
854 month = ToNumber(month); | 873 month = ToNumber(month); |
855 date = %_ArgumentsLength() < 2 ? GetDateFrom(this) : ToNumber(date); | 874 date = %_ArgumentsLength() < 2 ? GetDateFrom(this) : ToNumber(date); |
856 var day = MakeDay(YearFromTime(t), month, date); | 875 var day = MakeDay(YearFromTime(t), month, date); |
857 return %_SetValueOf(this, TimeClip(UTC(MakeDate(day, TimeWithinDay(t))))); | 876 return %_SetValueOf(this, TimeClip(UTC(MakeDate(day, TimeWithinDay(t))))); |
858 }; | 877 } |
859 | 878 |
860 | 879 |
861 // ECMA 262 - 15.9.5.39 | 880 // ECMA 262 - 15.9.5.39 |
862 function DateSetUTCMonth(month, date) { | 881 function DateSetUTCMonth(month, date) { |
863 var t = GetTimeFrom(this); | 882 var t = GetTimeFrom(this); |
864 month = ToNumber(month); | 883 month = ToNumber(month); |
865 date = %_ArgumentsLength() < 2 ? GetUTCDateFrom(this) : ToNumber(date); | 884 date = %_ArgumentsLength() < 2 ? GetUTCDateFrom(this) : ToNumber(date); |
866 var day = MakeDay(YearFromTime(t), month, date); | 885 var day = MakeDay(YearFromTime(t), month, date); |
867 return %_SetValueOf(this, TimeClip(MakeDate(day, TimeWithinDay(t)))); | 886 return %_SetValueOf(this, TimeClip(MakeDate(day, TimeWithinDay(t)))); |
868 }; | 887 } |
869 | 888 |
870 | 889 |
871 // ECMA 262 - 15.9.5.40 | 890 // ECMA 262 - 15.9.5.40 |
872 function DateSetFullYear(year, month, date) { | 891 function DateSetFullYear(year, month, date) { |
873 var t = GetTimeFrom(this); | 892 var t = GetTimeFrom(this); |
874 t = $isNaN(t) ? 0 : LocalTime(t); | 893 t = $isNaN(t) ? 0 : LocalTime(t); |
875 year = ToNumber(year); | 894 year = ToNumber(year); |
876 var argc = %_ArgumentsLength(); | 895 var argc = %_ArgumentsLength(); |
877 month = argc < 2 ? MonthFromTime(t) : ToNumber(month); | 896 month = argc < 2 ? MonthFromTime(t) : ToNumber(month); |
878 date = argc < 3 ? DateFromTime(t) : ToNumber(date); | 897 date = argc < 3 ? DateFromTime(t) : ToNumber(date); |
879 var day = MakeDay(year, month, date); | 898 var day = MakeDay(year, month, date); |
880 return %_SetValueOf(this, TimeClip(UTC(MakeDate(day, TimeWithinDay(t))))); | 899 return %_SetValueOf(this, TimeClip(UTC(MakeDate(day, TimeWithinDay(t))))); |
881 }; | 900 } |
882 | 901 |
883 | 902 |
884 // ECMA 262 - 15.9.5.41 | 903 // ECMA 262 - 15.9.5.41 |
885 function DateSetUTCFullYear(year, month, date) { | 904 function DateSetUTCFullYear(year, month, date) { |
886 var t = GetTimeFrom(this); | 905 var t = GetTimeFrom(this); |
887 if ($isNaN(t)) t = 0; | 906 if ($isNaN(t)) t = 0; |
888 var argc = %_ArgumentsLength(); | 907 var argc = %_ArgumentsLength(); |
889 year = ToNumber(year); | 908 year = ToNumber(year); |
890 month = argc < 2 ? MonthFromTime(t) : ToNumber(month); | 909 month = argc < 2 ? MonthFromTime(t) : ToNumber(month); |
891 date = argc < 3 ? DateFromTime(t) : ToNumber(date); | 910 date = argc < 3 ? DateFromTime(t) : ToNumber(date); |
892 var day = MakeDay(year, month, date); | 911 var day = MakeDay(year, month, date); |
893 return %_SetValueOf(this, TimeClip(MakeDate(day, TimeWithinDay(t)))); | 912 return %_SetValueOf(this, TimeClip(MakeDate(day, TimeWithinDay(t)))); |
894 }; | 913 } |
895 | 914 |
896 | 915 |
897 // ECMA 262 - 15.9.5.42 | 916 // ECMA 262 - 15.9.5.42 |
898 function DateToUTCString() { | 917 function DateToUTCString() { |
899 var t = GetTimeFrom(this); | 918 var t = GetTimeFrom(this); |
900 if ($isNaN(t)) return kInvalidDate; | 919 if ($isNaN(t)) return kInvalidDate; |
901 // Return UTC string of the form: Sat, 31 Jan 1970 23:00:00 GMT | 920 // Return UTC string of the form: Sat, 31 Jan 1970 23:00:00 GMT |
902 return WeekDays[WeekDay(t)] + ', ' | 921 return WeekDays[WeekDay(t)] + ', ' |
903 + TwoDigitString(DateFromTime(t)) + ' ' | 922 + TwoDigitString(DateFromTime(t)) + ' ' |
904 + Months[MonthFromTime(t)] + ' ' | 923 + Months[MonthFromTime(t)] + ' ' |
905 + YearFromTime(t) + ' ' | 924 + YearFromTime(t) + ' ' |
906 + TimeString(t) + ' GMT'; | 925 + TimeString(t) + ' GMT'; |
907 }; | 926 } |
908 | 927 |
909 | 928 |
910 // ECMA 262 - B.2.4 | 929 // ECMA 262 - B.2.4 |
911 function DateGetYear() { | 930 function DateGetYear() { |
912 var t = GetTimeFrom(this); | 931 var t = GetTimeFrom(this); |
913 if ($isNaN(t)) return $NaN; | 932 if ($isNaN(t)) return $NaN; |
914 return YearFromTime(LocalTime(t)) - 1900; | 933 return YearFromTime(LocalTime(t)) - 1900; |
915 }; | 934 } |
916 | 935 |
917 | 936 |
918 // ECMA 262 - B.2.5 | 937 // ECMA 262 - B.2.5 |
919 function DateSetYear(year) { | 938 function DateSetYear(year) { |
920 var t = LocalTime(GetTimeFrom(this)); | 939 var t = LocalTime(GetTimeFrom(this)); |
921 if ($isNaN(t)) t = 0; | 940 if ($isNaN(t)) t = 0; |
922 year = ToNumber(year); | 941 year = ToNumber(year); |
923 if ($isNaN(year)) return %_SetValueOf(this, $NaN); | 942 if ($isNaN(year)) return %_SetValueOf(this, $NaN); |
924 year = (0 <= TO_INTEGER(year) && TO_INTEGER(year) <= 99) | 943 year = (0 <= TO_INTEGER(year) && TO_INTEGER(year) <= 99) |
925 ? 1900 + TO_INTEGER(year) : year; | 944 ? 1900 + TO_INTEGER(year) : year; |
926 var day = MakeDay(year, GetMonthFrom(this), GetDateFrom(this)); | 945 var day = MakeDay(year, GetMonthFrom(this), GetDateFrom(this)); |
927 return %_SetValueOf(this, TimeClip(UTC(MakeDate(day, TimeWithinDay(t))))); | 946 return %_SetValueOf(this, TimeClip(UTC(MakeDate(day, TimeWithinDay(t))))); |
928 }; | 947 } |
| 948 |
| 949 |
| 950 // ECMA 262 - B.2.6 |
| 951 // |
| 952 // Notice that this does not follow ECMA 262 completely. ECMA 262 |
| 953 // says that toGMTString should be the same Function object as |
| 954 // toUTCString. JSC does not do this, so for compatibility we do not |
| 955 // do that either. Instead, we create a new function whose name |
| 956 // property will return toGMTString. |
| 957 function DateToGMTString() { |
| 958 return DateToUTCString.call(this); |
| 959 } |
929 | 960 |
930 | 961 |
931 // ------------------------------------------------------------------- | 962 // ------------------------------------------------------------------- |
932 | 963 |
933 function SetupDate() { | 964 function SetupDate() { |
934 // Setup non-enumerable properties of the Date object itself. | 965 // Setup non-enumerable properties of the Date object itself. |
935 InstallProperties($Date, DONT_ENUM, { | 966 InstallFunctions($Date, DONT_ENUM, $Array( |
936 UTC: DateUTC, | 967 "UTC", DateUTC, |
937 parse: DateParse, | 968 "parse", DateParse, |
938 now: DateNow | 969 "now", DateNow |
939 }); | 970 )); |
940 | 971 |
941 // Setup non-enumerable properties of the Date prototype object. | 972 // Setup non-enumerable constructor property of the Date prototype object. |
942 InstallProperties($Date.prototype, DONT_ENUM, { | 973 %AddProperty($Date.prototype, "constructor", $Date, DONT_ENUM); |
943 constructor: $Date, | 974 |
944 toString: DateToString, | 975 // Setup non-enumerable functions of the Date prototype object and |
945 toDateString: DateToDateString, | 976 // set their names. |
946 toTimeString: DateToTimeString, | 977 InstallFunctions($Date.prototype, DONT_ENUM, $Array( |
947 toLocaleString: DateToString, | 978 "toString", DateToString, |
948 toLocaleDateString: DateToDateString, | 979 "toDateString", DateToDateString, |
949 toLocaleTimeString: DateToLocaleTimeString, | 980 "toTimeString", DateToTimeString, |
950 valueOf: DateGetTime, | 981 "toLocaleString", DateToLocaleString, |
951 getTime: DateGetTime, | 982 "toLocaleDateString", DateToLocaleDateString, |
952 getFullYear: DateGetFullYear, | 983 "toLocaleTimeString", DateToLocaleTimeString, |
953 getUTCFullYear: DateGetUTCFullYear, | 984 "valueOf", DateValueOf, |
954 getMonth: DateGetMonth, | 985 "getTime", DateGetTime, |
955 getUTCMonth: DateGetUTCMonth, | 986 "getFullYear", DateGetFullYear, |
956 getDate: DateGetDate, | 987 "getUTCFullYear", DateGetUTCFullYear, |
957 getUTCDate: DateGetUTCDate, | 988 "getMonth", DateGetMonth, |
958 getDay: DateGetDay, | 989 "getUTCMonth", DateGetUTCMonth, |
959 getUTCDay: DateGetUTCDay, | 990 "getDate", DateGetDate, |
960 getHours: DateGetHours, | 991 "getUTCDate", DateGetUTCDate, |
961 getUTCHours: DateGetUTCHours, | 992 "getDay", DateGetDay, |
962 getMinutes: DateGetMinutes, | 993 "getUTCDay", DateGetUTCDay, |
963 getUTCMinutes: DateGetUTCMinutes, | 994 "getHours", DateGetHours, |
964 getSeconds: DateGetSeconds, | 995 "getUTCHours", DateGetUTCHours, |
965 getUTCSeconds: DateGetUTCSeconds, | 996 "getMinutes", DateGetMinutes, |
966 getMilliseconds: DateGetMilliseconds, | 997 "getUTCMinutes", DateGetUTCMinutes, |
967 getUTCMilliseconds: DateGetUTCMilliseconds, | 998 "getSeconds", DateGetSeconds, |
968 getTimezoneOffset: DateGetTimezoneOffset, | 999 "getUTCSeconds", DateGetUTCSeconds, |
969 setTime: DateSetTime, | 1000 "getMilliseconds", DateGetMilliseconds, |
970 setMilliseconds: DateSetMilliseconds, | 1001 "getUTCMilliseconds", DateGetUTCMilliseconds, |
971 setUTCMilliseconds: DateSetUTCMilliseconds, | 1002 "getTimezoneOffset", DateGetTimezoneOffset, |
972 setSeconds: DateSetSeconds, | 1003 "setTime", DateSetTime, |
973 setUTCSeconds: DateSetUTCSeconds, | 1004 "setMilliseconds", DateSetMilliseconds, |
974 setMinutes: DateSetMinutes, | 1005 "setUTCMilliseconds", DateSetUTCMilliseconds, |
975 setUTCMinutes: DateSetUTCMinutes, | 1006 "setSeconds", DateSetSeconds, |
976 setHours: DateSetHours, | 1007 "setUTCSeconds", DateSetUTCSeconds, |
977 setUTCHours: DateSetUTCHours, | 1008 "setMinutes", DateSetMinutes, |
978 setDate: DateSetDate, | 1009 "setUTCMinutes", DateSetUTCMinutes, |
979 setUTCDate: DateSetUTCDate, | 1010 "setHours", DateSetHours, |
980 setMonth: DateSetMonth, | 1011 "setUTCHours", DateSetUTCHours, |
981 setUTCMonth: DateSetUTCMonth, | 1012 "setDate", DateSetDate, |
982 setFullYear: DateSetFullYear, | 1013 "setUTCDate", DateSetUTCDate, |
983 setUTCFullYear: DateSetUTCFullYear, | 1014 "setMonth", DateSetMonth, |
984 toUTCString: DateToUTCString, | 1015 "setUTCMonth", DateSetUTCMonth, |
985 toGMTString: DateToUTCString, | 1016 "setFullYear", DateSetFullYear, |
986 getYear: DateGetYear, | 1017 "setUTCFullYear", DateSetUTCFullYear, |
987 setYear: DateSetYear | 1018 "toGMTString", DateToGMTString, |
988 }); | 1019 "toUTCString", DateToUTCString, |
989 }; | 1020 "getYear", DateGetYear, |
| 1021 "setYear", DateSetYear |
| 1022 )); |
| 1023 } |
990 | 1024 |
991 SetupDate(); | 1025 SetupDate(); |
OLD | NEW |