PageRenderTime 45ms CodeModel.GetById 19ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/js/src/metrics/jint/sunspider/date-format-tofte.js

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