|
1 <!DOCTYPE html> |
|
2 <head> |
|
3 <!-- |
|
4 Copyright (C) 2007 Apple Inc. All rights reserved. |
|
5 |
|
6 Redistribution and use in source and binary forms, with or without |
|
7 modification, are permitted provided that the following conditions |
|
8 are met: |
|
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. |
|
14 |
|
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. |
|
26 --> |
|
27 |
|
28 <title>SunSpider date-format-tofte</title> |
|
29 |
|
30 </head> |
|
31 |
|
32 <body> |
|
33 <h3>date-format-tofte</h3> |
|
34 <div id="console"> |
|
35 </div> |
|
36 |
|
37 <script> |
|
38 |
|
39 var _sunSpiderStartDate = new Date(); |
|
40 |
|
41 function arrayExists(array, x) { |
|
42 for (var i = 0; i < array.length; i++) { |
|
43 if (array[i] == x) return true; |
|
44 } |
|
45 return false; |
|
46 } |
|
47 |
|
48 Date.prototype.formatDate = function (input,time) { |
|
49 // formatDate : |
|
50 // a PHP date like function, for formatting date strings |
|
51 // See: http://www.php.net/date |
|
52 // |
|
53 // input : format string |
|
54 // time : epoch time (seconds, and optional) |
|
55 // |
|
56 // if time is not passed, formatting is based on |
|
57 // the current "this" date object's set time. |
|
58 // |
|
59 // supported: |
|
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 |
|
62 // |
|
63 // unsupported: |
|
64 // I (capital i), T, Z |
|
65 |
|
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", |
|
72 "Thu", "Fri", "Sat"]; |
|
73 var monthsShort = ["Jan", "Feb", "Mar", "Apr", |
|
74 "May", "Jun", "Jul", "Aug", "Sep", |
|
75 "Oct", "Nov", "Dec"]; |
|
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 |
|
84 |
|
85 function a() { |
|
86 // Lowercase Ante meridiem and Post meridiem |
|
87 return self.getHours() > 11? "pm" : "am"; |
|
88 } |
|
89 function A() { |
|
90 // Uppercase Ante meridiem and Post meridiem |
|
91 return self.getHours() > 11? "PM" : "AM"; |
|
92 } |
|
93 |
|
94 function B(){ |
|
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; |
|
107 return beat; |
|
108 } |
|
109 |
|
110 function d() { |
|
111 // Day of the month, 2 digits with leading zeros |
|
112 return new String(self.getDate()).length == 1? |
|
113 "0"+self.getDate() : self.getDate(); |
|
114 } |
|
115 function D() { |
|
116 // A textual representation of a day, three letters |
|
117 return daysShort[self.getDay()]; |
|
118 } |
|
119 function F() { |
|
120 // A full textual representation of a month |
|
121 return monthsLong[self.getMonth()]; |
|
122 } |
|
123 function g() { |
|
124 // 12-hour format of an hour without leading zeros |
|
125 return self.getHours() > 12? self.getHours()-12 : self.getHours(); |
|
126 } |
|
127 function G() { |
|
128 // 24-hour format of an hour without leading zeros |
|
129 return self.getHours(); |
|
130 } |
|
131 function h() { |
|
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; |
|
137 } else { |
|
138 var s = new String(self.getHours()); |
|
139 return s.length == 1? |
|
140 "0"+self.getHours() : self.getHours(); |
|
141 } |
|
142 } |
|
143 function H() { |
|
144 // 24-hour format of an hour with leading zeros |
|
145 return new String(self.getHours()).length == 1? |
|
146 "0"+self.getHours() : self.getHours(); |
|
147 } |
|
148 function i() { |
|
149 // Minutes with leading zeros |
|
150 return new String(self.getMinutes()).length == 1? |
|
151 "0"+self.getMinutes() : self.getMinutes(); |
|
152 } |
|
153 function j() { |
|
154 // Day of the month without leading zeros |
|
155 return self.getDate(); |
|
156 } |
|
157 function l() { |
|
158 // A full textual representation of the day of the week |
|
159 return daysLong[self.getDay()]; |
|
160 } |
|
161 function L() { |
|
162 // leap year or not. 1 if leap year, 0 if not. |
|
163 // the logic should match iso's 8601 standard. |
|
164 var y_ = Y(); |
|
165 if ( |
|
166 (y_ % 4 == 0 && y_ % 100 != 0) || |
|
167 (y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0) |
|
168 ) { |
|
169 return 1; |
|
170 } else { |
|
171 return 0; |
|
172 } |
|
173 } |
|
174 function m() { |
|
175 // Numeric representation of a month, with leading zeros |
|
176 return self.getMonth() < 9? |
|
177 "0"+(self.getMonth()+1) : |
|
178 self.getMonth()+1; |
|
179 } |
|
180 function M() { |
|
181 // A short textual representation of a month, three letters |
|
182 return monthsShort[self.getMonth()]; |
|
183 } |
|
184 function n() { |
|
185 // Numeric representation of a month, without leading zeros |
|
186 return self.getMonth()+1; |
|
187 } |
|
188 function O() { |
|
189 // Difference to Greenwich time (GMT) in hours |
|
190 var os = Math.abs(self.getTimezoneOffset()); |
|
191 var h = ""+Math.floor(os/60); |
|
192 var m = ""+(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; |
|
196 } |
|
197 function r() { |
|
198 // RFC 822 formatted date |
|
199 var r; // result |
|
200 // Thu , 21 Dec 2000 |
|
201 r = D() + ", " + j() + " " + M() + " " + Y() + |
|
202 // 16 : 01 : 07 +0200 |
|
203 " " + H() + ":" + i() + ":" + s() + " " + O(); |
|
204 return r; |
|
205 } |
|
206 function S() { |
|
207 // English ordinal suffix for the day of the month, 2 characters |
|
208 return daysSuffix[self.getDate()-1]; |
|
209 } |
|
210 function s() { |
|
211 // Seconds, with leading zeros |
|
212 return new String(self.getSeconds()).length == 1? |
|
213 "0"+self.getSeconds() : self.getSeconds(); |
|
214 } |
|
215 function t() { |
|
216 |
|
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()]; |
|
221 } |
|
222 function U() { |
|
223 // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) |
|
224 return Math.round(self.getTime()/1000); |
|
225 } |
|
226 function W() { |
|
227 // Weeknumber, as per ISO specification: |
|
228 // http://www.cl.cam.ac.uk/~mgk25/iso-time.html |
|
229 |
|
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(); |
|
234 var afterNY = z(); |
|
235 var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6. |
|
236 if (beforeNY <= 2 && weekday <= 2-beforeNY) { |
|
237 return 1; |
|
238 } |
|
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; |
|
243 if ( |
|
244 (afterNY <= 2) && |
|
245 (nyDay >=4) && |
|
246 (afterNY >= (6-nyDay)) |
|
247 ) { |
|
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 |
|
251 // return that week. |
|
252 var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00"); |
|
253 return prevNY.formatDate("W"); |
|
254 } |
|
255 |
|
256 // week 1, is the week that has the first thursday in it. |
|
257 // note that this value is not zero index. |
|
258 if (nyDay <= 3) { |
|
259 // first day of the year fell on a thursday, or earlier. |
|
260 return 1 + Math.floor( ( z() + nyDay ) / 7 ); |
|
261 } else { |
|
262 // first day of the year fell on a friday, or later. |
|
263 return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 ); |
|
264 } |
|
265 } |
|
266 function w() { |
|
267 // Numeric representation of the day of the week |
|
268 return self.getDay(); |
|
269 } |
|
270 |
|
271 function Y() { |
|
272 // A full numeric representation of a year, 4 digits |
|
273 |
|
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(); |
|
280 if (x == 2001) { |
|
281 // i trust the method now |
|
282 return self.getFullYear(); |
|
283 } |
|
284 } |
|
285 // else, do this: |
|
286 // codes thanks to ppk: |
|
287 // http://www.xs4all.nl/~ppk/js/introdate.html |
|
288 var x = self.getYear(); |
|
289 var y = x % 100; |
|
290 y += (y < 38) ? 2000 : 1900; |
|
291 return y; |
|
292 } |
|
293 function y() { |
|
294 // A two-digit representation of a year |
|
295 var y = Y()+""; |
|
296 return y.substring(y.length-2,y.length); |
|
297 } |
|
298 function z() { |
|
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); |
|
303 } |
|
304 |
|
305 var self = this; |
|
306 if (time) { |
|
307 // save time |
|
308 var prevTime = self.getTime(); |
|
309 self.setTime(time); |
|
310 } |
|
311 |
|
312 var ia = input.split(""); |
|
313 var ij = 0; |
|
314 while (ia[ij]) { |
|
315 if (ia[ij] == "\\") { |
|
316 // this is our way of allowing users to escape stuff |
|
317 ia.splice(ij,1); |
|
318 } else { |
|
319 if (arrayExists(switches,ia[ij])) { |
|
320 ia[ij] = eval(ia[ij] + "()"); |
|
321 } |
|
322 } |
|
323 ij++; |
|
324 } |
|
325 // reset time, back to what it was |
|
326 if (prevTime) { |
|
327 self.setTime(prevTime); |
|
328 } |
|
329 return ia.join(""); |
|
330 } |
|
331 |
|
332 var date = new Date("1/1/2007 1:11:11"); |
|
333 |
|
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); |
|
338 } |
|
339 |
|
340 |
|
341 |
|
342 var _sunSpiderInterval = new Date() - _sunSpiderStartDate; |
|
343 |
|
344 document.getElementById("console").innerHTML = _sunSpiderInterval; |
|
345 </script> |
|
346 |
|
347 |
|
348 </body> |
|
349 </html> |