- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / dromaeo / tests / sunspider-date-format-tofte.html
1 <html>
2 <head>
3 <script src="../htmlrunner.js"></script>
4 <script>
5 function arrayExists(array, x) {
6     for (var i = 0; i < array.length; i++) {
7         if (array[i] == x) return true;
8     }
9     return false;
10 }
11
12 Date.prototype.formatDate = function (input,time) {
13     // formatDate :
14     // a PHP date like function, for formatting date strings
15     // See: http://www.php.net/date
16     //
17     // input : format string
18     // time : epoch time (seconds, and optional)
19     //
20     // if time is not passed, formatting is based on 
21     // the current "this" date object's set time.
22     //
23     // supported:
24     // a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L, 
25     // m, M, n, O, r, s, S, t, U, w, W, y, Y, z
26     //
27     // unsupported:
28     // I (capital i), T, Z    
29
30     var switches =    ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H", 
31                        "i", "j", "l", "L", "m", "M", "n", "O", "r", "s", 
32                        "S", "t", "U", "w", "W", "y", "Y", "z"];
33     var daysLong =    ["Sunday", "Monday", "Tuesday", "Wednesday", 
34                        "Thursday", "Friday", "Saturday"];
35     var daysShort =   ["Sun", "Mon", "Tue", "Wed", 
36                        "Thu", "Fri", "Sat"];
37     var monthsShort = ["Jan", "Feb", "Mar", "Apr",
38                        "May", "Jun", "Jul", "Aug", "Sep",
39                        "Oct", "Nov", "Dec"];
40     var monthsLong =  ["January", "February", "March", "April",
41                        "May", "June", "July", "August", "September",
42                        "October", "November", "December"];
43     var daysSuffix = ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th
44                       "th", "th", "th", "th", "th", "th", "th", // 8th - 14th
45                       "th", "th", "th", "th", "th", "th", "st", // 15th - 21st
46                       "nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th
47                       "th", "th", "st"];                        // 29th - 31st
48
49     function a() {
50         // Lowercase Ante meridiem and Post meridiem
51         return self.getHours() > 11? "pm" : "am";
52     }
53     function A() {
54         // Uppercase Ante meridiem and Post meridiem
55         return self.getHours() > 11? "PM" : "AM";
56     }
57
58     function B(){
59         // Swatch internet time. code simply grabbed from ppk,
60         // since I was feeling lazy:
61         // http://www.xs4all.nl/~ppk/js/beat.html
62         var off = (self.getTimezoneOffset() + 60)*60;
63         var theSeconds = (self.getHours() * 3600) + 
64                          (self.getMinutes() * 60) + 
65                           self.getSeconds() + off;
66         var beat = Math.floor(theSeconds/86.4);
67         if (beat > 1000) beat -= 1000;
68         if (beat < 0) beat += 1000;
69         if ((""+beat).length == 1) beat = "00"+beat;
70         if ((""+beat).length == 2) beat = "0"+beat;
71         return beat;
72     }
73     
74     function d() {
75         // Day of the month, 2 digits with leading zeros
76         return new String(self.getDate()).length == 1?
77         "0"+self.getDate() : self.getDate();
78     }
79     function D() {
80         // A textual representation of a day, three letters
81         return daysShort[self.getDay()];
82     }
83     function F() {
84         // A full textual representation of a month
85         return monthsLong[self.getMonth()];
86     }
87     function g() {
88         // 12-hour format of an hour without leading zeros
89         return self.getHours() > 12? self.getHours()-12 : self.getHours();
90     }
91     function G() {
92         // 24-hour format of an hour without leading zeros
93         return self.getHours();
94     }
95     function h() {
96         // 12-hour format of an hour with leading zeros
97         if (self.getHours() > 12) {
98           var s = new String(self.getHours()-12);
99           return s.length == 1?
100           "0"+ (self.getHours()-12) : self.getHours()-12;
101         } else { 
102           var s = new String(self.getHours());
103           return s.length == 1?
104           "0"+self.getHours() : self.getHours();
105         }  
106     }
107     function H() {
108         // 24-hour format of an hour with leading zeros
109         return new String(self.getHours()).length == 1?
110         "0"+self.getHours() : self.getHours();
111     }
112     function i() {
113         // Minutes with leading zeros
114         return new String(self.getMinutes()).length == 1? 
115         "0"+self.getMinutes() : self.getMinutes(); 
116     }
117     function j() {
118         // Day of the month without leading zeros
119         return self.getDate();
120     }    
121     function l() {
122         // A full textual representation of the day of the week
123         return daysLong[self.getDay()];
124     }
125     function L() {
126         // leap year or not. 1 if leap year, 0 if not.
127         // the logic should match iso's 8601 standard.
128         var y_ = Y();
129         if (         
130             (y_ % 4 == 0 && y_ % 100 != 0) ||
131             (y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0)
132             ) {
133             return 1;
134         } else {
135             return 0;
136         }
137     }
138     function m() {
139         // Numeric representation of a month, with leading zeros
140         return self.getMonth() < 9?
141         "0"+(self.getMonth()+1) : 
142         self.getMonth()+1;
143     }
144     function M() {
145         // A short textual representation of a month, three letters
146         return monthsShort[self.getMonth()];
147     }
148     function n() {
149         // Numeric representation of a month, without leading zeros
150         return self.getMonth()+1;
151     }
152     function O() {
153         // Difference to Greenwich time (GMT) in hours
154         var os = Math.abs(self.getTimezoneOffset());
155         var h = ""+Math.floor(os/60);
156         var m = ""+(os%60);
157         h.length == 1? h = "0"+h:1;
158         m.length == 1? m = "0"+m:1;
159         return self.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
160     }
161     function r() {
162         // RFC 822 formatted date
163         var r; // result
164         //  Thu    ,     21          Dec         2000
165         r = D() + ", " + j() + " " + M() + " " + Y() +
166         //        16     :    01     :    07          +0200
167             " " + H() + ":" + i() + ":" + s() + " " + O();
168         return r;
169     }
170     function S() {
171         // English ordinal suffix for the day of the month, 2 characters
172         return daysSuffix[self.getDate()-1];
173     }
174     function s() {
175         // Seconds, with leading zeros
176         return new String(self.getSeconds()).length == 1?
177         "0"+self.getSeconds() : self.getSeconds();
178     }
179     function t() {
180
181         // thanks to Matt Bannon for some much needed code-fixes here!
182         var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31];
183         if (L()==1 && n()==2) return 29; // leap day
184         return daysinmonths[n()];
185     }
186     function U() {
187         // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
188         return Math.round(self.getTime()/1000);
189     }
190     function W() {
191         // Weeknumber, as per ISO specification:
192         // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
193         
194         // if the day is three days before newyears eve,
195         // there's a chance it's "week 1" of next year.
196         // here we check for that.
197         var beforeNY = 364+L() - z();
198         var afterNY  = z();
199         var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6.
200         if (beforeNY <= 2 && weekday <= 2-beforeNY) {
201             return 1;
202         }
203         // similarly, if the day is within threedays of newyears
204         // there's a chance it belongs in the old year.
205         var ny = new Date("January 1 " + Y() + " 00:00:00");
206         var nyDay = ny.getDay()!=0?ny.getDay()-1:6;
207         if (
208             (afterNY <= 2) && 
209             (nyDay >=4)  && 
210             (afterNY >= (6-nyDay))
211             ) {
212             // Since I'm not sure we can just always return 53,
213             // i call the function here again, using the last day
214             // of the previous year, as the date, and then just
215             // return that week.
216             var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00");
217             return prevNY.formatDate("W");
218         }
219         
220         // week 1, is the week that has the first thursday in it.
221         // note that this value is not zero index.
222         if (nyDay <= 3) {
223             // first day of the year fell on a thursday, or earlier.
224             return 1 + Math.floor( ( z() + nyDay ) / 7 );
225         } else {
226             // first day of the year fell on a friday, or later.
227             return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 );
228         }
229     }
230     function w() {
231         // Numeric representation of the day of the week
232         return self.getDay();
233     }
234     
235     function Y() {
236         // A full numeric representation of a year, 4 digits
237
238         // we first check, if getFullYear is supported. if it
239         // is, we just use that. ppks code is nice, but wont
240         // work with dates outside 1900-2038, or something like that
241         if (self.getFullYear) {
242             var newDate = new Date("January 1 2001 00:00:00 +0000");
243             var x = newDate .getFullYear();
244             if (x == 2001) {              
245                 // i trust the method now
246                 return self.getFullYear();
247             }
248         }
249         // else, do this:
250         // codes thanks to ppk:
251         // http://www.xs4all.nl/~ppk/js/introdate.html
252         var x = self.getYear();
253         var y = x % 100;
254         y += (y < 38) ? 2000 : 1900;
255         return y;
256     }
257     function y() {
258         // A two-digit representation of a year
259         var y = Y()+"";
260         return y.substring(y.length-2,y.length);
261     }
262     function z() {
263         // The day of the year, zero indexed! 0 through 366
264         var t = new Date("January 1 " + Y() + " 00:00:00");
265         var diff = self.getTime() - t.getTime();
266         return Math.floor(diff/1000/60/60/24);
267     }
268         
269     var self = this;
270     if (time) {
271         // save time
272         var prevTime = self.getTime();
273         self.setTime(time);
274     }
275     
276     var ia = input.split("");
277     var ij = 0;
278     while (ia[ij]) {
279         if (ia[ij] == "\\") {
280             // this is our way of allowing users to escape stuff
281             ia.splice(ij,1);
282         } else {
283             if (arrayExists(switches,ia[ij])) {
284                 ia[ij] = eval(ia[ij] + "()");
285             }
286         }
287         ij++;
288     }
289     // reset time, back to what it was
290     if (prevTime) {
291         self.setTime(prevTime);
292     }
293     return ia.join("");
294 }
295
296 var date = new Date("1/1/2007 1:11:11");
297
298 window.onload = function(){ startTest("sunspider-date-format-tofte", '393fb742');
299
300 test("Format Date", function(){
301         for (var i = 0; i < 50; ++i) {
302                 var shortFormat = date.formatDate("Y-m-d");
303                 var longFormat = date.formatDate("l, F d, Y g:i:s A");
304                 date.setTime(date.getTime() + 84266956);
305         }
306 });
307
308 endTest(); };
309 </script>
310 </head>
311 <body></body>
312 </html>