]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/date.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojo / date.js
1 if(!dojo._hasResource["dojo.date"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.date"] = true;
3 dojo.provide("dojo.date");
4
5 /*=====
6 dojo.date = {
7         // summary: Date manipulation utilities
8 }
9 =====*/
10
11 dojo.date.getDaysInMonth = function(/*Date*/dateObject){
12         //      summary:
13         //              Returns the number of days in the month used by dateObject
14         var month = dateObject.getMonth();
15         var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
16         if(month == 1 && dojo.date.isLeapYear(dateObject)){ return 29; } // Number
17         return days[month]; // Number
18 }
19
20 dojo.date.isLeapYear = function(/*Date*/dateObject){
21         //      summary:
22         //              Determines if the year of the dateObject is a leap year
23         //      description:
24         //              Leap years are years with an additional day YYYY-02-29, where the
25         //              year number is a multiple of four with the following exception: If
26         //              a year is a multiple of 100, then it is only a leap year if it is
27         //              also a multiple of 400. For example, 1900 was not a leap year, but
28         //              2000 is one.
29
30         var year = dateObject.getFullYear();
31         return !(year%400) || (!(year%4) && !!(year%100)); // Boolean
32 }
33
34 // FIXME: This is not localized
35 dojo.date.getTimezoneName = function(/*Date*/dateObject){
36         //      summary:
37         //              Get the user's time zone as provided by the browser
38         // dateObject:
39         //              Needed because the timezone may vary with time (daylight savings)
40         //      description:
41         //              Try to get time zone info from toString or toLocaleString method of
42         //              the Date object -- UTC offset is not a time zone.  See
43         //              http://www.twinsun.com/tz/tz-link.htm Note: results may be
44         //              inconsistent across browsers.
45
46         var str = dateObject.toString(); // Start looking in toString
47         var tz = ''; // The result -- return empty string if nothing found
48         var match;
49
50         // First look for something in parentheses -- fast lookup, no regex
51         var pos = str.indexOf('(');
52         if(pos > -1){
53                 tz = str.substring(++pos, str.indexOf(')'));
54         }else{
55                 // If at first you don't succeed ...
56                 // If IE knows about the TZ, it appears before the year
57                 // Capital letters or slash before a 4-digit year 
58                 // at the end of string
59                 var pat = /([A-Z\/]+) \d{4}$/;
60                 if((match = str.match(pat))){
61                         tz = match[1];
62                 }else{
63                 // Some browsers (e.g. Safari) glue the TZ on the end
64                 // of toLocaleString instead of putting it in toString
65                         str = dateObject.toLocaleString();
66                         // Capital letters or slash -- end of string, 
67                         // after space
68                         pat = / ([A-Z\/]+)$/;
69                         if((match = str.match(pat))){
70                                 tz = match[1];
71                         }
72                 }
73         }
74
75         // Make sure it doesn't somehow end up return AM or PM
76         return (tz == 'AM' || tz == 'PM') ? '' : tz; // String
77 }
78
79 // Utility methods to do arithmetic calculations with Dates
80
81 dojo.date.compare = function(/*Date*/date1, /*Date?*/date2, /*String?*/portion){
82         //      summary:
83         //              Compare two date objects by date, time, or both.
84         //      description:
85         //      Returns 0 if equal, positive if a > b, else negative.
86         //      date1:
87         //              Date object
88         //      date2:
89         //              Date object.  If not specified, the current Date is used.
90         //      portion:
91         //              A string indicating the "date" or "time" portion of a Date object.
92         //              Compares both "date" and "time" by default.  One of the following:
93         //              "date", "time", "datetime"
94
95         // Extra step required in copy for IE - see #3112
96         date1 = new Date(Number(date1));
97         date2 = new Date(Number(date2 || new Date()));
98
99         if(portion !== "undefined"){
100                 if(portion == "date"){
101                         // Ignore times and compare dates.
102                         date1.setHours(0, 0, 0, 0);
103                         date2.setHours(0, 0, 0, 0);
104                 }else if(portion == "time"){
105                         // Ignore dates and compare times.
106                         date1.setFullYear(0, 0, 0);
107                         date2.setFullYear(0, 0, 0);
108                 }
109         }
110         
111         if(date1 > date2){ return 1; } // int
112         if(date1 < date2){ return -1; } // int
113         return 0; // int
114 };
115
116 dojo.date.add = function(/*Date*/date, /*String*/interval, /*int*/amount){
117         //      summary:
118         //              Add to a Date in intervals of different size, from milliseconds to years
119         //      date: Date
120         //              Date object to start with
121         //      interval:
122         //              A string representing the interval.  One of the following:
123         //                      "year", "month", "day", "hour", "minute", "second",
124         //                      "millisecond", "quarter", "week", "weekday"
125         //      amount:
126         //              How much to add to the date.
127
128         var sum = new Date(Number(date)); // convert to Number before copying to accomodate IE (#3112)
129         var fixOvershoot = false;
130         var property = "Date";
131
132         switch(interval){
133                 case "day":
134                         break;
135                 case "weekday":
136                         //i18n FIXME: assumes Saturday/Sunday weekend, but this is not always true.  see dojo.cldr.supplemental
137
138                         // Divide the increment time span into weekspans plus leftover days
139                         // e.g., 8 days is one 5-day weekspan / and two leftover days
140                         // Can't have zero leftover days, so numbers divisible by 5 get
141                         // a days value of 5, and the remaining days make up the number of weeks
142                         var days, weeks;
143                         var mod = amount % 5;
144                         if(!mod){
145                                 days = (amount > 0) ? 5 : -5;
146                                 weeks = (amount > 0) ? ((amount-5)/5) : ((amount+5)/5);
147                         }else{
148                                 days = mod;
149                                 weeks = parseInt(amount/5);
150                         }
151                         // Get weekday value for orig date param
152                         var strt = date.getDay();
153                         // Orig date is Sat / positive incrementer
154                         // Jump over Sun
155                         var adj = 0;
156                         if(strt == 6 && amount > 0){
157                                 adj = 1;
158                         }else if(strt == 0 && amount < 0){
159                         // Orig date is Sun / negative incrementer
160                         // Jump back over Sat
161                                 adj = -1;
162                         }
163                         // Get weekday val for the new date
164                         var trgt = strt + days;
165                         // New date is on Sat or Sun
166                         if(trgt == 0 || trgt == 6){
167                                 adj = (amount > 0) ? 2 : -2;
168                         }
169                         // Increment by number of weeks plus leftover days plus
170                         // weekend adjustments
171                         amount = (7 * weeks) + days + adj;
172                         break;
173                 case "year":
174                         property = "FullYear";
175                         // Keep increment/decrement from 2/29 out of March
176                         fixOvershoot = true;
177                         break;
178                 case "week":
179                         amount *= 7;
180                         break;
181                 case "quarter":
182                         // Naive quarter is just three months
183                         amount *= 3;
184                         // fallthrough...
185                 case "month":
186                         // Reset to last day of month if you overshoot
187                         fixOvershoot = true;
188                         property = "Month";
189                         break;
190                 case "hour":
191                 case "minute":
192                 case "second":
193                 case "millisecond":
194                         property = "UTC"+interval.charAt(0).toUpperCase() + interval.substring(1) + "s";
195         }
196
197         if(property){
198                 sum["set"+property](sum["get"+property]()+amount);
199         }
200
201         if(fixOvershoot && (sum.getDate() < date.getDate())){
202                 sum.setDate(0);
203         }
204
205         return sum; // Date
206 };
207
208 dojo.date.difference = function(/*Date*/date1, /*Date?*/date2, /*String?*/interval){
209         //      summary:
210         //              Get the difference in a specific unit of time (e.g., number of
211         //              months, weeks, days, etc.) between two dates, rounded to the
212         //              nearest integer.
213         //      date1:
214         //              Date object
215         //      date2:
216         //              Date object.  If not specified, the current Date is used.
217         //      interval:
218         //              A string representing the interval.  One of the following:
219         //                      "year", "month", "day", "hour", "minute", "second",
220         //                      "millisecond", "quarter", "week", "weekday"
221         //              Defaults to "day".
222
223         date2 = date2 || new Date();
224         interval = interval || "day";
225         var yearDiff = date2.getFullYear() - date1.getFullYear();
226         var delta = 1; // Integer return value
227
228         switch(interval){
229                 case "quarter":
230                         var m1 = date1.getMonth();
231                         var m2 = date2.getMonth();
232                         // Figure out which quarter the months are in
233                         var q1 = Math.floor(m1/3) + 1;
234                         var q2 = Math.floor(m2/3) + 1;
235                         // Add quarters for any year difference between the dates
236                         q2 += (yearDiff * 4);
237                         delta = q2 - q1;
238                         break;
239                 case "weekday":
240                         var days = Math.round(dojo.date.difference(date1, date2, "day"));
241                         var weeks = parseInt(dojo.date.difference(date1, date2, "week"));
242                         var mod = days % 7;
243
244                         // Even number of weeks
245                         if(mod == 0){
246                                 days = weeks*5;
247                         }else{
248                                 // Weeks plus spare change (< 7 days)
249                                 var adj = 0;
250                                 var aDay = date1.getDay();
251                                 var bDay = date2.getDay();
252
253                                 weeks = parseInt(days/7);
254                                 mod = days % 7;
255                                 // Mark the date advanced by the number of
256                                 // round weeks (may be zero)
257                                 var dtMark = new Date(date1);
258                                 dtMark.setDate(dtMark.getDate()+(weeks*7));
259                                 var dayMark = dtMark.getDay();
260
261                                 // Spare change days -- 6 or less
262                                 if(days > 0){
263                                         switch(true){
264                                                 // Range starts on Sat
265                                                 case aDay == 6:
266                                                         adj = -1;
267                                                         break;
268                                                 // Range starts on Sun
269                                                 case aDay == 0:
270                                                         adj = 0;
271                                                         break;
272                                                 // Range ends on Sat
273                                                 case bDay == 6:
274                                                         adj = -1;
275                                                         break;
276                                                 // Range ends on Sun
277                                                 case bDay == 0:
278                                                         adj = -2;
279                                                         break;
280                                                 // Range contains weekend
281                                                 case (dayMark + mod) > 5:
282                                                         adj = -2;
283                                         }
284                                 }else if(days < 0){
285                                         switch(true){
286                                                 // Range starts on Sat
287                                                 case aDay == 6:
288                                                         adj = 0;
289                                                         break;
290                                                 // Range starts on Sun
291                                                 case aDay == 0:
292                                                         adj = 1;
293                                                         break;
294                                                 // Range ends on Sat
295                                                 case bDay == 6:
296                                                         adj = 2;
297                                                         break;
298                                                 // Range ends on Sun
299                                                 case bDay == 0:
300                                                         adj = 1;
301                                                         break;
302                                                 // Range contains weekend
303                                                 case (dayMark + mod) < 0:
304                                                         adj = 2;
305                                         }
306                                 }
307                                 days += adj;
308                                 days -= (weeks*2);
309                         }
310                         delta = days;
311                         break;
312                 case "year":
313                         delta = yearDiff;
314                         break;
315                 case "month":
316                         delta = (date2.getMonth() - date1.getMonth()) + (yearDiff * 12);
317                         break;
318                 case "week":
319                         // Truncate instead of rounding
320                         // Don't use Math.floor -- value may be negative
321                         delta = parseInt(dojo.date.difference(date1, date2, "day")/7);
322                         break;
323                 case "day":
324                         delta /= 24;
325                         // fallthrough
326                 case "hour":
327                         delta /= 60;
328                         // fallthrough
329                 case "minute":
330                         delta /= 60;
331                         // fallthrough
332                 case "second":
333                         delta /= 1000;
334                         // fallthrough
335                 case "millisecond":
336                         delta *= date2.getTime() - date1.getTime();
337         }
338
339         // Round for fractional values and DST leaps
340         return Math.round(delta); // Number (integer)
341 };
342
343 }