Debian Codegraph @

dojox/date/islamic/Date.js

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