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