JsTemplate/Link.js
[gnome.introspection-doc-generator] / Date.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12 //imports['Date.js'].load(Date);
13 if (imports) {
14     imports['String.js'].load(String);
15
16 }
17
18 if (imports) {
19     load = false; // declare global for gnome langs.
20 }
21 (function() {
22  
23
24
25 /**
26  * @class Date
27  *
28  * The date parsing and format syntax is a subset of
29  * <a href="http://www.php.net/date">PHP's date() function</a>, and the formats that are
30  * supported will provide results equivalent to their PHP versions.
31  *
32  * Following is the list of all currently supported formats:
33  *<pre>
34 Sample date:
35 'Wed Jan 10 2007 15:05:01 GMT-0600 (Central Standard Time)'
36
37 Format  Output      Description
38 ------  ----------  --------------------------------------------------------------
39   d      10         Day of the month, 2 digits with leading zeros
40   D      Wed        A textual representation of a day, three letters
41   j      10         Day of the month without leading zeros
42   l      Wednesday  A full textual representation of the day of the week
43   S      th         English ordinal day of month suffix, 2 chars (use with j)
44   w      3          Numeric representation of the day of the week
45   z      9          The julian date, or day of the year (0-365)
46   W      01         ISO-8601 2-digit week number of year, weeks starting on Monday (00-52)
47   F      January    A full textual representation of the month
48   m      01         Numeric representation of a month, with leading zeros
49   M      Jan        Month name abbreviation, three letters
50   n      1          Numeric representation of a month, without leading zeros
51   t      31         Number of days in the given month
52   L      0          Whether it's a leap year (1 if it is a leap year, else 0)
53   Y      2007       A full numeric representation of a year, 4 digits
54   y      07         A two digit representation of a year
55   a      pm         Lowercase Ante meridiem and Post meridiem
56   A      PM         Uppercase Ante meridiem and Post meridiem
57   g      3          12-hour format of an hour without leading zeros
58   G      15         24-hour format of an hour without leading zeros
59   h      03         12-hour format of an hour with leading zeros
60   H      15         24-hour format of an hour with leading zeros
61   i      05         Minutes with leading zeros
62   s      01         Seconds, with leading zeros
63   O      -0600      Difference to Greenwich time (GMT) in hours
64   T      CST        Timezone setting of the machine running the code
65   Z      -21600     Timezone offset in seconds (negative if west of UTC, positive if east)
66 </pre>
67  *
68  * Example usage (note that you must escape format specifiers with '\\' to render them as character literals):
69  * <pre><code>
70 var dt = new Date('1/10/2007 03:05:01 PM GMT-0600');
71 document.write(dt.format('Y-m-d'));                         //2007-01-10
72 document.write(dt.format('F j, Y, g:i a'));                 //January 10, 2007, 3:05 pm
73 document.write(dt.format('l, \\t\\he dS of F Y h:i:s A'));  //Wednesday, the 10th of January 2007 03:05:01 PM
74  </code></pre>
75  *
76  * Here are some standard date/time patterns that you might find helpful.  They
77  * are not part of the source of Date.js, but to use them you can simply copy this
78  * block of code into any script that is included after Date.js and they will also become
79  * globally available on the Date object.  Feel free to add or remove patterns as needed in your code.
80  * <pre><code>
81 Date.patterns = {
82     ISO8601Long:"Y-m-d H:i:s",
83     ISO8601Short:"Y-m-d",
84     ShortDate: "n/j/Y",
85     LongDate: "l, F d, Y",
86     FullDateTime: "l, F d, Y g:i:s A",
87     MonthDay: "F d",
88     ShortTime: "g:i A",
89     LongTime: "g:i:s A",
90     SortableDateTime: "Y-m-d\\TH:i:s",
91     UniversalSortableDateTime: "Y-m-d H:i:sO",
92     YearMonth: "F, Y"
93 };
94 </code></pre>
95  *
96  * Example usage:
97  * <pre><code>
98 var dt = new Date();
99 document.write(dt.format(Date.patterns.ShortDate));
100  </code></pre>
101  */
102
103 /*
104  * Most of the date-formatting functions below are the excellent work of Baron Schwartz.
105  * They generate precompiled functions from date formats instead of parsing and
106  * processing the pattern every time you format a date.  These functions are available
107  * on every Date object (any javascript function).
108  *
109  * The original article and download are here:
110  * http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency/
111  *
112  */
113  
114      
115      // was in core
116     /**
117      Returns the number of milliseconds between this date and date
118      @param {Date} date (optional) Defaults to now
119      @return {Number} The diff in milliseconds
120      @member Date getElapsed
121      */
122     var date = {
123         
124         // private
125         parseFunctions : {count:0},
126         // private
127         parseRegexes : [],
128         // private
129         formatFunctions : {count:0},
130             
131             
132         // private
133         // safari setMonth is broken
134          
135         /** Date interval constant 
136         * @static 
137         * @type String */
138         MILLI : "ms",
139         /** Date interval constant 
140         * @static 
141         * @type String */
142         SECOND : "s",
143         /** Date interval constant 
144         * @static 
145         * @type String */
146         MINUTE : "mi",
147         /** Date interval constant 
148         * @static 
149         * @type String */
150         HOUR : "h",
151         /** Date interval constant 
152         * @static 
153         * @type String */
154         DAY : "d",
155         /** Date interval constant 
156         * @static 
157         * @type String */
158         MONTH : "mo",
159         /** Date interval constant 
160         * @static 
161         * @type String */
162         YEAR : "y",
163
164         // private
165         daysInMonth : [31,28,31,30,31,30,31,31,30,31,30,31],
166
167         /**
168          * An array of textual month names.
169          * Override these values for international dates, for example...
170          * Date.monthNames = ['JanInYourLang', 'FebInYourLang', ...];
171          * @type Array
172          * @static
173          */
174         monthNames : [
175             "January",
176             "February",
177             "March",
178             "April",
179             "May",
180             "June",
181             "July",
182             "August",
183             "September",
184             "October",
185             "November",
186             "December"],
187
188         /**
189          * An array of textual day names.
190          * Override these values for international dates, for example...
191          * Date.dayNames = ['SundayInYourLang', 'MondayInYourLang', ...];
192          * @type Array
193          * @static
194          */
195         dayNames : [
196             "Sunday",
197             "Monday",
198             "Tuesday",
199             "Wednesday",
200             "Thursday",
201             "Friday",
202             "Saturday"],
203
204         // private
205         y2kYear : 50,
206         // private
207         monthNumbers : {
208             Jan:0,
209             Feb:1,
210             Mar:2,
211             Apr:3,
212             May:4,
213             Jun:5,
214             Jul:6,
215             Aug:7,
216             Sep:8,
217             Oct:9,
218             Nov:10,
219             Dec:11},
220         
221         createNewFormat : function(format) {
222             var funcName = "format" + Date.formatFunctions.count++;
223             Date.formatFunctions[format] = funcName;
224             var code = "Date.prototype." + funcName + " = function(){return ";
225             var special = false;
226             var ch = '';
227             for (var i = 0; i < format.length; ++i) {
228                 ch = format.charAt(i);
229                 if (!special && ch == "\\") {
230                     special = true;
231                 }
232                 else if (special) {
233                     special = false;
234                     code += "'" + String.escape(ch) + "' + ";
235                 }
236                 else {
237                     code += Date.getFormatCode(ch);
238                 }
239             }
240             /** eval:var:zzzzzzzzzzzzz */
241             eval(code.substring(0, code.length - 3) + ";}");
242         },
243
244         // private
245         getFormatCode : function(character) {
246             switch (character) {
247             case "d":
248                 return "String.leftPad(this.getDate(), 2, '0') + ";
249             case "D":
250                 return "Date.dayNames[this.getDay()].substring(0, 3) + ";
251             case "j":
252                 return "this.getDate() + ";
253             case "l":
254                 return "Date.dayNames[this.getDay()] + ";
255             case "S":
256                 return "this.getSuffix() + ";
257             case "w":
258                 return "this.getDay() + ";
259             case "z":
260                 return "this.getDayOfYear() + ";
261             case "W":
262                 return "this.getWeekOfYear() + ";
263             case "F":
264                 return "Date.monthNames[this.getMonth()] + ";
265             case "m":
266                 return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
267             case "M":
268                 return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
269             case "n":
270                 return "(this.getMonth() + 1) + ";
271             case "t":
272                 return "this.getDaysInMonth() + ";
273             case "L":
274                 return "(this.isLeapYear() ? 1 : 0) + ";
275             case "Y":
276                 return "this.getFullYear() + ";
277             case "y":
278                 return "('' + this.getFullYear()).substring(2, 4) + ";
279             case "a":
280                 return "(this.getHours() < 12 ? 'am' : 'pm') + ";
281             case "A":
282                 return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
283             case "g":
284                 return "((this.getHours() % 12) ? this.getHours() % 12 : 12) + ";
285             case "G":
286                 return "this.getHours() + ";
287             case "h":
288                 return "String.leftPad((this.getHours() % 12) ? this.getHours() % 12 : 12, 2, '0') + ";
289             case "H":
290                 return "String.leftPad(this.getHours(), 2, '0') + ";
291             case "i":
292                 return "String.leftPad(this.getMinutes(), 2, '0') + ";
293             case "s":
294                 return "String.leftPad(this.getSeconds(), 2, '0') + ";
295             case "O":
296                 return "this.getGMTOffset() + ";
297             case "T":
298                 return "this.getTimezone() + ";
299             case "Z":
300                 return "(this.getTimezoneOffset() * -60) + ";
301             default:
302                 return "'" + String.escape(character) + "' + ";
303             }
304         },
305
306         /**
307          * Parses the passed string using the specified format. Note that this function expects dates in normal calendar
308          * format, meaning that months are 1-based (1 = January) and not zero-based like in JavaScript dates.  Any part of
309          * the date format that is not specified will default to the current date value for that part.  Time parts can also
310          * be specified, but default to 0.  Keep in mind that the input date string must precisely match the specified format
311          * string or the parse operation will fail.
312          * Example Usage:
313         <pre><code>
314         //dt = Fri May 25 2007 (current date)
315         var dt = new Date();
316
317         //dt = Thu May 25 2006 (today's month/day in 2006)
318         dt = Date.parseDate("2006", "Y");
319
320         //dt = Sun Jan 15 2006 (all date parts specified)
321         dt = Date.parseDate("2006-1-15", "Y-m-d");
322
323         //dt = Sun Jan 15 2006 15:20:01 GMT-0600 (CST)
324         dt = Date.parseDate("2006-1-15 3:20:01 PM", "Y-m-d h:i:s A" );
325         </code></pre>
326          * @param {String} input The unparsed date as a string
327          * @param {String} format The format the date is in
328          * @return {Date} The parsed date
329          * @static
330          */
331         parseDate : function(input, format) {
332             if (Date.parseFunctions[format] == null) {
333                 Date.createParser(format);
334             }
335             var func = Date.parseFunctions[format];
336             return Date[func](input);
337         },
338
339         // private
340         createParser : function(format) {
341             var funcName = "parse" + Date.parseFunctions.count++;
342             var regexNum = Date.parseRegexes.length;
343             var currentGroup = 1;
344             Date.parseFunctions[format] = funcName;
345
346             var code = "Date." + funcName + " = function(input){\n"
347                 + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1, o, z, v;\n"
348                 + "var d = new Date();\n"
349                 + "y = d.getFullYear();\n"
350                 + "m = d.getMonth();\n"
351                 + "d = d.getDate();\n"
352                 + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
353                 + "if (results && results.length > 0) {";
354             var regex = "";
355
356             var special = false;
357             var ch = '';
358             for (var i = 0; i < format.length; ++i) {
359                 ch = format.charAt(i);
360                 if (!special && ch == "\\") {
361                     special = true;
362                 }
363                 else if (special) {
364                     special = false;
365                     regex += String.escape(ch);
366                 }
367                 else {
368                     var obj = Date.formatCodeToRegex(ch, currentGroup);
369                     currentGroup += obj.g;
370                     regex += obj.s;
371                     if (obj.g && obj.c) {
372                         code += obj.c;
373                     }
374                 }
375             }
376
377             code += "if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
378                 + "{v = new Date(y, m, d, h, i, s);}\n"
379                 + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
380                 + "{v = new Date(y, m, d, h, i);}\n"
381                 + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0)\n"
382                 + "{v = new Date(y, m, d, h);}\n"
383                 + "else if (y >= 0 && m >= 0 && d > 0)\n"
384                 + "{v = new Date(y, m, d);}\n"
385                 + "else if (y >= 0 && m >= 0)\n"
386                 + "{v = new Date(y, m);}\n"
387                 + "else if (y >= 0)\n"
388                 + "{v = new Date(y);}\n"
389                 + "}return (v && (z || o))?\n" // favour UTC offset over GMT offset
390                 + "    ((z)? v.add(Date.SECOND, (v.getTimezoneOffset() * 60) + (z*1)) :\n" // reset to UTC, then add offset
391                 + "        v.add(Date.HOUR, (v.getGMTOffset() / 100) + (o / -100))) : v\n" // reset to GMT, then add offset
392                 + ";}";
393
394             Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$");
395              /** eval:var:zzzzzzzzzzzzz */
396             eval(code);
397         },
398
399         // private
400         formatCodeToRegex : function(character, currentGroup) {
401             switch (character) {
402             case "D":
403                 return {g:0,
404                 c:null,
405                 s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
406             case "j":
407                 return {g:1,
408                     c:"d = parseInt(results[" + currentGroup + "], 10);\n",
409                     s:"(\\d{1,2})"}; // day of month without leading zeroes
410             case "d":
411                 return {g:1,
412                     c:"d = parseInt(results[" + currentGroup + "], 10);\n",
413                     s:"(\\d{2})"}; // day of month with leading zeroes
414             case "l":
415                 return {g:0,
416                     c:null,
417                     s:"(?:" + Date.dayNames.join("|") + ")"};
418             case "S":
419                 return {g:0,
420                     c:null,
421                     s:"(?:st|nd|rd|th)"};
422             case "w":
423                 return {g:0,
424                     c:null,
425                     s:"\\d"};
426             case "z":
427                 return {g:0,
428                     c:null,
429                     s:"(?:\\d{1,3})"};
430             case "W":
431                 return {g:0,
432                     c:null,
433                     s:"(?:\\d{2})"};
434             case "F":
435                 return {g:1,
436                     c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].substring(0, 3)], 10);\n",
437                     s:"(" + Date.monthNames.join("|") + ")"};
438             case "M":
439                 return {g:1,
440                     c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10);\n",
441                     s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
442             case "n":
443                 return {g:1,
444                     c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n",
445                     s:"(\\d{1,2})"}; // Numeric representation of a month, without leading zeros
446             case "m":
447                 return {g:1,
448                     c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n",
449                     s:"(\\d{2})"}; // Numeric representation of a month, with leading zeros
450             case "t":
451                 return {g:0,
452                     c:null,
453                     s:"\\d{1,2}"};
454             case "L":
455                 return {g:0,
456                     c:null,
457                     s:"(?:1|0)"};
458             case "Y":
459                 return {g:1,
460                     c:"y = parseInt(results[" + currentGroup + "], 10);\n",
461                     s:"(\\d{4})"};
462             case "y":
463                 return {g:1,
464                     c:"var ty = parseInt(results[" + currentGroup + "], 10);\n"
465                         + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
466                     s:"(\\d{1,2})"};
467             case "a":
468                 return {g:1,
469                     c:"if (results[" + currentGroup + "] == 'am') {\n"
470                         + "if (h == 12) { h = 0; }\n"
471                         + "} else { if (h < 12) { h += 12; }}",
472                     s:"(am|pm)"};
473             case "A":
474                 return {g:1,
475                     c:"if (results[" + currentGroup + "] == 'AM') {\n"
476                         + "if (h == 12) { h = 0; }\n"
477                         + "} else { if (h < 12) { h += 12; }}",
478                     s:"(AM|PM)"};
479             case "g":
480             case "G":
481                 return {g:1,
482                     c:"h = parseInt(results[" + currentGroup + "], 10);\n",
483                     s:"(\\d{1,2})"}; // 12/24-hr format  format of an hour without leading zeroes
484             case "h":
485             case "H":
486                 return {g:1,
487                     c:"h = parseInt(results[" + currentGroup + "], 10);\n",
488                     s:"(\\d{2})"}; //  12/24-hr format  format of an hour with leading zeroes
489             case "i":
490                 return {g:1,
491                     c:"i = parseInt(results[" + currentGroup + "], 10);\n",
492                     s:"(\\d{2})"};
493             case "s":
494                 return {g:1,
495                     c:"s = parseInt(results[" + currentGroup + "], 10);\n",
496                     s:"(\\d{2})"};
497             case "O":
498                 return {g:1,
499                     c:[
500                         "o = results[", currentGroup, "];\n",
501                         "var sn = o.substring(0,1);\n", // get + / - sign
502                         "var hr = o.substring(1,3)*1 + Math.floor(o.substring(3,5) / 60);\n", // get hours (performs minutes-to-hour conversion also)
503                         "var mn = o.substring(3,5) % 60;\n", // get minutes
504                         "o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))?\n", // -12hrs <= GMT offset <= 14hrs
505                         "    (sn + String.leftPad(hr, 2, 0) + String.leftPad(mn, 2, 0)) : null;\n"
506                     ].join(""),
507                     s:"([+\-]\\d{4})"};
508             case "T":
509                 return {g:0,
510                     c:null,
511                     s:"[A-Z]{1,4}"}; // timezone abbrev. may be between 1 - 4 chars
512             case "Z":
513                 return {g:1,
514                     c:"z = results[" + currentGroup + "];\n" // -43200 <= UTC offset <= 50400
515                           + "z = (-43200 <= z*1 && z*1 <= 50400)? z : null;\n",
516                     s:"([+\-]?\\d{1,5})"}; // leading '+' sign is optional for UTC offset
517             default:
518             
519                 return {g:0,
520                     c:null,
521                     s:String.escape(character)};
522             }
523         }
524
525         
526     };
527
528     var datePrototype = {
529
530         getElapsed : function(date) {
531             return Math.abs((date || new Date()).getTime()-this.getTime());
532         },
533          
534
535         /**
536          * Formats a date given the supplied format string
537          * @param {String} format The format string
538          * @return {String} The formatted date
539          * @method
540          */
541
542         
543         format : function(format) {
544             if (Date.formatFunctions[format] == null) {
545                 Date.createNewFormat(format);
546             }
547             var func = Date.formatFunctions[format];
548             return this[func]();
549         },
550
551         // private - why is dataFormat an alias?
552         //format : dateFormat;
553
554         // private
555
556         /**
557          * Get the timezone abbreviation of the current date (equivalent to the format specifier 'T').
558          * @return {String} The abbreviated timezone name (e.g. 'CST')
559          */
560         getTimezone : function() {
561             return this.toString().replace(/^.*? ([A-Z]{1,4})[\-+][0-9]{4} .*$/, "$1");
562         },
563
564         /**
565          * Get the offset from GMT of the current date (equivalent to the format specifier 'O').
566          * @return {String} The 4-character offset string prefixed with + or - (e.g. '-0600')
567          */
568         getGMTOffset : function() {
569             return (this.getTimezoneOffset() > 0 ? "-" : "+")
570                 + String.leftPad(Math.abs(Math.floor(this.getTimezoneOffset() / 60)), 2, "0")
571                 + String.leftPad(this.getTimezoneOffset() % 60, 2, "0");
572         },
573
574         /**
575          * Get the numeric day number of the year, adjusted for leap year.
576          * @return {Number} 0 through 364 (365 in leap years)
577          */
578         getDayOfYear : function() {
579             var num = 0;
580             Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
581             for (var i = 0; i < this.getMonth(); ++i) {
582                 num += Date.daysInMonth[i];
583             }
584             return num + this.getDate() - 1;
585         },
586
587         /**
588          * Get the string representation of the numeric week number of the year
589          * (equivalent to the format specifier 'W').
590          * @return {String} '00' through '52'
591          */
592         getWeekOfYear : function() {
593             // Skip to Thursday of this week
594             var now = this.getDayOfYear() + (4 - this.getDay());
595             // Find the first Thursday of the year
596             var jan1 = new Date(this.getFullYear(), 0, 1);
597             var then = (7 - jan1.getDay() + 4);
598             return String.leftPad(((now - then) / 7) + 1, 2, "0");
599         },
600
601         /**
602          * Whether or not the current date is in a leap year.
603          * @return {Boolean} True if the current date is in a leap year, else false
604          */
605         isLeapYear : function() {
606             var year = this.getFullYear();
607             return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
608         },
609
610         /**
611          * Get the first day of the current month, adjusted for leap year.  The returned value
612          * is the numeric day index within the week (0-6) which can be used in conjunction with
613          * the {@link #monthNames} array to retrieve the textual day name.
614          * Example:
615          *<pre><code>
616         var dt = new Date('1/10/2007');
617         document.write(Date.dayNames[dt.getFirstDayOfMonth()]); //output: 'Monday'
618         </code></pre>
619          * @return {Number} The day number (0-6)
620          */
621         getFirstDayOfMonth : function() {
622             var day = (this.getDay() - (this.getDate() - 1)) % 7;
623             return (day < 0) ? (day + 7) : day;
624         },
625
626         /**
627          * Get the last day of the current month, adjusted for leap year.  The returned value
628          * is the numeric day index within the week (0-6) which can be used in conjunction with
629          * the {@link #monthNames} array to retrieve the textual day name.
630          * Example:
631          *<pre><code>
632         var dt = new Date('1/10/2007');
633         document.write(Date.dayNames[dt.getLastDayOfMonth()]); //output: 'Wednesday'
634         </code></pre>
635          * @return {Number} The day number (0-6)
636          */
637         getLastDayOfMonth : function() {
638             var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate())) % 7;
639             return (day < 0) ? (day + 7) : day;
640         },
641
642
643         /**
644          * Get the first date of this date's month
645          * @return {Date}
646          */
647         getFirstDateOfMonth : function() {
648             return new Date(this.getFullYear(), this.getMonth(), 1);
649         },
650
651         /**
652          * Get the last date of this date's month
653          * @return {Date}
654          */
655         getLastDateOfMonth : function() {
656             return new Date(this.getFullYear(), this.getMonth(), this.getDaysInMonth());
657         },
658         /**
659          * Get the number of days in the current month, adjusted for leap year.
660          * @return {Number} The number of days in the month
661          */
662         getDaysInMonth : function() {
663             Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
664             return Date.daysInMonth[this.getMonth()];
665         },
666
667         /**
668          * Get the English ordinal suffix of the current day (equivalent to the format specifier 'S').
669          * @return {String} 'st, 'nd', 'rd' or 'th'
670          */
671         getSuffix : function() {
672             switch (this.getDate()) {
673                 case 1:
674                 case 21:
675                 case 31:
676                     return "st";
677                 case 2:
678                 case 22:
679                     return "nd";
680                 case 3:
681                 case 23:
682                     return "rd";
683                 default:
684                     return "th";
685             }
686         },
687
688
689         /**
690          * Creates and returns a new Date instance with the exact same date value as the called instance.
691          * Dates are copied and passed by reference, so if a copied date variable is modified later, the original
692          * variable will also be changed.  When the intention is to create a new variable that will not
693          * modify the original instance, you should create a clone.
694          *
695          * Example of correctly cloning a date:
696          * <pre><code>
697         //wrong way:
698         var orig = new Date('10/1/2006');
699         var copy = orig;
700         copy.setDate(5);
701         document.write(orig);  //returns 'Thu Oct 05 2006'!
702
703         //correct way:
704         var orig = new Date('10/1/2006');
705         var copy = orig.clone();
706         copy.setDate(5);
707         document.write(orig);  //returns 'Thu Oct 01 2006'
708         </code></pre>
709          * @return {Date} The new Date instance
710          */
711         clone : function() {
712             return new Date(this.getTime());
713         },
714
715         /**
716          * Clears any time information from this date
717          @param {Boolean} clone true to create a clone of this date, clear the time and return it
718          @return {Date} this or the clone
719          */
720         clearTime : function(clone){
721             if(clone){
722                 return this.clone().clearTime();
723             }
724             this.setHours(0);
725             this.setMinutes(0);
726             this.setSeconds(0);
727             this.setMilliseconds(0);
728             return this;
729         },
730
731         /**
732          * Provides a convenient method of performing basic date arithmetic.  This method
733          * does not modify the Date instance being called - it creates and returns
734          * a new Date instance containing the resulting date value.
735          *
736          * Examples:
737          * <pre><code>
738         //Basic usage:
739         var dt = new Date('10/29/2006').add(Date.DAY, 5);
740         document.write(dt); //returns 'Fri Oct 06 2006 00:00:00'
741
742         //Negative values will subtract correctly:
743         var dt2 = new Date('10/1/2006').add(Date.DAY, -5);
744         document.write(dt2); //returns 'Tue Sep 26 2006 00:00:00'
745
746         //You can even chain several calls together in one line!
747         var dt3 = new Date('10/1/2006').add(Date.DAY, 5).add(Date.HOUR, 8).add(Date.MINUTE, -30);
748         document.write(dt3); //returns 'Fri Oct 06 2006 07:30:00'
749          </code></pre>
750          *
751          * @param {String} interval   A valid date interval enum value
752          * @param {Number} value      The amount to add to the current date
753          * @return {Date} The new Date instance
754          */
755         add : function(interval, value){
756           var d = this.clone();
757           if (!interval || value === 0) return d;
758           switch(interval.toLowerCase()){
759             case Date.MILLI:
760               d.setMilliseconds(this.getMilliseconds() + value);
761               break;
762             case Date.SECOND:
763               d.setSeconds(this.getSeconds() + value);
764               break;
765             case Date.MINUTE:
766               d.setMinutes(this.getMinutes() + value);
767               break;
768             case Date.HOUR:
769               d.setHours(this.getHours() + value);
770               break;
771             case Date.DAY:
772               d.setDate(this.getDate() + value);
773               break;
774             case Date.MONTH:
775               var day = this.getDate();
776               if(day > 28){
777                   day = Math.min(day, this.getFirstDateOfMonth().add('mo', value).getLastDateOfMonth().getDate());
778               }
779               d.setDate(day);
780               d.setMonth(this.getMonth() + value);
781               break;
782             case Date.YEAR:
783               d.setFullYear(this.getFullYear() + value);
784               break;
785           }
786           return d;
787         }
788     };
789     if (imports) {
790         load = function(ar) {
791             Date = ar;
792             imports.lang.copyPropertiesNoOverwrite( date,ar);
793             imports.lang.copyPropertiesNoOverwrite(datePrototype, ar.prototype);
794             
795         };
796     } else {
797         // non imports version.
798         for(i in datePrototype) {
799             if (!Date.prototype[i]) {
800                 Date.prototype[i] = datePrototype[i];
801             }
802         }
803         for(i in date) {
804             if (!Date[i]) {
805                 Date[i] = date[i];
806             }
807         }
808     }
809 })();