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-tofte</title>
33
<h3>date-format-tofte</h3>
39
var _sunSpiderStartDate = new Date();
41
function arrayExists(array, x) {
42
for (var i = 0; i < array.length; i++) {
43
if (array[i] == x) return true;
48
Date.prototype.formatDate = function (input,time) {
50
// a PHP date like function, for formatting date strings
51
// See: http://www.php.net/date
53
// input : format string
54
// time : epoch time (seconds, and optional)
56
// if time is not passed, formatting is based on
57
// the current "this" date object's set time.
60
// a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L,
61
// m, M, n, O, r, s, S, t, U, w, W, y, Y, z
64
// I (capital i), T, Z
66
var switches = ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H",
67
"i", "j", "l", "L", "m", "M", "n", "O", "r", "s",
68
"S", "t", "U", "w", "W", "y", "Y", "z"];
69
var daysLong = ["Sunday", "Monday", "Tuesday", "Wednesday",
70
"Thursday", "Friday", "Saturday"];
71
var daysShort = ["Sun", "Mon", "Tue", "Wed",
73
var monthsShort = ["Jan", "Feb", "Mar", "Apr",
74
"May", "Jun", "Jul", "Aug", "Sep",
76
var monthsLong = ["January", "February", "March", "April",
77
"May", "June", "July", "August", "September",
78
"October", "November", "December"];
79
var daysSuffix = ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th
80
"th", "th", "th", "th", "th", "th", "th", // 8th - 14th
81
"th", "th", "th", "th", "th", "th", "st", // 15th - 21st
82
"nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th
83
"th", "th", "st"]; // 29th - 31st
86
// Lowercase Ante meridiem and Post meridiem
87
return self.getHours() > 11? "pm" : "am";
90
// Uppercase Ante meridiem and Post meridiem
91
return self.getHours() > 11? "PM" : "AM";
95
// Swatch internet time. code simply grabbed from ppk,
96
// since I was feeling lazy:
97
// http://www.xs4all.nl/~ppk/js/beat.html
98
var off = (self.getTimezoneOffset() + 60)*60;
99
var theSeconds = (self.getHours() * 3600) +
100
(self.getMinutes() * 60) +
101
self.getSeconds() + off;
102
var beat = Math.floor(theSeconds/86.4);
103
if (beat > 1000) beat -= 1000;
104
if (beat < 0) beat += 1000;
105
if ((""+beat).length == 1) beat = "00"+beat;
106
if ((""+beat).length == 2) beat = "0"+beat;
111
// Day of the month, 2 digits with leading zeros
112
return new String(self.getDate()).length == 1?
113
"0"+self.getDate() : self.getDate();
116
// A textual representation of a day, three letters
117
return daysShort[self.getDay()];
120
// A full textual representation of a month
121
return monthsLong[self.getMonth()];
124
// 12-hour format of an hour without leading zeros
125
return self.getHours() > 12? self.getHours()-12 : self.getHours();
128
// 24-hour format of an hour without leading zeros
129
return self.getHours();
132
// 12-hour format of an hour with leading zeros
133
if (self.getHours() > 12) {
134
var s = new String(self.getHours()-12);
135
return s.length == 1?
136
"0"+ (self.getHours()-12) : self.getHours()-12;
138
var s = new String(self.getHours());
139
return s.length == 1?
140
"0"+self.getHours() : self.getHours();
144
// 24-hour format of an hour with leading zeros
145
return new String(self.getHours()).length == 1?
146
"0"+self.getHours() : self.getHours();
149
// Minutes with leading zeros
150
return new String(self.getMinutes()).length == 1?
151
"0"+self.getMinutes() : self.getMinutes();
154
// Day of the month without leading zeros
155
return self.getDate();
158
// A full textual representation of the day of the week
159
return daysLong[self.getDay()];
162
// leap year or not. 1 if leap year, 0 if not.
163
// the logic should match iso's 8601 standard.
166
(y_ % 4 == 0 && y_ % 100 != 0) ||
167
(y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0)
175
// Numeric representation of a month, with leading zeros
176
return self.getMonth() < 9?
177
"0"+(self.getMonth()+1) :
181
// A short textual representation of a month, three letters
182
return monthsShort[self.getMonth()];
185
// Numeric representation of a month, without leading zeros
186
return self.getMonth()+1;
189
// Difference to Greenwich time (GMT) in hours
190
var os = Math.abs(self.getTimezoneOffset());
191
var h = ""+Math.floor(os/60);
193
h.length == 1? h = "0"+h:1;
194
m.length == 1? m = "0"+m:1;
195
return self.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
198
// RFC 822 formatted date
201
r = D() + ", " + j() + " " + M() + " " + Y() +
202
// 16 : 01 : 07 +0200
203
" " + H() + ":" + i() + ":" + s() + " " + O();
207
// English ordinal suffix for the day of the month, 2 characters
208
return daysSuffix[self.getDate()-1];
211
// Seconds, with leading zeros
212
return new String(self.getSeconds()).length == 1?
213
"0"+self.getSeconds() : self.getSeconds();
217
// thanks to Matt Bannon for some much needed code-fixes here!
218
var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31];
219
if (L()==1 && n()==2) return 29; // leap day
220
return daysinmonths[n()];
223
// Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
224
return Math.round(self.getTime()/1000);
227
// Weeknumber, as per ISO specification:
228
// http://www.cl.cam.ac.uk/~mgk25/iso-time.html
230
// if the day is three days before newyears eve,
231
// there's a chance it's "week 1" of next year.
232
// here we check for that.
233
var beforeNY = 364+L() - z();
235
var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6.
236
if (beforeNY <= 2 && weekday <= 2-beforeNY) {
239
// similarly, if the day is within threedays of newyears
240
// there's a chance it belongs in the old year.
241
var ny = new Date("January 1 " + Y() + " 00:00:00");
242
var nyDay = ny.getDay()!=0?ny.getDay()-1:6;
246
(afterNY >= (6-nyDay))
248
// Since I'm not sure we can just always return 53,
249
// i call the function here again, using the last day
250
// of the previous year, as the date, and then just
252
var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00");
253
return prevNY.formatDate("W");
256
// week 1, is the week that has the first thursday in it.
257
// note that this value is not zero index.
259
// first day of the year fell on a thursday, or earlier.
260
return 1 + Math.floor( ( z() + nyDay ) / 7 );
262
// first day of the year fell on a friday, or later.
263
return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 );
267
// Numeric representation of the day of the week
268
return self.getDay();
272
// A full numeric representation of a year, 4 digits
274
// we first check, if getFullYear is supported. if it
275
// is, we just use that. ppks code is nice, but wont
276
// work with dates outside 1900-2038, or something like that
277
if (self.getFullYear) {
278
var newDate = new Date("January 1 2001 00:00:00 +0000");
279
var x = newDate .getFullYear();
281
// i trust the method now
282
return self.getFullYear();
286
// codes thanks to ppk:
287
// http://www.xs4all.nl/~ppk/js/introdate.html
288
var x = self.getYear();
290
y += (y < 38) ? 2000 : 1900;
294
// A two-digit representation of a year
296
return y.substring(y.length-2,y.length);
299
// The day of the year, zero indexed! 0 through 366
300
var t = new Date("January 1 " + Y() + " 00:00:00");
301
var diff = self.getTime() - t.getTime();
302
return Math.floor(diff/1000/60/60/24);
308
var prevTime = self.getTime();
312
var ia = input.split("");
315
if (ia[ij] == "\\") {
316
// this is our way of allowing users to escape stuff
319
if (arrayExists(switches,ia[ij])) {
320
ia[ij] = eval(ia[ij] + "()");
325
// reset time, back to what it was
327
self.setTime(prevTime);
332
var date = new Date("1/1/2007 1:11:11");
334
for (i = 0; i < 500; ++i) {
335
var shortFormat = date.formatDate("Y-m-d");
336
var longFormat = date.formatDate("l, F d, Y g:i:s A");
337
date.setTime(date.getTime() + 84266956);
342
var _sunSpiderInterval = new Date() - _sunSpiderStartDate;
344
document.getElementById("console").innerHTML = _sunSpiderInterval;