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