4
Copyright (C) 2007 Apple Inc. All rights reserved.
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
9
1. Redistributions of source code must retain the above copyright
10
notice, this list of conditions and the following disclaimer.
11
2. Redistributions in binary form must reproduce the above copyright
12
notice, this list of conditions and the following disclaimer in the
13
documentation and/or other materials provided with the distribution.
15
THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
<title>SunSpider date-format-xparb</title>
33
<h3>date-format-xparb</h3>
39
var _sunSpiderStartDate = new Date();
42
* Copyright (C) 2004 Baron Schwartz <baron at sequent dot org>
44
* This program is free software; you can redistribute it and/or modify it
45
* under the terms of the GNU Lesser General Public License as published by the
46
* Free Software Foundation, version 2.1.
48
* This program is distributed in the hope that it will be useful, but WITHOUT
49
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
50
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
54
Date.parseFunctions = {count:0};
55
Date.parseRegexes = [];
56
Date.formatFunctions = {count:0};
58
Date.prototype.dateFormat = function(format) {
59
if (Date.formatFunctions[format] == null) {
60
Date.createNewFormat(format);
62
var func = Date.formatFunctions[format];
66
Date.createNewFormat = function(format) {
67
var funcName = "format" + Date.formatFunctions.count++;
68
Date.formatFunctions[format] = funcName;
69
var code = "Date.prototype." + funcName + " = function(){return ";
72
for (var i = 0; i < format.length; ++i) {
73
ch = format.charAt(i);
74
if (!special && ch == "\\") {
79
code += "'" + String.escape(ch) + "' + ";
82
code += Date.getFormatCode(ch);
85
eval(code.substring(0, code.length - 3) + ";}");
88
Date.getFormatCode = function(character) {
91
return "String.leftPad(this.getDate(), 2, '0') + ";
93
return "Date.dayNames[this.getDay()].substring(0, 3) + ";
95
return "this.getDate() + ";
97
return "Date.dayNames[this.getDay()] + ";
99
return "this.getSuffix() + ";
101
return "this.getDay() + ";
103
return "this.getDayOfYear() + ";
105
return "this.getWeekOfYear() + ";
107
return "Date.monthNames[this.getMonth()] + ";
109
return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
111
return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
113
return "(this.getMonth() + 1) + ";
115
return "this.getDaysInMonth() + ";
117
return "(this.isLeapYear() ? 1 : 0) + ";
119
return "this.getFullYear() + ";
121
return "('' + this.getFullYear()).substring(2, 4) + ";
123
return "(this.getHours() < 12 ? 'am' : 'pm') + ";
125
return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
127
return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
129
return "this.getHours() + ";
131
return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
133
return "String.leftPad(this.getHours(), 2, '0') + ";
135
return "String.leftPad(this.getMinutes(), 2, '0') + ";
137
return "String.leftPad(this.getSeconds(), 2, '0') + ";
139
return "this.getGMTOffset() + ";
141
return "this.getTimezone() + ";
143
return "(this.getTimezoneOffset() * -60) + ";
145
return "'" + String.escape(character) + "' + ";
149
Date.parseDate = function(input, format) {
150
if (Date.parseFunctions[format] == null) {
151
Date.createParser(format);
153
var func = Date.parseFunctions[format];
154
return Date[func](input);
157
Date.createParser = function(format) {
158
var funcName = "parse" + Date.parseFunctions.count++;
159
var regexNum = Date.parseRegexes.length;
160
var currentGroup = 1;
161
Date.parseFunctions[format] = funcName;
163
var code = "Date." + funcName + " = function(input){\n"
164
+ "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n"
165
+ "var d = new Date();\n"
166
+ "y = d.getFullYear();\n"
167
+ "m = d.getMonth();\n"
168
+ "d = d.getDate();\n"
169
+ "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
170
+ "if (results && results.length > 0) {"
175
for (var i = 0; i < format.length; ++i) {
176
ch = format.charAt(i);
177
if (!special && ch == "\\") {
182
regex += String.escape(ch);
185
obj = Date.formatCodeToRegex(ch, currentGroup);
186
currentGroup += obj.g;
188
if (obj.g && obj.c) {
194
code += "if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
195
+ "{return new Date(y, m, d, h, i, s);}\n"
196
+ "else if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
197
+ "{return new Date(y, m, d, h, i);}\n"
198
+ "else if (y > 0 && m >= 0 && d > 0 && h >= 0)\n"
199
+ "{return new Date(y, m, d, h);}\n"
200
+ "else if (y > 0 && m >= 0 && d > 0)\n"
201
+ "{return new Date(y, m, d);}\n"
202
+ "else if (y > 0 && m >= 0)\n"
203
+ "{return new Date(y, m);}\n"
204
+ "else if (y > 0)\n"
205
+ "{return new Date(y);}\n"
208
Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$");
212
Date.formatCodeToRegex = function(character, currentGroup) {
217
s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
221
c:"d = parseInt(results[" + currentGroup + "], 10);\n",
226
s:"(?:" + Date.dayNames.join("|") + ")"};
230
s:"(?:st|nd|rd|th)"};
245
c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].substring(0, 3)], 10);\n",
246
s:"(" + Date.monthNames.join("|") + ")"};
249
c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10);\n",
250
s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
254
c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n",
266
c:"y = parseInt(results[" + currentGroup + "], 10);\n",
270
c:"var ty = parseInt(results[" + currentGroup + "], 10);\n"
271
+ "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
275
c:"if (results[" + currentGroup + "] == 'am') {\n"
276
+ "if (h == 12) { h = 0; }\n"
277
+ "} else { if (h < 12) { h += 12; }}",
281
c:"if (results[" + currentGroup + "] == 'AM') {\n"
282
+ "if (h == 12) { h = 0; }\n"
283
+ "} else { if (h < 12) { h += 12; }}",
290
c:"h = parseInt(results[" + currentGroup + "], 10);\n",
294
c:"i = parseInt(results[" + currentGroup + "], 10);\n",
298
c:"s = parseInt(results[" + currentGroup + "], 10);\n",
315
s:String.escape(character)};
319
Date.prototype.getTimezone = function() {
320
return this.toString().replace(
321
/^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace(
322
/^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3");
325
Date.prototype.getGMTOffset = function() {
326
return (this.getTimezoneOffset() > 0 ? "-" : "+")
327
+ String.leftPad(Math.floor(this.getTimezoneOffset() / 60), 2, "0")
328
+ String.leftPad(this.getTimezoneOffset() % 60, 2, "0");
331
Date.prototype.getDayOfYear = function() {
333
Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
334
for (var i = 0; i < this.getMonth(); ++i) {
335
num += Date.daysInMonth[i];
337
return num + this.getDate() - 1;
340
Date.prototype.getWeekOfYear = function() {
341
// Skip to Thursday of this week
342
var now = this.getDayOfYear() + (4 - this.getDay());
343
// Find the first Thursday of the year
344
var jan1 = new Date(this.getFullYear(), 0, 1);
345
var then = (7 - jan1.getDay() + 4);
346
document.write(then);
347
return String.leftPad(((now - then) / 7) + 1, 2, "0");
350
Date.prototype.isLeapYear = function() {
351
var year = this.getFullYear();
352
return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
355
Date.prototype.getFirstDayOfMonth = function() {
356
var day = (this.getDay() - (this.getDate() - 1)) % 7;
357
return (day < 0) ? (day + 7) : day;
360
Date.prototype.getLastDayOfMonth = function() {
361
var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate())) % 7;
362
return (day < 0) ? (day + 7) : day;
365
Date.prototype.getDaysInMonth = function() {
366
Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
367
return Date.daysInMonth[this.getMonth()];
370
Date.prototype.getSuffix = function() {
371
switch (this.getDate()) {
387
String.escape = function(string) {
388
return string.replace(/('|\\)/g, "\\$1");
391
String.leftPad = function (val, size, ch) {
392
var result = new String(val);
396
while (result.length < size) {
397
result = ch + result;
402
Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
425
Date.monthNumbers = {
439
ISO8601LongPattern:"Y-m-d H:i:s",
440
ISO8601ShortPattern:"Y-m-d",
441
ShortDatePattern: "n/j/Y",
442
LongDatePattern: "l, F d, Y",
443
FullDateTimePattern: "l, F d, Y g:i:s A",
444
MonthDayPattern: "F d",
445
ShortTimePattern: "g:i A",
446
LongTimePattern: "g:i:s A",
447
SortableDateTimePattern: "Y-m-d\\TH:i:s",
448
UniversalSortableDateTimePattern: "Y-m-d H:i:sO",
449
YearMonthPattern: "F, Y"};
451
var date = new Date("1/1/2007 1:11:11");
453
for (i = 0; i < 4000; ++i) {
454
var shortFormat = date.dateFormat("Y-m-d");
455
var longFormat = date.dateFormat("l, F d, Y g:i:s A");
456
date.setTime(date.getTime() + 84266956);
460
var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
462
document.getElementById("console").innerHTML = _sunSpiderInterval;