]> git.saurik.com Git - apple/javascriptcore.git/blob - tests/exceptionFuzz/date-format-xparb.js
JavaScriptCore-7600.1.4.9.tar.gz
[apple/javascriptcore.git] / tests / exceptionFuzz / date-format-xparb.js
1 try {
2
3 /*
4 * Copyright (C) 2004 Baron Schwartz <baron at sequent dot org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation, version 2.1.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 */
15
16 Date.parseFunctions = {count:0};
17 Date.parseRegexes = [];
18 Date.formatFunctions = {count:0};
19
20 Date.prototype.dateFormat = function(format) {
21 if (Date.formatFunctions[format] == null) {
22 Date.createNewFormat(format);
23 }
24 var func = Date.formatFunctions[format];
25 return this[func]();
26 }
27
28 Date.createNewFormat = function(format) {
29 var funcName = "format" + Date.formatFunctions.count++;
30 Date.formatFunctions[format] = funcName;
31 var code = "Date.prototype." + funcName + " = function(){return ";
32 var special = false;
33 var ch = '';
34 for (var i = 0; i < format.length; ++i) {
35 ch = format.charAt(i);
36 if (!special && ch == "\\") {
37 special = true;
38 }
39 else if (special) {
40 special = false;
41 code += "'" + String.escape(ch) + "' + ";
42 }
43 else {
44 code += Date.getFormatCode(ch);
45 }
46 }
47 eval(code.substring(0, code.length - 3) + ";}");
48 }
49
50 Date.getFormatCode = function(character) {
51 switch (character) {
52 case "d":
53 return "String.leftPad(this.getDate(), 2, '0') + ";
54 case "D":
55 return "Date.dayNames[this.getDay()].substring(0, 3) + ";
56 case "j":
57 return "this.getDate() + ";
58 case "l":
59 return "Date.dayNames[this.getDay()] + ";
60 case "S":
61 return "this.getSuffix() + ";
62 case "w":
63 return "this.getDay() + ";
64 case "z":
65 return "this.getDayOfYear() + ";
66 case "W":
67 return "this.getWeekOfYear() + ";
68 case "F":
69 return "Date.monthNames[this.getMonth()] + ";
70 case "m":
71 return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
72 case "M":
73 return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
74 case "n":
75 return "(this.getMonth() + 1) + ";
76 case "t":
77 return "this.getDaysInMonth() + ";
78 case "L":
79 return "(this.isLeapYear() ? 1 : 0) + ";
80 case "Y":
81 return "this.getFullYear() + ";
82 case "y":
83 return "('' + this.getFullYear()).substring(2, 4) + ";
84 case "a":
85 return "(this.getHours() < 12 ? 'am' : 'pm') + ";
86 case "A":
87 return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
88 case "g":
89 return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
90 case "G":
91 return "this.getHours() + ";
92 case "h":
93 return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
94 case "H":
95 return "String.leftPad(this.getHours(), 2, '0') + ";
96 case "i":
97 return "String.leftPad(this.getMinutes(), 2, '0') + ";
98 case "s":
99 return "String.leftPad(this.getSeconds(), 2, '0') + ";
100 case "O":
101 return "this.getGMTOffset() + ";
102 case "T":
103 return "this.getTimezone() + ";
104 case "Z":
105 return "(this.getTimezoneOffset() * -60) + ";
106 default:
107 return "'" + String.escape(character) + "' + ";
108 }
109 }
110
111 Date.parseDate = function(input, format) {
112 if (Date.parseFunctions[format] == null) {
113 Date.createParser(format);
114 }
115 var func = Date.parseFunctions[format];
116 return Date[func](input);
117 }
118
119 Date.createParser = function(format) {
120 var funcName = "parse" + Date.parseFunctions.count++;
121 var regexNum = Date.parseRegexes.length;
122 var currentGroup = 1;
123 Date.parseFunctions[format] = funcName;
124
125 var code = "Date." + funcName + " = function(input){\n"
126 + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n"
127 + "var d = new Date();\n"
128 + "y = d.getFullYear();\n"
129 + "m = d.getMonth();\n"
130 + "d = d.getDate();\n"
131 + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
132 + "if (results && results.length > 0) {"
133 var regex = "";
134
135 var special = false;
136 var ch = '';
137 for (var i = 0; i < format.length; ++i) {
138 ch = format.charAt(i);
139 if (!special && ch == "\\") {
140 special = true;
141 }
142 else if (special) {
143 special = false;
144 regex += String.escape(ch);
145 }
146 else {
147 obj = Date.formatCodeToRegex(ch, currentGroup);
148 currentGroup += obj.g;
149 regex += obj.s;
150 if (obj.g && obj.c) {
151 code += obj.c;
152 }
153 }
154 }
155
156 code += "if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
157 + "{return new Date(y, m, d, h, i, s);}\n"
158 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
159 + "{return new Date(y, m, d, h, i);}\n"
160 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0)\n"
161 + "{return new Date(y, m, d, h);}\n"
162 + "else if (y > 0 && m >= 0 && d > 0)\n"
163 + "{return new Date(y, m, d);}\n"
164 + "else if (y > 0 && m >= 0)\n"
165 + "{return new Date(y, m);}\n"
166 + "else if (y > 0)\n"
167 + "{return new Date(y);}\n"
168 + "}return null;}";
169
170 Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$");
171 eval(code);
172 }
173
174 Date.formatCodeToRegex = function(character, currentGroup) {
175 switch (character) {
176 case "D":
177 return {g:0,
178 c:null,
179 s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
180 case "j":
181 case "d":
182 return {g:1,
183 c:"d = parseInt(results[" + currentGroup + "], 10);\n",
184 s:"(\\d{1,2})"};
185 case "l":
186 return {g:0,
187 c:null,
188 s:"(?:" + Date.dayNames.join("|") + ")"};
189 case "S":
190 return {g:0,
191 c:null,
192 s:"(?:st|nd|rd|th)"};
193 case "w":
194 return {g:0,
195 c:null,
196 s:"\\d"};
197 case "z":
198 return {g:0,
199 c:null,
200 s:"(?:\\d{1,3})"};
201 case "W":
202 return {g:0,
203 c:null,
204 s:"(?:\\d{2})"};
205 case "F":
206 return {g:1,
207 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].substring(0, 3)], 10);\n",
208 s:"(" + Date.monthNames.join("|") + ")"};
209 case "M":
210 return {g:1,
211 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10);\n",
212 s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
213 case "n":
214 case "m":
215 return {g:1,
216 c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n",
217 s:"(\\d{1,2})"};
218 case "t":
219 return {g:0,
220 c:null,
221 s:"\\d{1,2}"};
222 case "L":
223 return {g:0,
224 c:null,
225 s:"(?:1|0)"};
226 case "Y":
227 return {g:1,
228 c:"y = parseInt(results[" + currentGroup + "], 10);\n",
229 s:"(\\d{4})"};
230 case "y":
231 return {g:1,
232 c:"var ty = parseInt(results[" + currentGroup + "], 10);\n"
233 + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
234 s:"(\\d{1,2})"};
235 case "a":
236 return {g:1,
237 c:"if (results[" + currentGroup + "] == 'am') {\n"
238 + "if (h == 12) { h = 0; }\n"
239 + "} else { if (h < 12) { h += 12; }}",
240 s:"(am|pm)"};
241 case "A":
242 return {g:1,
243 c:"if (results[" + currentGroup + "] == 'AM') {\n"
244 + "if (h == 12) { h = 0; }\n"
245 + "} else { if (h < 12) { h += 12; }}",
246 s:"(AM|PM)"};
247 case "g":
248 case "G":
249 case "h":
250 case "H":
251 return {g:1,
252 c:"h = parseInt(results[" + currentGroup + "], 10);\n",
253 s:"(\\d{1,2})"};
254 case "i":
255 return {g:1,
256 c:"i = parseInt(results[" + currentGroup + "], 10);\n",
257 s:"(\\d{2})"};
258 case "s":
259 return {g:1,
260 c:"s = parseInt(results[" + currentGroup + "], 10);\n",
261 s:"(\\d{2})"};
262 case "O":
263 return {g:0,
264 c:null,
265 s:"[+-]\\d{4}"};
266 case "T":
267 return {g:0,
268 c:null,
269 s:"[A-Z]{3}"};
270 case "Z":
271 return {g:0,
272 c:null,
273 s:"[+-]\\d{1,5}"};
274 default:
275 return {g:0,
276 c:null,
277 s:String.escape(character)};
278 }
279 }
280
281 Date.prototype.getTimezone = function() {
282 return this.toString().replace(
283 /^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace(
284 /^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3");
285 }
286
287 Date.prototype.getGMTOffset = function() {
288 return (this.getTimezoneOffset() > 0 ? "-" : "+")
289 + String.leftPad(Math.floor(this.getTimezoneOffset() / 60), 2, "0")
290 + String.leftPad(this.getTimezoneOffset() % 60, 2, "0");
291 }
292
293 Date.prototype.getDayOfYear = function() {
294 var num = 0;
295 Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
296 for (var i = 0; i < this.getMonth(); ++i) {
297 num += Date.daysInMonth[i];
298 }
299 return num + this.getDate() - 1;
300 }
301
302 Date.prototype.getWeekOfYear = function() {
303 // Skip to Thursday of this week
304 var now = this.getDayOfYear() + (4 - this.getDay());
305 // Find the first Thursday of the year
306 var jan1 = new Date(this.getFullYear(), 0, 1);
307 var then = (7 - jan1.getDay() + 4);
308 document.write(then);
309 return String.leftPad(((now - then) / 7) + 1, 2, "0");
310 }
311
312 Date.prototype.isLeapYear = function() {
313 var year = this.getFullYear();
314 return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
315 }
316
317 Date.prototype.getFirstDayOfMonth = function() {
318 var day = (this.getDay() - (this.getDate() - 1)) % 7;
319 return (day < 0) ? (day + 7) : day;
320 }
321
322 Date.prototype.getLastDayOfMonth = function() {
323 var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate())) % 7;
324 return (day < 0) ? (day + 7) : day;
325 }
326
327 Date.prototype.getDaysInMonth = function() {
328 Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
329 return Date.daysInMonth[this.getMonth()];
330 }
331
332 Date.prototype.getSuffix = function() {
333 switch (this.getDate()) {
334 case 1:
335 case 21:
336 case 31:
337 return "st";
338 case 2:
339 case 22:
340 return "nd";
341 case 3:
342 case 23:
343 return "rd";
344 default:
345 return "th";
346 }
347 }
348
349 String.escape = function(string) {
350 return string.replace(/('|\\)/g, "\\$1");
351 }
352
353 String.leftPad = function (val, size, ch) {
354 var result = new String(val);
355 if (ch == null) {
356 ch = " ";
357 }
358 while (result.length < size) {
359 result = ch + result;
360 }
361 return result;
362 }
363
364 Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
365 Date.monthNames =
366 ["January",
367 "February",
368 "March",
369 "April",
370 "May",
371 "June",
372 "July",
373 "August",
374 "September",
375 "October",
376 "November",
377 "December"];
378 Date.dayNames =
379 ["Sunday",
380 "Monday",
381 "Tuesday",
382 "Wednesday",
383 "Thursday",
384 "Friday",
385 "Saturday"];
386 Date.y2kYear = 50;
387 Date.monthNumbers = {
388 Jan:0,
389 Feb:1,
390 Mar:2,
391 Apr:3,
392 May:4,
393 Jun:5,
394 Jul:6,
395 Aug:7,
396 Sep:8,
397 Oct:9,
398 Nov:10,
399 Dec:11};
400 Date.patterns = {
401 ISO8601LongPattern:"Y-m-d H:i:s",
402 ISO8601ShortPattern:"Y-m-d",
403 ShortDatePattern: "n/j/Y",
404 LongDatePattern: "l, F d, Y",
405 FullDateTimePattern: "l, F d, Y g:i:s A",
406 MonthDayPattern: "F d",
407 ShortTimePattern: "g:i A",
408 LongTimePattern: "g:i:s A",
409 SortableDateTimePattern: "Y-m-d\\TH:i:s",
410 UniversalSortableDateTimePattern: "Y-m-d H:i:sO",
411 YearMonthPattern: "F, Y"};
412
413 var date = new Date("1/1/2007 1:11:11");
414
415 for (i = 0; i < 4000; ++i) {
416 var shortFormat = date.dateFormat("Y-m-d");
417 var longFormat = date.dateFormat("l, F d, Y g:i:s A");
418 date.setTime(date.getTime() + 84266956);
419 }
420
421 // FIXME: Find a way to validate this test.
422 // https://bugs.webkit.org/show_bug.cgi?id=114849
423
424 } catch (e) {
425 print("JSC EXCEPTION FUZZ: Caught exception: " + e);
426 }