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>
29 <link rel=
"stylesheet" href=
"sunspider.css"></link>
33 <h3>date-format-xparb
</h3>
40 * Copyright (C) 2004 Baron Schwartz <baron at sequent dot org>
42 * This program is free software; you can redistribute it and/or modify it
43 * under the terms of the GNU Lesser General Public License as published by the
44 * Free Software Foundation, version 2.1.
46 * This program is distributed in the hope that it will be useful, but WITHOUT
47 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
48 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
52 Date
.parseFunctions
= {count
:0};
53 Date
.parseRegexes
= [];
54 Date
.formatFunctions
= {count
:0};
56 Date
.prototype.dateFormat = function(format
) {
57 if (Date
.formatFunctions
[format
] == null) {
58 Date
.createNewFormat(format
);
60 var func
= Date
.formatFunctions
[format
];
64 Date
.createNewFormat = function(format
) {
65 var funcName
= "format" + Date
.formatFunctions
.count
++;
66 Date
.formatFunctions
[format
] = funcName
;
67 var code
= "Date.prototype." + funcName
+ " = function(){return ";
70 for (var i
= 0; i
< format
.length
; ++i
) {
71 ch
= format
.charAt(i
);
72 if (!special
&& ch
== "\\") {
77 code
+= "'" + String
.escape(ch
) + "' + ";
80 code
+= Date
.getFormatCode(ch
);
83 eval(code
.substring(0, code
.length
- 3) + ";}");
86 Date
.getFormatCode = function(character
) {
89 return "String.leftPad(this.getDate(), 2, '0') + ";
91 return "Date.dayNames[this.getDay()].substring(0, 3) + ";
93 return "this.getDate() + ";
95 return "Date.dayNames[this.getDay()] + ";
97 return "this.getSuffix() + ";
99 return "this.getDay() + ";
101 return "this.getDayOfYear() + ";
103 return "this.getWeekOfYear() + ";
105 return "Date.monthNames[this.getMonth()] + ";
107 return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
109 return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
111 return "(this.getMonth() + 1) + ";
113 return "this.getDaysInMonth() + ";
115 return "(this.isLeapYear() ? 1 : 0) + ";
117 return "this.getFullYear() + ";
119 return "('' + this.getFullYear()).substring(2, 4) + ";
121 return "(this.getHours() < 12 ? 'am' : 'pm') + ";
123 return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
125 return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
127 return "this.getHours() + ";
129 return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
131 return "String.leftPad(this.getHours(), 2, '0') + ";
133 return "String.leftPad(this.getMinutes(), 2, '0') + ";
135 return "String.leftPad(this.getSeconds(), 2, '0') + ";
137 return "this.getGMTOffset() + ";
139 return "this.getTimezone() + ";
141 return "(this.getTimezoneOffset() * -60) + ";
143 return "'" + String
.escape(character
) + "' + ";
147 Date
.parseDate = function(input
, format
) {
148 if (Date
.parseFunctions
[format
] == null) {
149 Date
.createParser(format
);
151 var func
= Date
.parseFunctions
[format
];
152 return Date
[func
](input
);
155 Date
.createParser = function(format
) {
156 var funcName
= "parse" + Date
.parseFunctions
.count
++;
157 var regexNum
= Date
.parseRegexes
.length
;
158 var currentGroup
= 1;
159 Date
.parseFunctions
[format
] = funcName
;
161 var code
= "Date." + funcName
+ " = function(input){\n"
162 + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n"
163 + "var d = new Date();\n"
164 + "y = d.getFullYear();\n"
165 + "m = d.getMonth();\n"
166 + "d = d.getDate();\n"
167 + "var results = input.match(Date.parseRegexes[" + regexNum
+ "]);\n"
168 + "if (results && results.length > 0) {"
173 for (var i
= 0; i
< format
.length
; ++i
) {
174 ch
= format
.charAt(i
);
175 if (!special
&& ch
== "\\") {
180 regex
+= String
.escape(ch
);
183 obj
= Date
.formatCodeToRegex(ch
, currentGroup
);
184 currentGroup
+= obj
.g
;
186 if (obj
.g
&& obj
.c
) {
192 code
+= "if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
193 + "{return new Date(y, m, d, h, i, s);}\n"
194 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
195 + "{return new Date(y, m, d, h, i);}\n"
196 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0)\n"
197 + "{return new Date(y, m, d, h);}\n"
198 + "else if (y > 0 && m >= 0 && d > 0)\n"
199 + "{return new Date(y, m, d);}\n"
200 + "else if (y > 0 && m >= 0)\n"
201 + "{return new Date(y, m);}\n"
202 + "else if (y > 0)\n"
203 + "{return new Date(y);}\n"
206 Date
.parseRegexes
[regexNum
] = new RegExp("^" + regex
+ "$");
210 Date
.formatCodeToRegex = function(character
, currentGroup
) {
215 s
:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
219 c
:"d = parseInt(results[" + currentGroup
+ "], 10);\n",
224 s
:"(?:" + Date
.dayNames
.join("|") + ")"};
228 s
:"(?:st|nd|rd|th)"};
243 c
:"m = parseInt(Date.monthNumbers[results[" + currentGroup
+ "].substring(0, 3)], 10);\n",
244 s
:"(" + Date
.monthNames
.join("|") + ")"};
247 c
:"m = parseInt(Date.monthNumbers[results[" + currentGroup
+ "]], 10);\n",
248 s
:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
252 c
:"m = parseInt(results[" + currentGroup
+ "], 10) - 1;\n",
264 c
:"y = parseInt(results[" + currentGroup
+ "], 10);\n",
268 c
:"var ty = parseInt(results[" + currentGroup
+ "], 10);\n"
269 + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
273 c
:"if (results[" + currentGroup
+ "] == 'am') {\n"
274 + "if (h == 12) { h = 0; }\n"
275 + "} else { if (h < 12) { h += 12; }}",
279 c
:"if (results[" + currentGroup
+ "] == 'AM') {\n"
280 + "if (h == 12) { h = 0; }\n"
281 + "} else { if (h < 12) { h += 12; }}",
288 c
:"h = parseInt(results[" + currentGroup
+ "], 10);\n",
292 c
:"i = parseInt(results[" + currentGroup
+ "], 10);\n",
296 c
:"s = parseInt(results[" + currentGroup
+ "], 10);\n",
313 s
:String
.escape(character
)};
317 Date
.prototype.getTimezone = function() {
318 return this.toString().replace(
319 /^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace(
320 /^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3");
323 Date
.prototype.getGMTOffset = function() {
324 return (this.getTimezoneOffset() > 0 ? "-" : "+")
325 + String
.leftPad(Math
.floor(this.getTimezoneOffset() / 60), 2, "0")
326 + String
.leftPad(this.getTimezoneOffset() % 60, 2, "0");
329 Date
.prototype.getDayOfYear = function() {
331 Date
.daysInMonth
[1] = this.isLeapYear() ? 29 : 28;
332 for (var i
= 0; i
< this.getMonth(); ++i
) {
333 num
+= Date
.daysInMonth
[i
];
335 return num
+ this.getDate() - 1;
338 Date
.prototype.getWeekOfYear = function() {
339 // Skip to Thursday of this week
340 var now
= this.getDayOfYear() + (4 - this.getDay());
341 // Find the first Thursday of the year
342 var jan1
= new Date(this.getFullYear(), 0, 1);
343 var then
= (7 - jan1
.getDay() + 4);
344 document
.write(then
);
345 return String
.leftPad(((now
- then
) / 7) + 1, 2, "0");
348 Date
.prototype.isLeapYear = function() {
349 var year
= this.getFullYear();
350 return ((year
& 3) == 0 && (year
% 100 || (year
% 400 == 0 && year
)));
353 Date
.prototype.getFirstDayOfMonth = function() {
354 var day
= (this.getDay() - (this.getDate() - 1)) % 7;
355 return (day
< 0) ? (day
+ 7) : day
;
358 Date
.prototype.getLastDayOfMonth = function() {
359 var day
= (this.getDay() + (Date
.daysInMonth
[this.getMonth()] - this.getDate())) % 7;
360 return (day
< 0) ? (day
+ 7) : day
;
363 Date
.prototype.getDaysInMonth = function() {
364 Date
.daysInMonth
[1] = this.isLeapYear() ? 29 : 28;
365 return Date
.daysInMonth
[this.getMonth()];
368 Date
.prototype.getSuffix = function() {
369 switch (this.getDate()) {
385 String
.escape = function(string
) {
386 return string
.replace(/('|\\)/g, "\\$1");
389 String
.leftPad = function (val
, size
, ch
) {
390 var result
= new String(val
);
394 while (result
.length
< size
) {
395 result
= ch
+ result
;
400 Date
.daysInMonth
= [31,28,31,30,31,30,31,31,30,31,30,31];
423 Date
.monthNumbers
= {
437 ISO8601LongPattern
:"Y-m-d H:i:s",
438 ISO8601ShortPattern
:"Y-m-d",
439 ShortDatePattern
: "n/j/Y",
440 LongDatePattern
: "l, F d, Y",
441 FullDateTimePattern
: "l, F d, Y g:i:s A",
442 MonthDayPattern
: "F d",
443 ShortTimePattern
: "g:i A",
444 LongTimePattern
: "g:i:s A",
445 SortableDateTimePattern
: "Y-m-d\\TH:i:s",
446 UniversalSortableDateTimePattern
: "Y-m-d H:i:sO",
447 YearMonthPattern
: "F, Y"};
449 var date
= new Date("1/1/2007 1:11:11");
451 for (i
= 0; i
< 4000; ++i
) {
452 var shortFormat
= date
.dateFormat("Y-m-d");
453 var longFormat
= date
.dateFormat("l, F d, Y g:i:s A");
454 date
.setTime(date
.getTime() + 84266956);