3 <script src=
"../htmlrunner.js"></script>
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
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
];
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 ";
36 for (var i
= 0; i
< format
.length
; ++i
) {
37 ch
= format
.charAt(i
);
38 if (!special
&& ch
== "\\") {
43 code
+= "'" + String
.escape(ch
) + "' + ";
46 code
+= Date
.getFormatCode(ch
);
49 eval(code
.substring(0, code
.length
- 3) + ";}");
52 Date
.getFormatCode = function(character
) {
55 return "String.leftPad(this.getDate(), 2, '0') + ";
57 return "Date.dayNames[this.getDay()].substring(0, 3) + ";
59 return "this.getDate() + ";
61 return "Date.dayNames[this.getDay()] + ";
63 return "this.getSuffix() + ";
65 return "this.getDay() + ";
67 return "this.getDayOfYear() + ";
69 return "this.getWeekOfYear() + ";
71 return "Date.monthNames[this.getMonth()] + ";
73 return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
75 return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
77 return "(this.getMonth() + 1) + ";
79 return "this.getDaysInMonth() + ";
81 return "(this.isLeapYear() ? 1 : 0) + ";
83 return "this.getFullYear() + ";
85 return "('' + this.getFullYear()).substring(2, 4) + ";
87 return "(this.getHours() < 12 ? 'am' : 'pm') + ";
89 return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
91 return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
93 return "this.getHours() + ";
95 return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
97 return "String.leftPad(this.getHours(), 2, '0') + ";
99 return "String.leftPad(this.getMinutes(), 2, '0') + ";
101 return "String.leftPad(this.getSeconds(), 2, '0') + ";
103 return "this.getGMTOffset() + ";
105 return "this.getTimezone() + ";
107 return "(this.getTimezoneOffset() * -60) + ";
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) {"
139 for (var i
= 0; i
< format
.length
; ++i
) {
140 ch
= format
.charAt(i
);
141 if (!special
&& ch
== "\\") {
146 regex
+= String
.escape(ch
);
149 obj
= Date
.formatCodeToRegex(ch
, currentGroup
);
150 currentGroup
+= obj
.g
;
152 if (obj
.g
&& 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"
172 Date
.parseRegexes
[regexNum
] = new RegExp("^" + regex
+ "$");
176 Date
.formatCodeToRegex = function(character
, currentGroup
) {
181 s
:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
185 c
:"d = parseInt(results[" + currentGroup
+ "], 10);\n",
190 s
:"(?:" + Date
.dayNames
.join("|") + ")"};
194 s
:"(?:st|nd|rd|th)"};
209 c
:"m = parseInt(Date.monthNumbers[results[" + currentGroup
+ "].substring(0, 3)], 10);\n",
210 s
:"(" + Date
.monthNames
.join("|") + ")"};
213 c
:"m = parseInt(Date.monthNumbers[results[" + currentGroup
+ "]], 10);\n",
214 s
:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
218 c
:"m = parseInt(results[" + currentGroup
+ "], 10) - 1;\n",
230 c
:"y = parseInt(results[" + currentGroup
+ "], 10);\n",
234 c
:"var ty = parseInt(results[" + currentGroup
+ "], 10);\n"
235 + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
239 c
:"if (results[" + currentGroup
+ "] == 'am') {\n"
240 + "if (h == 12) { h = 0; }\n"
241 + "} else { if (h < 12) { h += 12; }}",
245 c
:"if (results[" + currentGroup
+ "] == 'AM') {\n"
246 + "if (h == 12) { h = 0; }\n"
247 + "} else { if (h < 12) { h += 12; }}",
254 c
:"h = parseInt(results[" + currentGroup
+ "], 10);\n",
258 c
:"i = parseInt(results[" + currentGroup
+ "], 10);\n",
262 c
:"s = parseInt(results[" + currentGroup
+ "], 10);\n",
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() {
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()) {
351 String
.escape = function(string
) {
352 return string
.replace(/('|\\)/g, "\\$1");
355 String
.leftPad = function (val
, size
, ch
) {
356 var result
= new String(val
);
360 while (result
.length
< size
) {
361 result
= ch
+ result
;
366 Date
.daysInMonth
= [31,28,31,30,31,30,31,31,30,31,30,31];
389 Date
.monthNumbers
= {
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);