]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/exceptionFuzz/date-format-xparb.js
4 * Copyright (C) 2004 Baron Schwartz <baron at sequent dot org>
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.
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
16 enableExceptionFuzz();
19 Date
.parseFunctions
= {count:0};
20 Date
.parseRegexes
= [];
21 Date
.formatFunctions
= {count:0};
23 Date
.prototype.dateFormat = function(format
) {
24 if (Date
.formatFunctions
[format
] == null) {
25 Date
.createNewFormat(format
);
27 var func
= Date
.formatFunctions
[format
];
31 Date
.createNewFormat = function(format
) {
32 var funcName
= "format" + Date
.formatFunctions
.count
++;
33 Date
.formatFunctions
[format
] = funcName
;
34 var code
= "Date.prototype." + funcName
+ " = function(){return ";
37 for (var i
= 0; i
< format
.length
; ++i
) {
38 ch
= format
.charAt(i
);
39 if (!special
&& ch
== "\\") {
44 code
+= "'" + String
.escape(ch
) + "' + ";
47 code
+= Date
.getFormatCode(ch
);
50 eval(code
.substring(0, code
.length
- 3) + ";}");
53 Date
.getFormatCode = function(character
) {
56 return "String.leftPad(this.getDate(), 2, '0') + ";
58 return "Date.dayNames[this.getDay()].substring(0, 3) + ";
60 return "this.getDate() + ";
62 return "Date.dayNames[this.getDay()] + ";
64 return "this.getSuffix() + ";
66 return "this.getDay() + ";
68 return "this.getDayOfYear() + ";
70 return "this.getWeekOfYear() + ";
72 return "Date.monthNames[this.getMonth()] + ";
74 return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
76 return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
78 return "(this.getMonth() + 1) + ";
80 return "this.getDaysInMonth() + ";
82 return "(this.isLeapYear() ? 1 : 0) + ";
84 return "this.getFullYear() + ";
86 return "('' + this.getFullYear()).substring(2, 4) + ";
88 return "(this.getHours() < 12 ? 'am' : 'pm') + ";
90 return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
92 return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
94 return "this.getHours() + ";
96 return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
98 return "String.leftPad(this.getHours(), 2, '0') + ";
100 return "String.leftPad(this.getMinutes(), 2, '0') + ";
102 return "String.leftPad(this.getSeconds(), 2, '0') + ";
104 return "this.getGMTOffset() + ";
106 return "this.getTimezone() + ";
108 return "(this.getTimezoneOffset() * -60) + ";
110 return "'" + String
.escape(character
) + "' + ";
114 Date
.parseDate = function(input
, format
) {
115 if (Date
.parseFunctions
[format
] == null) {
116 Date
.createParser(format
);
118 var func
= Date
.parseFunctions
[format
];
119 return Date
[func
](input
);
122 Date
.createParser = function(format
) {
123 var funcName
= "parse" + Date
.parseFunctions
.count
++;
124 var regexNum
= Date
.parseRegexes
.length
;
125 var currentGroup
= 1;
126 Date
.parseFunctions
[format
] = funcName
;
128 var code
= "Date." + funcName
+ " = function(input){\n"
129 + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n"
130 + "var d = new Date();\n"
131 + "y = d.getFullYear();\n"
132 + "m = d.getMonth();\n"
133 + "d = d.getDate();\n"
134 + "var results = input.match(Date.parseRegexes[" + regexNum
+ "]);\n"
135 + "if (results && results.length > 0) {"
140 for (var i
= 0; i
< format
.length
; ++i
) {
141 ch
= format
.charAt(i
);
142 if (!special
&& ch
== "\\") {
147 regex
+= String
.escape(ch
);
150 obj
= Date
.formatCodeToRegex(ch
, currentGroup
);
151 currentGroup
+= obj
.g
;
153 if (obj
.g
&& obj
.c
) {
159 code
+= "if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
160 + "{return new Date(y, m, d, h, i, s);}\n"
161 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
162 + "{return new Date(y, m, d, h, i);}\n"
163 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0)\n"
164 + "{return new Date(y, m, d, h);}\n"
165 + "else if (y > 0 && m >= 0 && d > 0)\n"
166 + "{return new Date(y, m, d);}\n"
167 + "else if (y > 0 && m >= 0)\n"
168 + "{return new Date(y, m);}\n"
169 + "else if (y > 0)\n"
170 + "{return new Date(y);}\n"
173 Date
.parseRegexes
[regexNum
] = new RegExp("^" + regex
+ "$");
177 Date
.formatCodeToRegex = function(character
, currentGroup
) {
182 s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
186 c:"d = parseInt(results[" + currentGroup
+ "], 10);\n",
191 s:"(?:" + Date
.dayNames
.join("|") + ")"};
195 s:"(?:st|nd|rd|th)"};
210 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup
+ "].substring(0, 3)], 10);\n",
211 s:"(" + Date
.monthNames
.join("|") + ")"};
214 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup
+ "]], 10);\n",
215 s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
219 c:"m = parseInt(results[" + currentGroup
+ "], 10) - 1;\n",
231 c:"y = parseInt(results[" + currentGroup
+ "], 10);\n",
235 c:"var ty = parseInt(results[" + currentGroup
+ "], 10);\n"
236 + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
240 c:"if (results[" + currentGroup
+ "] == 'am') {\n"
241 + "if (h == 12) { h = 0; }\n"
242 + "} else { if (h < 12) { h += 12; }}",
246 c:"if (results[" + currentGroup
+ "] == 'AM') {\n"
247 + "if (h == 12) { h = 0; }\n"
248 + "} else { if (h < 12) { h += 12; }}",
255 c:"h = parseInt(results[" + currentGroup
+ "], 10);\n",
259 c:"i = parseInt(results[" + currentGroup
+ "], 10);\n",
263 c:"s = parseInt(results[" + currentGroup
+ "], 10);\n",
280 s:String
.escape(character
)};
284 Date
.prototype.getTimezone = function() {
285 return this.toString().replace(
286 /^.*? ([A
-Z
]{3}) [0-9]{4}.*$/, "$1").replace(
287 /^.*?\(([A
-Z
])[a
-z
]+ ([A
-Z
])[a
-z
]+ ([A
-Z
])[a
-z
]+\)$/, "$1$2$3");
290 Date
.prototype.getGMTOffset = function() {
291 return (this.getTimezoneOffset() > 0 ? "-" : "+")
292 + String
.leftPad(Math
.floor(this.getTimezoneOffset() / 60), 2, "0")
293 + String
.leftPad(this.getTimezoneOffset() % 60, 2, "0");
296 Date
.prototype.getDayOfYear = function() {
298 Date
.daysInMonth
[1] = this.isLeapYear() ? 29 : 28;
299 for (var i
= 0; i
< this.getMonth(); ++i
) {
300 num
+= Date
.daysInMonth
[i
];
302 return num
+ this.getDate() - 1;
305 Date
.prototype.getWeekOfYear = function() {
306 // Skip to Thursday of this week
307 var now
= this.getDayOfYear() + (4 - this.getDay());
308 // Find the first Thursday of the year
309 var jan1
= new Date(this.getFullYear(), 0, 1);
310 var then
= (7 - jan1
.getDay() + 4);
311 document
.write(then
);
312 return String
.leftPad(((now
- then
) / 7) + 1, 2, "0");
315 Date
.prototype.isLeapYear = function() {
316 var year
= this.getFullYear();
317 return ((year
& 3) == 0 && (year
% 100 || (year
% 400 == 0 && year
)));
320 Date
.prototype.getFirstDayOfMonth = function() {
321 var day
= (this.getDay() - (this.getDate() - 1)) % 7;
322 return (day
< 0) ? (day
+ 7) : day
;
325 Date
.prototype.getLastDayOfMonth = function() {
326 var day
= (this.getDay() + (Date
.daysInMonth
[this.getMonth()] - this.getDate())) % 7;
327 return (day
< 0) ? (day
+ 7) : day
;
330 Date
.prototype.getDaysInMonth = function() {
331 Date
.daysInMonth
[1] = this.isLeapYear() ? 29 : 28;
332 return Date
.daysInMonth
[this.getMonth()];
335 Date
.prototype.getSuffix = function() {
336 switch (this.getDate()) {
352 String
.escape = function(string
) {
353 return string
.replace(/('|\\)/g, "\\$1");
356 String
.leftPad = function (val
, size
, ch
) {
357 var result
= new String(val
);
361 while (result
.length
< size
) {
362 result
= ch
+ result
;
367 Date
.daysInMonth
= [31,28,31,30,31,30,31,31,30,31,30,31];
390 Date
.monthNumbers
= {
404 ISO8601LongPattern:"Y-m-d H:i:s",
405 ISO8601ShortPattern:"Y-m-d",
406 ShortDatePattern: "n/j/Y",
407 LongDatePattern: "l, F d, Y",
408 FullDateTimePattern: "l, F d, Y g:i:s A",
409 MonthDayPattern: "F d",
410 ShortTimePattern: "g:i A",
411 LongTimePattern: "g:i:s A",
412 SortableDateTimePattern: "Y-m-d\\TH:i:s",
413 UniversalSortableDateTimePattern: "Y-m-d H:i:sO",
414 YearMonthPattern: "F, Y"};
416 var date
= new Date("1/1/2007 1:11:11");
418 for (i
= 0; i
< 4000; ++i
) {
419 var shortFormat
= date
.dateFormat("Y-m-d");
420 var longFormat
= date
.dateFormat("l, F d, Y g:i:s A");
421 date
.setTime(date
.getTime() + 84266956);
424 // FIXME: Find a way to validate this test.
425 // https://bugs.webkit.org/show_bug.cgi?id=114849
428 print("JSC EXCEPTION FUZZ: Caught exception: " + e
);