source: Dev/branches/rest-dojo-ui/client/dojox/date/buddhist.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 6.4 KB
Line 
1define(["dojo/_base/kernel", "dojo/date", "./buddhist/Date"], function(dojo, dd, buddhistDate){
2        dojo.getObject("date.buddhist", true, dojox);
3        dojo.experimental("dojox.date.buddhist");
4
5// Utility methods to do arithmetic calculations with buddhist.Dates
6
7dojox.date.buddhist.getDaysInMonth = function(/*buddhist.Date*/dateObject){
8        return dd.getDaysInMonth(dateObject.toGregorian());
9};
10
11dojox.date.buddhist.isLeapYear = function(/*buddhist.Date*/dateObject){
12        return dd.isLeapYear(dateObject.toGregorian());
13};
14
15//FIXME: reduce compare, add, diff also
16dojox.date.buddhist.compare = function(/*buddhist.Date*/date1, /*buddhist.Date*/date2, /*String?*/portion){
17        //      summary:
18        //              Compare two buddhist date objects by date, time, or both.
19        return dd.compare(date1,date2, portion); // int
20};
21
22
23dojox.date.buddhist.add = function(/*dojox.date.buddhist.Date*/date, /*String*/interval, /*int*/amount){
24        //      based on and similar to dojo.date.add
25        //      summary:
26        //              Add to a Date in intervals of different size, from milliseconds to years
27        //      date: buddhist.Date
28        //              Date object to start with
29        //      interval:
30        //              A string representing the interval.  One of the following:
31        //                      "year", "month", "day", "hour", "minute", "second",
32        //                      "millisecond", "week", "weekday"
33        //      amount:
34        //              How much to add to the date.
35
36        var newBuddDate = new buddhistDate(date);
37
38        switch(interval){
39                case "day":
40                        newBuddDate.setDate(date.getDate(true) + amount);
41                        break;
42                case "weekday":
43                        var days, weeks;
44                        var mod = amount % 5;
45                        if(!mod){
46                                days = (amount > 0) ? 5 : -5;
47                                weeks = (amount > 0) ? ((amount-5)/5) : ((amount+5)/5);
48                        }else{
49                                days = mod;
50                                weeks = parseInt(amount/5);
51                        }
52                        // Get weekday value for orig date param
53                        var strt = date.getDay();
54                        // Orig date is Sat / positive incrementer
55                        // Jump over Sun
56                        var adj = 0;
57                        if(strt == 6 && amount > 0){
58                                adj = 1;
59                        }else if(strt == 0 && amount < 0){
60                        // Orig date is Sun / negative incrementer
61                        // Jump back over Sat
62                                adj = -1;
63                        }
64                        // Get weekday val for the new date
65                        var trgt = strt + days;
66                        // New date is on Sat or Sun
67                        if(trgt == 0 || trgt == 6){
68                                adj = (amount > 0) ? 2 : -2;
69                        }
70                        // Increment by number of weeks plus leftover days plus
71                        // weekend adjustments
72                        amount = (7 * weeks) + days + adj;
73                        newBuddDate.setDate(date.getDate(true) + amount);
74                        break;
75                case "year":
76                        newBuddDate.setFullYear(date.getFullYear() + amount );
77                        break;
78                case "week":
79                        amount *= 7;
80                        newBuddDate.setDate(date.getDate(true) + amount);
81                        break;
82                case "month":
83                        newBuddDate.setMonth(date.getMonth() + amount);
84                        break;
85                case "hour":
86                        newBuddDate.setHours(date.getHours() + amount );
87                        break;
88                case "minute":
89                        newBuddDate._addMinutes(amount);
90                        break;
91                case "second":
92                        newBuddDate._addSeconds(amount);
93                        break;
94                case "millisecond":
95                        newBuddDate._addMilliseconds(amount);
96                        break;
97        }
98        return newBuddDate; // dojox.date.buddhist.Date
99};
100
101dojox.date.buddhist.difference = function(/*dojox.date.buddhist.Date*/date1, /*dojox.date.buddhist.Date?*/date2, /*String?*/interval){
102        //      based on and similar to dojo.date.difference
103        //      summary:
104        //        date2 - date1
105        //       date2 is hebrew.Date object.  If not specified, the current hebrew.Date is used.
106        //      interval:
107        //              A string representing the interval.  One of the following:
108        //                      "year", "month", "day", "hour", "minute", "second",
109        //                      "millisecond",  "week", "weekday"
110        //              Defaults to "day".
111       
112        date2 = date2 || new buddhistDate();
113        interval = interval || "day";
114        var yearDiff = date2.getFullYear() - date1.getFullYear();
115        var delta = 1; // Integer return value
116        switch(interval){
117                case "weekday":
118                        var days = Math.round(dojox.date.buddhist.difference(date1, date2, "day"));
119                        var weeks = parseInt(dojox.date.buddhist.difference(date1, date2, "week"));
120                        var mod = days % 7;
121       
122                        // Even number of weeks
123                        if(mod == 0){
124                                days = weeks*5;
125                        }else{
126                                // Weeks plus spare change (< 7 days)
127                                var adj = 0;
128                                var aDay = date1.getDay();
129                                var bDay = date2.getDay();
130       
131                                weeks = parseInt(days/7);
132                                mod = days % 7;
133                                // Mark the date advanced by the number of
134                                // round weeks (may be zero)
135                                var dtMark = new buddhistDate(date2);
136                                dtMark.setDate(dtMark.getDate(true)+(weeks*7));
137                                var dayMark = dtMark.getDay();
138       
139                                // Spare change days -- 6 or less
140                                if(days > 0){
141                                        switch(true){
142                                                // Range starts on Fri
143                                                case aDay == 5:
144                                                        adj = -1;
145                                                        break;
146                                                // Range starts on Sat
147                                                case aDay == 6:
148                                                        adj = 0;
149                                                        break;
150                                                // Range ends on Fri
151                                                case bDay == 5:
152                                                        adj = -1;
153                                                        break;
154                                                // Range ends on Sat
155                                                case bDay == 6:
156                                                        adj = -2;
157                                                        break;
158                                                // Range contains weekend
159                                                case (dayMark + mod) > 5:
160                                                        adj = -2;
161                                        }
162                                }else if(days < 0){
163                                        switch(true){
164                                                // Range starts on Fri
165                                                case aDay == 5:
166                                                        adj = 0;
167                                                        break;
168                                                // Range starts on Sat
169                                                case aDay == 6:
170                                                        adj = 1;
171                                                        break;
172                                                // Range ends on Fri
173                                                case bDay == 5:
174                                                        adj = 2;
175                                                        break;
176                                                // Range ends on Sat
177                                                case bDay == 6:
178                                                        adj = 1;
179                                                        break;
180                                                // Range contains weekend
181                                                case (dayMark + mod) < 0:
182                                                        adj = 2;
183                                        }
184                                }
185                                days += adj;
186                                days -= (weeks*2);
187                        }
188                        delta = days;
189                        break;
190                case "year":
191                        delta = yearDiff;
192                        break;
193                case "month":
194                        var startdate =  (date2.toGregorian() > date1.toGregorian()) ? date2 : date1; // more
195                        var enddate = (date2.toGregorian() > date1.toGregorian()) ? date1 : date2;
196                       
197                        var month1 = startdate.getMonth();
198                        var month2 = enddate.getMonth();
199                       
200                        if (yearDiff == 0){
201                                delta = startdate.getMonth() - enddate.getMonth() ;
202                        }else{
203                                delta = 12-month2;
204                                delta +=  month1;
205                                var i = enddate.getFullYear()+1;
206                                var e = startdate.getFullYear();
207                                for (i;   i < e;  i++){
208                                        delta += 12;
209                                }
210                        }
211                        if (date2.toGregorian() < date1.toGregorian()){
212                                delta = -delta;
213                        }
214                        break;
215                case "week":
216                        // Truncate instead of rounding
217                        // Don't use Math.floor -- value may be negative
218                        delta = parseInt(dojox.date.buddhist.difference(date1, date2, "day")/7);
219                        break;
220                case "day":
221                        delta /= 24;
222                        // fallthrough
223                case "hour":
224                        delta /= 60;
225                        // fallthrough
226                case "minute":
227                        delta /= 60;
228                        // fallthrough
229                case "second":
230                        delta /= 1000;
231                        // fallthrough
232                case "millisecond":
233                        delta *= date2.toGregorian().getTime()- date1.toGregorian().getTime();
234        }
235       
236        // Round for fractional values and DST leaps
237        return Math.round(delta); // Number (integer)
238};
239return dojox.date.buddhist;
240});
Note: See TracBrowser for help on using the repository browser.