1 | define(["dojo/_base/kernel", "dojo/_base/declare", "dojo/date"], function(dojo, declare, dd){ |
---|
2 | |
---|
3 | dojo.getObject("date.buddhist.Date", true, dojox); |
---|
4 | dojo.experimental("dojox.date.buddhist.Date"); |
---|
5 | |
---|
6 | dojo.requireLocalization("dojo.cldr", "islamic"); |
---|
7 | |
---|
8 | dojo.declare("dojox.date.islamic.Date", null, { |
---|
9 | // summary: The component defines the Islamic (Hijri) Calendar Object |
---|
10 | // |
---|
11 | // description: |
---|
12 | // This module is similar to the Date() object provided by JavaScript |
---|
13 | // |
---|
14 | // example: |
---|
15 | // | dojo.require("dojox.date.islamic.Date"); |
---|
16 | // | |
---|
17 | // | var date = new dojox.date.islamic.Date(); |
---|
18 | // | document.writeln(date.getFullYear()+'\'+date.getMonth()+'\'+date.getDate()); |
---|
19 | |
---|
20 | |
---|
21 | _date: 0, |
---|
22 | _month: 0, |
---|
23 | _year: 0, |
---|
24 | _hours: 0, |
---|
25 | _minutes: 0, |
---|
26 | _seconds: 0, |
---|
27 | _milliseconds: 0, |
---|
28 | _day: 0, |
---|
29 | _GREGORIAN_EPOCH : 1721425.5, |
---|
30 | _ISLAMIC_EPOCH : 1948439.5, |
---|
31 | |
---|
32 | constructor: function(){ |
---|
33 | // summary: This is the constructor |
---|
34 | // description: |
---|
35 | // This function initialize the date object values |
---|
36 | // |
---|
37 | // example: |
---|
38 | // | var date1 = new dojox.date.islamic.Date(); |
---|
39 | // | |
---|
40 | // | var date2 = new dojox.date.islamic.Date("12\2\1429"); |
---|
41 | // | |
---|
42 | // | var date3 = new dojox.date.islamic.Date(date2); |
---|
43 | // | |
---|
44 | // | var date4 = new dojox.date.islamic.Date(1429,2,12); |
---|
45 | |
---|
46 | var len = arguments.length; |
---|
47 | if(!len){// use the current date value, added "" to the similarity to date |
---|
48 | this.fromGregorian(new Date()); |
---|
49 | }else if(len == 1){ |
---|
50 | var arg0 = arguments[0]; |
---|
51 | if(typeof arg0 == "number"){ // this is time "valueof" |
---|
52 | arg0 = new Date(arg0); |
---|
53 | } |
---|
54 | |
---|
55 | if(arg0 instanceof Date){ |
---|
56 | this.fromGregorian(arg0); |
---|
57 | }else if(arg0 == ""){ |
---|
58 | // date should be invalid. Dijit relies on this behavior. |
---|
59 | this._date = new Date(""); //TODO: should this be NaN? _date is not a Date object |
---|
60 | }else{ // this is Islamic.Date object |
---|
61 | this._year = arg0._year; |
---|
62 | this._month = arg0._month; |
---|
63 | this._date = arg0._date; |
---|
64 | this._hours = arg0._hours; |
---|
65 | this._minutes = arg0._minutes; |
---|
66 | this._seconds = arg0._seconds; |
---|
67 | this._milliseconds = arg0._milliseconds; |
---|
68 | } |
---|
69 | }else if(len >=3){ |
---|
70 | // YYYY MM DD arguments passed, month is from 0-12 |
---|
71 | this._year += arguments[0]; |
---|
72 | this._month += arguments[1]; |
---|
73 | this._date += arguments[2]; |
---|
74 | this._hours += arguments[3] || 0; |
---|
75 | this._minutes += arguments[4] || 0; |
---|
76 | this._seconds += arguments[5] || 0; |
---|
77 | this._milliseconds += arguments[6] || 0; |
---|
78 | } |
---|
79 | }, |
---|
80 | |
---|
81 | getDate:function(){ |
---|
82 | // summary: This function returns the date value (1 - 30) |
---|
83 | // |
---|
84 | // example: |
---|
85 | // | var date1 = new dojox.date.islamic.Date(); |
---|
86 | // | |
---|
87 | // | document.writeln(date1.getDate); |
---|
88 | return this._date; |
---|
89 | }, |
---|
90 | |
---|
91 | getMonth:function(){ |
---|
92 | // summary: This function return the month value ( 0 - 11 ) |
---|
93 | // |
---|
94 | // example: |
---|
95 | // | var date1 = new dojox.date.islamic.Date(); |
---|
96 | // | |
---|
97 | // | document.writeln(date1.getMonth()+1); |
---|
98 | |
---|
99 | return this._month; |
---|
100 | }, |
---|
101 | |
---|
102 | getFullYear:function(){ |
---|
103 | // summary: This function return the Year value |
---|
104 | // |
---|
105 | // example: |
---|
106 | // | var date1 = new dojox.date.islamic.Date(); |
---|
107 | // | |
---|
108 | // | document.writeln(date1.getFullYear()); |
---|
109 | |
---|
110 | return this._year; |
---|
111 | }, |
---|
112 | |
---|
113 | getDay:function(){ |
---|
114 | // summary: This function return Week Day value ( 0 - 6 ) |
---|
115 | // |
---|
116 | // example: |
---|
117 | // | var date1 = new dojox.date.islamic.Date(); |
---|
118 | // | |
---|
119 | // | document.writeln(date1.getDay()); |
---|
120 | |
---|
121 | return this.toGregorian().getDay(); |
---|
122 | }, |
---|
123 | |
---|
124 | getHours:function(){ |
---|
125 | //summary: returns the Hour value |
---|
126 | return this._hours; |
---|
127 | }, |
---|
128 | |
---|
129 | getMinutes:function(){ |
---|
130 | //summary: returns the Minuites value |
---|
131 | return this._minutes; |
---|
132 | }, |
---|
133 | |
---|
134 | getSeconds:function(){ |
---|
135 | //summary: returns the seconde value |
---|
136 | return this._seconds; |
---|
137 | }, |
---|
138 | |
---|
139 | getMilliseconds:function(){ |
---|
140 | //summary: returns the Milliseconds value |
---|
141 | return this._milliseconds; |
---|
142 | }, |
---|
143 | |
---|
144 | setDate: function(/*number*/date){ |
---|
145 | // summary: This function sets the Date |
---|
146 | // example: |
---|
147 | // | var date1 = new dojox.date.islamic.Date(); |
---|
148 | // | date1.setDate(2); |
---|
149 | |
---|
150 | date = parseInt(date); |
---|
151 | |
---|
152 | if(date > 0 && date <= this.getDaysInIslamicMonth(this._month, this._year)){ |
---|
153 | this._date = date; |
---|
154 | }else{ |
---|
155 | var mdays; |
---|
156 | if(date>0){ |
---|
157 | for(mdays = this.getDaysInIslamicMonth(this._month, this._year); |
---|
158 | date > mdays; |
---|
159 | date -= mdays,mdays =this.getDaysInIslamicMonth(this._month, this._year)){ |
---|
160 | this._month++; |
---|
161 | if(this._month >= 12){this._year++; this._month -= 12;} |
---|
162 | } |
---|
163 | |
---|
164 | this._date = date; |
---|
165 | }else{ |
---|
166 | for(mdays = this.getDaysInIslamicMonth((this._month-1)>=0 ?(this._month-1) :11 ,((this._month-1)>=0)? this._year: this._year-1); |
---|
167 | date <= 0; |
---|
168 | mdays = this.getDaysInIslamicMonth((this._month-1)>=0 ? (this._month-1) :11,((this._month-1)>=0)? this._year: this._year-1)){ |
---|
169 | this._month--; |
---|
170 | if(this._month < 0){this._year--; this._month += 12;} |
---|
171 | |
---|
172 | date+=mdays; |
---|
173 | } |
---|
174 | this._date = date; |
---|
175 | } |
---|
176 | } |
---|
177 | return this; |
---|
178 | }, |
---|
179 | |
---|
180 | setFullYear:function(/*number*/year){ |
---|
181 | // summary: This function set Year |
---|
182 | // |
---|
183 | // example: |
---|
184 | // | var date1 = new dojox.date.islamic.Date(); |
---|
185 | // | date1.setYear(1429); |
---|
186 | |
---|
187 | this._year = +year; |
---|
188 | }, |
---|
189 | |
---|
190 | setMonth: function(/*number*/month) { |
---|
191 | // summary: This function set Month |
---|
192 | // |
---|
193 | // example: |
---|
194 | // | var date1 = new dojox.date.islamic.Date(); |
---|
195 | // | date1.setMonth(2); |
---|
196 | |
---|
197 | this._year += Math.floor(month / 12); |
---|
198 | if(month > 0){ |
---|
199 | this._month = Math.floor(month % 12); |
---|
200 | }else{ |
---|
201 | this._month = Math.floor(((month % 12) + 12) % 12); |
---|
202 | } |
---|
203 | }, |
---|
204 | |
---|
205 | setHours:function(){ |
---|
206 | //summary: set the Hours |
---|
207 | var hours_arg_no = arguments.length; |
---|
208 | var hours = 0; |
---|
209 | if(hours_arg_no >= 1){ |
---|
210 | hours = parseInt(arguments[0]); |
---|
211 | } |
---|
212 | |
---|
213 | if(hours_arg_no >= 2){ |
---|
214 | this._minutes = parseInt(arguments[1]); |
---|
215 | } |
---|
216 | |
---|
217 | if(hours_arg_no >= 3){ |
---|
218 | this._seconds = parseInt(arguments[2]); |
---|
219 | } |
---|
220 | |
---|
221 | if(hours_arg_no == 4){ |
---|
222 | this._milliseconds = parseInt(arguments[3]); |
---|
223 | } |
---|
224 | |
---|
225 | while(hours >= 24){ |
---|
226 | this._date++; |
---|
227 | var mdays = this.getDaysInIslamicMonth(this._month, this._year); |
---|
228 | if(this._date > mdays){ |
---|
229 | this._month ++; |
---|
230 | if(this._month >= 12){this._year++; this._month -= 12;} |
---|
231 | this._date -= mdays; |
---|
232 | } |
---|
233 | hours -= 24; |
---|
234 | } |
---|
235 | this._hours = hours; |
---|
236 | }, |
---|
237 | |
---|
238 | _addMinutes: function(/*Number*/minutes){ |
---|
239 | minutes += this._minutes; |
---|
240 | this.setMinutes(minutes); |
---|
241 | this.setHours(this._hours + parseInt(minutes / 60)); |
---|
242 | return this; |
---|
243 | }, |
---|
244 | |
---|
245 | _addSeconds: function(/*Number*/seconds){ |
---|
246 | seconds += this._seconds; |
---|
247 | this.setSeconds(seconds); |
---|
248 | this._addMinutes(parseInt(seconds / 60)); |
---|
249 | return this; |
---|
250 | }, |
---|
251 | |
---|
252 | _addMilliseconds: function(/*Number*/milliseconds){ |
---|
253 | milliseconds += this._milliseconds; |
---|
254 | this.setMilliseconds(milliseconds); |
---|
255 | this._addSeconds(parseInt(milliseconds / 1000)); |
---|
256 | return this; |
---|
257 | }, |
---|
258 | |
---|
259 | setMinutes: function(/*Number*/minutes){ |
---|
260 | //summary: sets the minutes (0-59) only. |
---|
261 | this._minutes = minutes % 60; |
---|
262 | return this; |
---|
263 | }, |
---|
264 | |
---|
265 | setSeconds: function(/*Number*/seconds){ |
---|
266 | //summary: sets the seconds (0-59) only. |
---|
267 | this._seconds = seconds % 60; |
---|
268 | return this; |
---|
269 | }, |
---|
270 | |
---|
271 | setMilliseconds: function(/*Number*/milliseconds){ |
---|
272 | this._milliseconds = milliseconds % 1000; |
---|
273 | return this; |
---|
274 | }, |
---|
275 | |
---|
276 | toString:function(){ |
---|
277 | // summary: This returns a string representation of the date in "DDDD MMMM DD YYYY HH:MM:SS" format |
---|
278 | // example: |
---|
279 | // | var date1 = new dojox.date.islamic.Date(); |
---|
280 | // | document.writeln(date1.toString()); |
---|
281 | |
---|
282 | //FIXME: TZ/DST issues? |
---|
283 | var x = new Date(); |
---|
284 | x.setHours(this._hours); |
---|
285 | x.setMinutes(this._minutes); |
---|
286 | x.setSeconds(this._seconds); |
---|
287 | x.setMilliseconds(this._milliseconds); |
---|
288 | return this._month+" "+ this._date + " " + this._year + " " + x.toTimeString(); |
---|
289 | }, |
---|
290 | |
---|
291 | |
---|
292 | toGregorian:function(){ |
---|
293 | // summary: This returns the equevalent Grogorian date value in Date object |
---|
294 | // example: |
---|
295 | // | var dateIslamic = new dojox.date.islamic.Date(1429,11,20); |
---|
296 | // | var dateGregorian = dateIslamic.toGregorian(); |
---|
297 | |
---|
298 | var hYear = this._year; |
---|
299 | var hMonth = this._month; |
---|
300 | var hDate = this._date; |
---|
301 | var julianDay = hDate + Math.ceil(29.5 * hMonth) + (hYear - 1) * 354 |
---|
302 | + Math.floor((3 + (11 * hYear)) / 30) + this._ISLAMIC_EPOCH - 1; |
---|
303 | |
---|
304 | var wjd = Math.floor(julianDay - 0.5) + 0.5, |
---|
305 | depoch = wjd - this._GREGORIAN_EPOCH, |
---|
306 | quadricent = Math.floor(depoch / 146097), |
---|
307 | dqc = this._mod(depoch, 146097), |
---|
308 | cent = Math.floor(dqc / 36524), |
---|
309 | dcent = this._mod(dqc, 36524), |
---|
310 | quad = Math.floor(dcent / 1461), |
---|
311 | dquad = this._mod(dcent, 1461), |
---|
312 | yindex = Math.floor(dquad / 365), |
---|
313 | year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex; |
---|
314 | if(!(cent == 4 || yindex == 4)){ |
---|
315 | year++; |
---|
316 | } |
---|
317 | |
---|
318 | var gYearStart = this._GREGORIAN_EPOCH + (365 * (year - 1)) + Math.floor((year - 1) / 4) |
---|
319 | - ( Math.floor((year - 1) / 100)) + Math.floor((year - 1) / 400); |
---|
320 | |
---|
321 | var yearday = wjd - gYearStart; |
---|
322 | |
---|
323 | var tjd = (this._GREGORIAN_EPOCH - 1) + (365 * (year - 1)) + Math.floor((year - 1) / 4) |
---|
324 | -( Math.floor((year - 1) / 100)) + Math.floor((year - 1) / 400) + Math.floor( (739 / 12) |
---|
325 | + ( (dd.isLeapYear(new Date(year,3,1)) ? -1 : -2)) + 1); |
---|
326 | |
---|
327 | var leapadj = ((wjd < tjd ) ? 0 : (dd.isLeapYear(new Date(year,3,1)) ? 1 : 2)); |
---|
328 | |
---|
329 | var month = Math.floor((((yearday + leapadj) * 12) + 373) / 367); |
---|
330 | var tjd2 = (this._GREGORIAN_EPOCH - 1) + (365 * (year - 1)) |
---|
331 | + Math.floor((year - 1) / 4) - (Math.floor((year - 1) / 100)) |
---|
332 | + Math.floor((year - 1) / 400) + Math.floor((((367 * month) - 362) / 12) |
---|
333 | + ((month <= 2) ? 0 : (dd.isLeapYear(new Date(year,month,1)) ? -1 : -2)) + 1); |
---|
334 | |
---|
335 | var day = (wjd - tjd2) + 1; |
---|
336 | |
---|
337 | var gdate = new Date(year, (month - 1), day, this._hours, this._minutes, this._seconds, this._milliseconds); |
---|
338 | |
---|
339 | return gdate; |
---|
340 | }, |
---|
341 | |
---|
342 | //TODO: would it make more sense to make this a constructor option? or a static? |
---|
343 | // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/ |
---|
344 | fromGregorian:function(/*Date*/gdate){ |
---|
345 | // summary: This function returns the equivalent Islamic Date value for the Gregorian Date |
---|
346 | // example: |
---|
347 | // | var dateIslamic = new dojox.date.islamic.Date(); |
---|
348 | // | var dateGregorian = new Date(2008,10,12); |
---|
349 | // | dateIslamic.fromGregorian(dateGregorian); |
---|
350 | |
---|
351 | var date = new Date(gdate); |
---|
352 | var gYear = date.getFullYear(), |
---|
353 | gMonth = date.getMonth(), |
---|
354 | gDay = date.getDate(); |
---|
355 | |
---|
356 | var julianDay = (this._GREGORIAN_EPOCH - 1) + (365 * (gYear - 1)) + Math.floor((gYear - 1) / 4) |
---|
357 | + (-Math.floor((gYear - 1) / 100)) + Math.floor((gYear - 1) / 400) |
---|
358 | + Math.floor((((367 * (gMonth+1)) - 362) / 12) |
---|
359 | + (((gMonth+1) <= 2) ? 0 : (dd.isLeapYear(date) ? -1 : -2)) + gDay); |
---|
360 | julianDay = Math.floor(julianDay) + 0.5; |
---|
361 | |
---|
362 | var days = julianDay - this._ISLAMIC_EPOCH; |
---|
363 | var hYear = Math.floor( (30 * days + 10646) / 10631.0 ); |
---|
364 | var hMonth = Math.ceil((days - 29 - this._yearStart(hYear)) / 29.5 ); |
---|
365 | hMonth = Math.min(hMonth, 11); |
---|
366 | var hDay = Math.ceil(days - this._monthStart(hYear, hMonth)) + 1; |
---|
367 | |
---|
368 | this._date = hDay; |
---|
369 | this._month = hMonth; |
---|
370 | this._year = hYear; |
---|
371 | this._hours = date.getHours(); |
---|
372 | this._minutes = date.getMinutes(); |
---|
373 | this._seconds = date.getSeconds(); |
---|
374 | this._milliseconds = date.getMilliseconds(); |
---|
375 | this._day = date.getDay(); |
---|
376 | return this; |
---|
377 | }, |
---|
378 | |
---|
379 | valueOf:function(){ |
---|
380 | // summary: This function returns The stored time value in milliseconds |
---|
381 | // since midnight, January 1, 1970 UTC |
---|
382 | |
---|
383 | return this.toGregorian().valueOf(); |
---|
384 | }, |
---|
385 | |
---|
386 | // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/ |
---|
387 | _yearStart:function(/*Number*/year){ |
---|
388 | //summary: return start of Islamic year |
---|
389 | return (year-1)*354 + Math.floor((3+11*year)/30.0); |
---|
390 | }, |
---|
391 | |
---|
392 | // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/ |
---|
393 | _monthStart:function(/*Number*/year, /*Number*/month){ |
---|
394 | //summary: return the start of Islamic Month |
---|
395 | return Math.ceil(29.5*month) + |
---|
396 | (year-1)*354 + Math.floor((3+11*year)/30.0); |
---|
397 | }, |
---|
398 | |
---|
399 | // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/ |
---|
400 | _civilLeapYear:function(/*Number*/year){ |
---|
401 | //summary: return Boolean value if Islamic leap year |
---|
402 | return (14 + 11 * year) % 30 < 11; |
---|
403 | }, |
---|
404 | |
---|
405 | // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/ |
---|
406 | getDaysInIslamicMonth:function(/*Number*/month, /*Number*/ year){ |
---|
407 | //summary: returns the number of days in the given Islamic Month |
---|
408 | var length = 0; |
---|
409 | length = 29 + ((month+1) % 2); |
---|
410 | if(month == 11 && this._civilLeapYear(year)){ |
---|
411 | length++; |
---|
412 | } |
---|
413 | return length; |
---|
414 | }, |
---|
415 | |
---|
416 | _mod:function(a, b){ |
---|
417 | return a - (b * Math.floor(a / b)); |
---|
418 | } |
---|
419 | }); |
---|
420 | |
---|
421 | //TODOC |
---|
422 | dojox.date.islamic.Date.getDaysInIslamicMonth = function(/*dojox.date.islamic.Date*/month){ |
---|
423 | return new dojox.date.islamic.Date().getDaysInIslamicMonth(month.getMonth(),month.getFullYear()); // dojox.date.islamic.Date |
---|
424 | }; |
---|
425 | return dojox.date.islamic.Date; |
---|
426 | }); |
---|