Debian Codegraph @

dojox/date/umalqura/Date.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/declare", "dojo/date", "../islamic/Date"],
2 function(lang, declare, dd){
3
4 var IDate = declare("dojox.date.umalqura.Date", null, {
5
6 // summary:
7 // The component defines the UmAlqura (Hijri) Calendar Object according to Umalqura calculations
8 // This module is similar to the Date() object provided by JavaScript
9 // example:
10 // | var date = new dojox.date.umalqura.Date();
11 // | document.writeln(date.getFullYear()+'\'+date.getMonth()+'\'+date.getDate());
12
13 _MONTH_LENGTH: [
14 //1300-1304
15 "101010101010", "110101010100", "111011001001", "011011010100", "011011101010",
16 //1305-1309
17 "001101101100", "101010101101", "010101010101", "011010101001", "011110010010",
18 //1310-1314
19 "101110101001", "010111010100", "101011011010", "010101011100", "110100101101",
20 //1315-1319
21 "011010010101", "011101001010", "101101010100", "101101101010", "010110101101",
22 //1320-1324
23 "010010101110", "101001001111", "010100010111", "011010001011", "011010100101",
24 //1325-1329
25 "101011010101", "001011010110", "100101011011", "010010011101", "101001001101",
26 //1330-1334
27 "110100100110", "110110010101", "010110101100", "100110110110", "001010111010",
28 //1335-1339
29 "101001011011", "010100101011", "101010010101", "011011001010", "101011101001",
30 //1340-1344
31 "001011110100", "100101110110", "001010110110", "100101010110", "101011001010",
32 //1345-1349
33 "101110100100", "101111010010", "010111011001", "001011011100", "100101101101",
34 //1350-1354
35 "010101001101", "101010100101", "101101010010", "101110100101", "010110110100",
36 //1355-1359
37 "100110110110", "010101010111", "001010010111", "010101001011", "011010100011",
38 //1360-1364
39 "011101010010", "101101100101", "010101101010", "101010101011", "010100101011",
40 //1365-1369
41 "110010010101", "110101001010", "110110100101", "010111001010", "101011010110",
42 //1370-1374
43 "100101010111", "010010101011", "100101001011", "101010100101", "101101010010",
44 //1375-1379
45 "101101101010", "010101110101", "001001110110", "100010110111", "010001011011",
46 //1380-1384
47 "010101010101", "010110101001", "010110110100", "100111011010", "010011011101",
48 //1385-1389
49 "001001101110", "100100110110", "101010101010", "110101010100", "110110110010",
50 //1390-1394
51 "010111010101", "001011011010", "100101011011", "010010101011", "101001010101",
52 //1395-1399
53 "101101001001", "101101100100", "101101110001", "010110110100", "101010110101",
54 //1400-1404
55 "101001010101", "110100100101", "111010010010", "111011001001", "011011010100",
56 //1405-1409
57 "101011101001", "100101101011", "010010101011", "101010010011", "110101001001",
58 //1410-1414
59 "110110100100", "110110110010", "101010111001", "010010111010", "101001011011",
60 //1415-1419
61 "010100101011", "101010010101", "101100101010", "101101010101", "010101011100",
62 //1420-1424
63 "010010111101", "001000111101", "100100011101", "101010010101", "101101001010",
64 //1425-1429
65 "101101011010", "010101101101", "001010110110", "100100111011", "010010011011",
66 //1430-1434
67 "011001010101", "011010101001", "011101010100", "101101101010", "010101101100",
68 //1435-1439
69 "101010101101", "010101010101", "101100101001", "101110010010", "101110101001",
70 //1440-1444
71 "010111010100", "101011011010", "010101011010", "101010101011", "010110010101",
72 //1445-1449
73 "011101001001", "011101100100", "101110101010", "010110110101", "001010110110",
74 //1450-1454
75 "101001010110", "111001001101", "101100100101", "101101010010", "101101101010",
76 //1455-1459
77 "010110101101", "001010101110", "100100101111", "010010010111", "011001001011",
78 //1460-1464
79 "011010100101", "011010101100", "101011010110", "010101011101", "010010011101",
80 //1465-1469
81 "101001001101", "110100010110", "110110010101", "010110101010", "010110110101",
82 //1470-1474
83 "001011011010", "100101011011", "010010101101", "010110010101", "011011001010",
84 //1475-1479
85 "011011100100", "101011101010", "010011110101", "001010110110", "100101010110",
86 //1480-1484
87 "101010101010", "101101010100", "101111010010", "010111011001", "001011101010",
88 //1485-1489
89 "100101101101", "010010101101", "101010010101", "101101001010", "101110100101",
90 //1490-1494
91 "010110110010", "100110110101", "010011010110", "101010010111", "010101000111",
92 //1495-1499
93 "011010010011", "011101001001", "101101010101", "010101101010", "101001101011",
94 //1500-1504
95 "010100101011", "101010001011", "110101000110", "110110100011", "010111001010",
96 //1505-1509
97 "101011010110", "010011011011", "001001101011", "100101001011", "101010100101",
98 //1510-1514
99 "101101010010", "101101101001", "010101110101", "000101110110", "100010110111",
100 //1515-1519
101 "001001011011", "010100101011", "010101100101", "010110110100", "100111011010",
102 //1520-1524
103 "010011101101", "000101101101", "100010110110", "101010100110", "110101010010",
104 //1525-1529
105 "110110101001", "010111010100", "101011011010", "100101011011", "010010101011",
106 //1530-1534
107 "011001010011", "011100101001", "011101100010", "101110101001", "010110110010",
108 //1535-1539
109 "101010110101", "010101010101", "101100100101", "110110010010", "111011001001",
110 //1540-1544
111 "011011010010", "101011101001", "010101101011", "010010101011", "101001010101",
112 //1545-1549
113 "110100101001", "110101010100", "110110101010", "100110110101", "010010111010",
114 //1550-1554
115 "101000111011", "010010011011", "101001001101", "101010101010", "101011010101",
116 //1555-1559
117 "001011011010", "100101011101", "010001011110", "101000101110", "110010011010",
118 //1560-1564
119 "110101010101", "011010110010", "011010111001", "010010111010", "101001011101",
120 //1565-1569
121 "010100101101", "101010010101", "101101010010", "101110101000", "101110110100",
122 //1570-1574
123 "010110111001", "001011011010", "100101011010", "101101001010", "110110100100",
124 //1575-1579
125 "111011010001", "011011101000", "101101101010", "010101101101", "010100110101",
126 //1580-1584
127 "011010010101", "110101001010", "110110101000", "110111010100", "011011011010",
128 //1585-1589
129 "010101011011", "001010011101", "011000101011", "101100010101", "101101001010",
130 //1590-1594
131 "101110010101", "010110101010", "101010101110", "100100101110", "110010001111",
132 //1595-1599
133 "010100100111", "011010010101", "011010101010", "101011010110", "010101011101",
134 //1600
135 "001010011101"],
136
137 _hijriBegin: 1300,
138 _hijriEnd: 1600,
139 _date: 0,
140 _month: 0,
141 _year: 0,
142 _hours: 0,
143 _minutes: 0,
144 _seconds: 0,
145 _milliseconds: 0,
146 _day: 0,
147
148 constructor: function(){
149 // summary:
150 // This function initialize the date object values
151
152 var len = arguments.length;
153 if(!len){// use the current date value, added "" to the similarity to date
154 this.fromGregorian(new Date());
155 }else if(len == 1){
156 var arg0 = arguments[0];
157 if(typeof arg0 == "number"){ // this is time "valueof"
158 arg0 = new Date(arg0);
159 }
160
161 if(arg0 instanceof Date){
162 this.fromGregorian(arg0);
163 }else if(arg0 == ""){
164 // date should be invalid. Dijit relies on this behavior.
165 this._date = new Date(""); //TODO: should this be NaN? _date is not a Date object
166 }else{ // this is umalqura.Date object
167 this._year = arg0._year;
168 this._month = arg0._month;
169 this._date = arg0._date;
170 this._hours = arg0._hours;
171 this._minutes = arg0._minutes;
172 this._seconds = arg0._seconds;
173 this._milliseconds = arg0._milliseconds;
174 }
175 }else if(len >= 3){
176 // YYYY MM DD arguments passed, month is from 0-12
177 this._year += arguments[0];
178 this._month += arguments[1];
179 this._date += arguments[2];
180 this._hours += arguments[3] || 0;
181 this._minutes += arguments[4] || 0;
182 this._seconds += arguments[5] || 0;
183 this._milliseconds += arguments[6] || 0;
184 }
185 },
186
187 getDate: function(){
188 // summary:
189 // This function returns the date value (1 - 30)
190 // example:
191 // | var date1 = new dojox.date.umalqura.Date();
192 // |
193 // | document.writeln(date1.getDate);
194 return this._date;
195 },
196
197 getMonth: function(){
198 // summary:
199 // This function return the month value ( 0 - 11 )
200 // example:
201 // | var date1 = new dojox.date.umalqura.Date();
202 // |
203 // | document.writeln(date1.getMonth()+1);
204
205 return this._month;
206 },
207
208 getFullYear: function(){
209 // summary:
210 // This function return the year value
211 // example:
212 // | var date1 = new dojox.date.umalqura.Date();
213 // |
214 // | document.writeln(date1.getFullYear());
215
216 return this._year;
217 },
218
219 getDay: function(){
220 // summary:
221 // This function returns the week day value ( 0 - 6 )
222 // sunday is 0, monday is 1,...etc
223 // example:
224 // | var date1 = new dojox.date.umalqura.Date();
225 // |
226 // | document.writeln(date1.getDay());
227 var d = this.toGregorian();
228 var dd = d.getDay();
229 return dd;
230 },
231
232 getHours: function(){
233 // summary:
234 // returns the hour value
235 return this._hours;
236 },
237
238 getMinutes: function(){
239 // summary:
240 // returns the minutes value
241 return this._minutes;
242 },
243
244 getSeconds: function(){
245 // summary:
246 // returns the seconds value
247 return this._seconds;
248 },
249
250 getMilliseconds: function(){
251 // summary:
252 // returns the milliseconds value
253 return this._milliseconds;
254 },
255
256 setDate: function(/*number*/ date){
257 // summary:
258 // This function sets the date
259 // example:
260 // | var date1 = new dojox.date.umalqura.Date();
261 // | date1.setDate(2);
262
263 date = parseInt(date);
264
265 if(date > 0 && date <= this.getDaysInIslamicMonth(this._month, this._year)){
266 this._date = date;
267 }else{
268 var mdays;
269 if(date > 0){
270 for(mdays = this.getDaysInIslamicMonth(this._month, this._year);
271 date > mdays;
272 date -= mdays, mdays = this.getDaysInIslamicMonth(this._month, this._year)){
273 this._month++;
274 if(this._month >= 12){ this._year++; this._month -= 12; }
275 }
276
277 this._date = date;
278 }else{
279 for(mdays = this.getDaysInIslamicMonth((this._month - 1) >= 0 ? (this._month - 1) : 11, ((this._month - 1) >= 0) ? this._year : this._year - 1);
280 date <= 0;
281 mdays = this.getDaysInIslamicMonth((this._month - 1) >= 0 ? (this._month - 1) : 11, ((this._month - 1) >= 0) ? this._year : this._year - 1)){
282 this._month--;
283 if(this._month < 0){ this._year--; this._month += 12; }
284
285 date += mdays;
286 }
287 this._date = date;
288 }
289 }
290 return this;
291 },
292
293 setFullYear: function(/*number*/ year){
294 // summary:
295 // This function set Year
296 // example:
297 // | var date1 = new dojox.date.umalqura.Date();
298 // | date1.setYear(1429);
299
300 this._year = +year;
301 },
302
303 setMonth: function(/*number*/ month){
304 // summary:
305 // This function sets the month
306 // example:
307 // | var date1 = new dojox.date.umalqura.Date();
308 // | date1.setMonth(2);
309
310 this._year += Math.floor(month / 12);
311 if(month > 0){
312 this._month = Math.floor(month % 12);
313 }else{
314 this._month = Math.floor(((month % 12) + 12) % 12);
315 }
316 },
317
318 setHours: function(){
319 // summary:
320 // set the hours
321 var hours_arg_no = arguments.length;
322 var hours = 0;
323 if(hours_arg_no >= 1){
324 hours = parseInt(arguments[0]);
325 }
326
327 if(hours_arg_no >= 2){
328 this._minutes = parseInt(arguments[1]);
329 }
330
331 if(hours_arg_no >= 3){
332 this._seconds = parseInt(arguments[2]);
333 }
334
335 if(hours_arg_no == 4){
336 this._milliseconds = parseInt(arguments[3]);
337 }
338
339 while (hours >= 24){
340 this._date++;
341 var mdays = this.getDaysInIslamicMonth(this._month, this._year);
342 if(this._date > mdays){
343 this._month++;
344 if(this._month >= 12){ this._year++; this._month -= 12; }
345 this._date -= mdays;
346 }
347 hours -= 24;
348 }
349 this._hours = hours;
350 },
351
352 _addMinutes: function(/*Number*/ minutes){
353 minutes += this._minutes;
354 this.setMinutes(minutes);
355 this.setHours(this._hours + parseInt(minutes / 60));
356 return this;
357 },
358
359 _addSeconds: function(/*Number*/ seconds){
360 seconds += this._seconds;
361 this.setSeconds(seconds);
362 this._addMinutes(parseInt(seconds / 60));
363 return this;
364 },
365
366 _addMilliseconds: function(/*Number*/ milliseconds){
367 milliseconds += this._milliseconds;
368 this.setMilliseconds(milliseconds);
369 this._addSeconds(parseInt(milliseconds / 1000));
370 return this;
371 },
372
373 setMinutes: function(/*number*/ minutes){
374 // summary:
375 // set the minutes
376
377 while (minutes >= 60){
378 this._hours++;
379 if(this._hours >= 24){
380 this._date++;
381 this._hours -= 24;
382 var mdays = this.getDaysInIslamicMonth(this._month, this._year);
383 if(this._date > mdays){
384 this._month++;
385 if(this._month >= 12){ this._year++; this._month -= 12; }
386 this._date -= mdays;
387 }
388 }
389 minutes -= 60;
390 }
391 this._minutes = minutes;
392 },
393
394
395 setSeconds: function(/*number*/ seconds){
396 // summary:
397 // set seconds
398 while (seconds >= 60){
399 this._minutes++;
400 if(this._minutes >= 60){
401 this._hours++;
402 this._minutes -= 60;
403 if(this._hours >= 24){
404 this._date++;
405 this._hours -= 24;
406 var mdays = this.getDaysInIslamicMonth(this._month, this._year);
407 if(this._date > mdays){
408 this._month++;
409 if(this._month >= 12){ this._year++; this._month -= 12; }
410 this._date -= mdays;
411 }
412 }
413 }
414 seconds -= 60;
415 }
416 this._seconds = seconds;
417 },
418
419 setMilliseconds: function(/*number*/ milliseconds){
420 // summary:
421 // set the milliseconds
422 while (milliseconds >= 1000){
423 this.setSeconds++;
424 if(this.setSeconds >= 60){
425 this._minutes++;
426 this.setSeconds -= 60;
427 if(this._minutes >= 60){
428 this._hours++;
429 this._minutes -= 60;
430 if(this._hours >= 24){
431 this._date++;
432 this._hours -= 24;
433 var mdays = this.getDaysInIslamicMonth(this._month, this._year);
434 if(this._date > mdays){
435 this._month++;
436 if(this._month >= 12){ this._year++; this._month -= 12; }
437 this._date -= mdays;
438 }
439 }
440 }
441 }
442 milliseconds -= 1000;
443 }
444 this._milliseconds = milliseconds;
445 },
446
447
448 toString: function(){
449 // summary:
450 // This returns a string representation of the date in "DDDD MMMM DD YYYY HH:MM:SS" format
451 // example:
452 // | var date1 = new dojox.date.umalqura.Date();
453 // | document.writeln(date1.toString());
454
455 //FIXME: TZ/DST issues?
456 var x = new Date();
457 x.setHours(this._hours);
458 x.setMinutes(this._minutes);
459 x.setSeconds(this._seconds);
460 x.setMilliseconds(this._milliseconds);
461 return this._month + " " + this._date + " " + this._year + " " + x.toTimeString();
462 },
463
464
465 toGregorian: function(){
466 // summary:
467 // This returns the equivalent gregorian date value in Date object
468 // example:
469 // | var dateIslamic = new dojox.date.umalqura.Date(1429,11,20);
470 // | var dateGregorian = dateIslamic.toGregorian();
471
472 var hYear = this._year;
473 var hMonth = this._month;
474 var hDate = this._date;
475 var gdate = new Date();
476 var dayDiff = hDate - 1;
477 var gregorianFirstRef = new Date(1882, 10, 12, this._hours, this._minutes, this._seconds, this._milliseconds);
478 if(hYear >= this._hijriBegin && hYear <= this._hijriEnd){
479 for (var y = 0; y < hYear - this._hijriBegin; y++){
480 for(var m = 0; m < 12; m++){
481 dayDiff += parseInt(this._MONTH_LENGTH[y][m], 10) + 29;
482 }
483 }
484 for(m = 0; m < hMonth; m++){
485 dayDiff += parseInt(this._MONTH_LENGTH[hYear - this._hijriBegin][m], 10) + 29;
486 }
487 gdate = dd.add(gregorianFirstRef, "day", dayDiff);
488
489 } else{
490 var islamicDate = new dojox.date.islamic.Date(this._year, this._month, this._date, this._hours, this._minutes, this._seconds, this._milliseconds);
491 gdate = new Date(islamicDate.toGregorian());
492 }
493 return gdate;
494 },
495
496 getDaysDiff: function (date1, date2) {
497 // summary:
498 // This function returns the number of days between two different dates.
499 var ONE_DAY = 1000 * 60 * 60 * 24;
500 var diff = Math.abs(date1.getTime() - date2.getTime());
501 return Math.round(diff / ONE_DAY);
502 },
503
504 //TODO: would it make more sense to make this a constructor option? or a static?
505 fromGregorian: function(/*Date*/ gdate){
506 // summary:
507 // This function returns the equivalent UmAlqura Date value for the Gregorian Date
508 // example:
509 // | var dateIslamic = new dojox.date.umalqura.Date();
510 // | var dateGregorian = new Date(2008,10,12);
511 // | dateIslamic.fromGregorian(dateGregorian);
512
513 var date = new Date(gdate);
514 var gregorianFirstRef = new Date(1882, 10, 12, 0, 0, 0, 0);
515 var gregorianLastRef = new Date(2174, 10, 25, 23, 59, 59, 999);
516 var daysDiff = this.getDaysDiff(date, gregorianFirstRef);
517 if (date - gregorianFirstRef >= 0 && date - gregorianLastRef <= 0) {
518 var year = 1300;
519 for (var i = 0; i < this._MONTH_LENGTH.length; i++, year++) {
520 for (var j = 0; j < 12; j++) {
521 var numOfDays = parseInt(this._MONTH_LENGTH[i][j], 10) + 29;
522 if (daysDiff <= numOfDays) {
523 this._date = daysDiff + 1;
524 if (this._date > numOfDays) {
525 this._date = 1;
526 j++;
527 }
528 if (j > 11) {
529 j = 0;
530 year++;
531 }
532 this._month = j;
533 this._year = year;
534 this._hours = date.getHours();
535 this._minutes = date.getMinutes();
536 this._seconds = date.getSeconds();
537 this._milliseconds = date.getMilliseconds();
538 this._day = date.getDay();
539 return this;
540 }
541 daysDiff = parseInt(daysDiff, 10) - numOfDays;
542 }
543 }
544 } else {
545 var islamicDate = new dojox.date.islamic.Date(date);
546 this._date = islamicDate.getDate();
547 this._month = islamicDate.getMonth();
548 this._year = islamicDate.getFullYear();
549 this._hours = gdate.getHours();
550 this._minutes = gdate.getMinutes();
551 this._seconds = gdate.getSeconds();
552 this._milliseconds = gdate.getMilliseconds();
553 this._day = gdate.getDay();
554 }
555 return this;
556 },
557
558 valueOf: function(){
559 // summary:
560 // This function returns the stored time value in milliseconds
561 // since midnight, January 1, 1970 UTC
562
563 return (this.toGregorian()).valueOf();
564 },
565
566 // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
567 _yearStart: function(/*Number*/year){
568 // summary:
569 // return start of Islamic year
570 return (year - 1) * 354 + Math.floor((3 + 11 * year) / 30.0); //1078
571 },
572
573 // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
574 _monthStart: function(/*Number*/year, /*Number*/month){
575 // summary:
576 // return the start of Islamic Month
577 return Math.ceil(29.5 * month) +
578 (year - 1) * 354 + Math.floor((3 + 11 * year) / 30.0);
579 },
580
581 // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
582 _civilLeapYear: function(/*Number*/year){
583 // summary:
584 // return Boolean value if Islamic leap year
585 return (14 + 11 * year) % 30 < 11;
586 },
587
588
589 getDaysInIslamicMonth: function(/*Number*/ month, /*Number*/ year){
590 // summary:
591 // returns the number of days in the given Islamic month
592 if(year >= this._hijriBegin && year <= this._hijriEnd){
593 var pos = year - this._hijriBegin;
594 var length = 0;
595 if(this._MONTH_LENGTH[pos].charAt(month) == 1)
596 length = 30;
597 else length = 29;
598 }else{
599 var islamicDate = new dojox.date.islamic.Date();
600 length = islamicDate.getDaysInIslamicMonth(month, year);
601 }
602 return length;
603 }
604 });
605
606 //TODOC
607 IDate.getDaysInIslamicMonth = function(/*dojox.date.umalqura.Date*/month){
608 return new IDate().getDaysInIslamicMonth(month.getMonth(),month.getFullYear()); // dojox.date.islamic.Date
609 };
610
611 return IDate;
612 });
613