]>
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 Date
.parseFunctions
= {count:0};
17 Date
.parseRegexes
= [];
18 Date
.formatFunctions
= {count:0};
20 Date
.prototype.dateFormat = function(format
) {
21 if (Date
.formatFunctions
[format
] == null) {
22 Date
.createNewFormat(format
);
24 var func
= Date
.formatFunctions
[format
];
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 ";
34 for (var i
= 0; i
< format
.length
; ++i
) {
35 ch
= format
.charAt(i
);
36 if (!special
&& ch
== "\\") {
41 code
+= "'" + String
.escape(ch
) + "' + ";
44 code
+= Date
.getFormatCode(ch
);
47 eval(code
.substring(0, code
.length
- 3) + ";}");
50 Date
.getFormatCode = function(character
) {
53 return "String.leftPad(this.getDate(), 2, '0') + ";
55 return "Date.dayNames[this.getDay()].substring(0, 3) + ";
57 return "this.getDate() + ";
59 return "Date.dayNames[this.getDay()] + ";
61 return "this.getSuffix() + ";
63 return "this.getDay() + ";
65 return "this.getDayOfYear() + ";
67 return "this.getWeekOfYear() + ";
69 return "Date.monthNames[this.getMonth()] + ";
71 return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
73 return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
75 return "(this.getMonth() + 1) + ";
77 return "this.getDaysInMonth() + ";
79 return "(this.isLeapYear() ? 1 : 0) + ";
81 return "this.getFullYear() + ";
83 return "('' + this.getFullYear()).substring(2, 4) + ";
85 return "(this.getHours() < 12 ? 'am' : 'pm') + ";
87 return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
89 return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
91 return "this.getHours() + ";
93 return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
95 return "String.leftPad(this.getHours(), 2, '0') + ";
97 return "String.leftPad(this.getMinutes(), 2, '0') + ";
99 return "String.leftPad(this.getSeconds(), 2, '0') + ";
101 return "this.getGMTOffset() + ";
103 return "this.getTimezone() + ";
105 return "(this.getTimezoneOffset() * -60) + ";
107 return "'" + String
.escape(character
) + "' + ";
111 Date
.parseDate = function(input
, format
) {
112 if (Date
.parseFunctions
[format
] == null) {
113 Date
.createParser(format
);
115 var func
= Date
.parseFunctions
[format
];
116 return Date
[func
](input
);
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
;
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) {"
137 for (var i
= 0; i
< format
.length
; ++i
) {
138 ch
= format
.charAt(i
);
139 if (!special
&& ch
== "\\") {
144 regex
+= String
.escape(ch
);
147 obj
= Date
.formatCodeToRegex(ch
, currentGroup
);
148 currentGroup
+= obj
.g
;
150 if (obj
.g
&& obj
.c
) {
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"
170 Date
.parseRegexes
[regexNum
] = new RegExp("^" + regex
+ "$");
174 Date
.formatCodeToRegex = function(character
, currentGroup
) {
179 s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
183 c:"d = parseInt(results[" + currentGroup
+ "], 10);\n",
188 s:"(?:" + Date
.dayNames
.join("|") + ")"};
192 s:"(?:st|nd|rd|th)"};
207 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup
+ "].substring(0, 3)], 10);\n",
208 s:"(" + Date
.monthNames
.join("|") + ")"};
211 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup
+ "]], 10);\n",
212 s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
216 c:"m = parseInt(results[" + currentGroup
+ "], 10) - 1;\n",
228 c:"y = parseInt(results[" + currentGroup
+ "], 10);\n",
232 c:"var ty = parseInt(results[" + currentGroup
+ "], 10);\n"
233 + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
237 c:"if (results[" + currentGroup
+ "] == 'am') {\n"
238 + "if (h == 12) { h = 0; }\n"
239 + "} else { if (h < 12) { h += 12; }}",
243 c:"if (results[" + currentGroup
+ "] == 'AM') {\n"
244 + "if (h == 12) { h = 0; }\n"
245 + "} else { if (h < 12) { h += 12; }}",
252 c:"h = parseInt(results[" + currentGroup
+ "], 10);\n",
256 c:"i = parseInt(results[" + currentGroup
+ "], 10);\n",
260 c:"s = parseInt(results[" + currentGroup
+ "], 10);\n",
277 s:String
.escape(character
)};
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");
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");
293 Date
.prototype.getDayOfYear = function() {
295 Date
.daysInMonth
[1] = this.isLeapYear() ? 29 : 28;
296 for (var i
= 0; i
< this.getMonth(); ++i
) {
297 num
+= Date
.daysInMonth
[i
];
299 return num
+ this.getDate() - 1;
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");
312 Date
.prototype.isLeapYear = function() {
313 var year
= this.getFullYear();
314 return ((year
& 3) == 0 && (year
% 100 || (year
% 400 == 0 && year
)));
317 Date
.prototype.getFirstDayOfMonth = function() {
318 var day
= (this.getDay() - (this.getDate() - 1)) % 7;
319 return (day
< 0) ? (day
+ 7) : day
;
322 Date
.prototype.getLastDayOfMonth = function() {
323 var day
= (this.getDay() + (Date
.daysInMonth
[this.getMonth()] - this.getDate())) % 7;
324 return (day
< 0) ? (day
+ 7) : day
;
327 Date
.prototype.getDaysInMonth = function() {
328 Date
.daysInMonth
[1] = this.isLeapYear() ? 29 : 28;
329 return Date
.daysInMonth
[this.getMonth()];
332 Date
.prototype.getSuffix = function() {
333 switch (this.getDate()) {
349 String
.escape = function(string
) {
350 return string
.replace(/('|\\)/g, "\\$1");
353 String
.leftPad = function (val
, size
, ch
) {
354 var result
= new String(val
);
358 while (result
.length
< size
) {
359 result
= ch
+ result
;
364 Date
.daysInMonth
= [31,28,31,30,31,30,31,31,30,31,30,31];
387 Date
.monthNumbers
= {
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"};
413 var date
= new Date("1/1/2007 1:11:11");
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);
421 // FIXME: Find a way to validate this test.
422 // https://bugs.webkit.org/show_bug.cgi?id=114849
425 print("JSC EXCEPTION FUZZ: Caught exception: " + e
);