source: Dev/trunk/src/client/dojox/date/buddhist.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 6.4 KB
Line 
1define(["..", "dojo/_base/lang", "dojo/date", "./buddhist/Date"], function(dojox, lang, dd, BDate){
2        var dbuddhist = lang.getObject("date.buddhist", true, dojox);
3
4// Utility methods to do arithmetic calculations with buddhist.Dates
5
6dbuddhist.getDaysInMonth = function(/*dojox/date/buddhist/Date*/dateObject){
7        return dd.getDaysInMonth(dateObject.toGregorian());
8};
9
10dbuddhist.isLeapYear = function(/*dojox/date/buddhist/Date*/dateObject){
11        return dd.isLeapYear(dateObject.toGregorian());
12};
13
14//FIXME: reduce compare, add, diff also
15dbuddhist.compare = function(/*dojox/date/buddhist/Date*/date1, /*dojox/date/buddhist/Date*/date2, /*String?*/portion){
16        // summary:
17        //              Compare two buddhist date objects by date, time, or both.
18        return dd.compare(date1,date2, portion); // int
19};
20
21
22dbuddhist.add = function(/*dojox/date/buddhist/Date*/date, /*String*/interval, /*int*/amount){
23        // summary:
24        //              Add to a Date in intervals of different size, from milliseconds to years
25        // date: dojox/date/buddhist/Date
26        //              Date object to start with
27        // interval:
28        //              A string representing the interval.  One of the following:
29        //              "year", "month", "day", "hour", "minute", "second",
30        //              "millisecond", "week", "weekday"
31        // amount:
32        //              How much to add to the date.
33
34        //      based on and similar to dojo.date.add
35
36        var newBuddDate = new BDate(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
101dbuddhist.difference = function(/*dojox/date/buddhist/Date*/date1, /*dojox/date/buddhist/Date?*/date2, /*String?*/interval){
102        // summary:
103        //              date2 - date1
104        // date1: dojox/date/dbuddhist/Date
105        // date2: dojox/date/dbuddhist/Date
106        //              If not specified, the current dojox.date.dbuddhist.Date is used.
107        // interval:
108        //              A string representing the interval.  One of the following:
109        //              "year", "month", "day", "hour", "minute", "second",
110        //              "millisecond",  "week", "weekday"
111        //
112        //              Defaults to "day".
113
114        //      based on and similar to dojo.date.difference
115
116        date2 = date2 || new BDate();
117        interval = interval || "day";
118        var yearDiff = date2.getFullYear() - date1.getFullYear();
119        var delta = 1; // Integer return value
120        switch(interval){
121                case "weekday":
122                        var days = Math.round(dbuddhist.difference(date1, date2, "day"));
123                        var weeks = parseInt(dbuddhist.difference(date1, date2, "week"));
124                        var mod = days % 7;
125       
126                        // Even number of weeks
127                        if(mod == 0){
128                                days = weeks*5;
129                        }else{
130                                // Weeks plus spare change (< 7 days)
131                                var adj = 0;
132                                var aDay = date1.getDay();
133                                var bDay = date2.getDay();
134       
135                                weeks = parseInt(days/7);
136                                mod = days % 7;
137                                // Mark the date advanced by the number of
138                                // round weeks (may be zero)
139                                var dtMark = new BDate(date2);
140                                dtMark.setDate(dtMark.getDate(true)+(weeks*7));
141                                var dayMark = dtMark.getDay();
142       
143                                // Spare change days -- 6 or less
144                                if(days > 0){
145                                        switch(true){
146                                                // Range starts on Fri
147                                                case aDay == 5:
148                                                        adj = -1;
149                                                        break;
150                                                // Range starts on Sat
151                                                case aDay == 6:
152                                                        adj = 0;
153                                                        break;
154                                                // Range ends on Fri
155                                                case bDay == 5:
156                                                        adj = -1;
157                                                        break;
158                                                // Range ends on Sat
159                                                case bDay == 6:
160                                                        adj = -2;
161                                                        break;
162                                                // Range contains weekend
163                                                case (dayMark + mod) > 5:
164                                                        adj = -2;
165                                        }
166                                }else if(days < 0){
167                                        switch(true){
168                                                // Range starts on Fri
169                                                case aDay == 5:
170                                                        adj = 0;
171                                                        break;
172                                                // Range starts on Sat
173                                                case aDay == 6:
174                                                        adj = 1;
175                                                        break;
176                                                // Range ends on Fri
177                                                case bDay == 5:
178                                                        adj = 2;
179                                                        break;
180                                                // Range ends on Sat
181                                                case bDay == 6:
182                                                        adj = 1;
183                                                        break;
184                                                // Range contains weekend
185                                                case (dayMark + mod) < 0:
186                                                        adj = 2;
187                                        }
188                                }
189                                days += adj;
190                                days -= (weeks*2);
191                        }
192                        delta = days;
193                        break;
194                case "year":
195                        delta = yearDiff;
196                        break;
197                case "month":
198                        var startdate =  (date2.toGregorian() > date1.toGregorian()) ? date2 : date1; // more
199                        var enddate = (date2.toGregorian() > date1.toGregorian()) ? date1 : date2;
200                       
201                        var month1 = startdate.getMonth();
202                        var month2 = enddate.getMonth();
203                       
204                        if (yearDiff == 0){
205                                delta = startdate.getMonth() - enddate.getMonth() ;
206                        }else{
207                                delta = 12-month2;
208                                delta +=  month1;
209                                var i = enddate.getFullYear()+1;
210                                var e = startdate.getFullYear();
211                                for (i;   i < e;  i++){
212                                        delta += 12;
213                                }
214                        }
215                        if (date2.toGregorian() < date1.toGregorian()){
216                                delta = -delta;
217                        }
218                        break;
219                case "week":
220                        // Truncate instead of rounding
221                        // Don't use Math.floor -- value may be negative
222                        delta = parseInt(dbuddhist.difference(date1, date2, "day")/7);
223                        break;
224                case "day":
225                        delta /= 24;
226                        // fallthrough
227                case "hour":
228                        delta /= 60;
229                        // fallthrough
230                case "minute":
231                        delta /= 60;
232                        // fallthrough
233                case "second":
234                        delta /= 1000;
235                        // fallthrough
236                case "millisecond":
237                        delta *= date2.toGregorian().getTime()- date1.toGregorian().getTime();
238        }
239       
240        // Round for fractional values and DST leaps
241        return Math.round(delta); // Number (integer)
242};
243return dbuddhist;
244});
Note: See TracBrowser for help on using the repository browser.