Debian Codegraph @

dojox/date/persian/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.persian.Date", null, {
4 // summary:
5 // The component defines the Persian (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.persian.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 _PERSIAN_EPOCH : 1948320.5,
23 daysInMonth:[31,31,31,31,31,31,30,30,30,30,30,29 ],
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.persian.Date();
31 // | var date2 = new dojox.date.persian.Date("12\2\1429");
32 // | var date3 = new dojox.date.persian.Date(date2);
33 // | var date4 = new dojox.date.persian.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 Persian.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.persian.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.persian.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.persian.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.persian.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.persian.Date();
138 // | date1.setDate(2);
139
140 date = parseInt(date);
141
142 if(date > 0 && date <= this.getDaysInPersianMonth(this._month, this._year)){
143 this._date = date;
144 }else{
145 var mdays;
146 if(date>0){
147 for(mdays = this.getDaysInPersianMonth(this._month, this._year);
148 date > mdays;
149 date -= mdays,mdays =this.getDaysInPersianMonth(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.getDaysInPersianMonth((this._month-1)>=0 ?(this._month-1) :11 ,((this._month-1)>=0)? this._year: this._year-1);
157 date <= 0;
158 mdays = this.getDaysInPersianMonth((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.persian.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.persian.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.getDaysInPersianMonth(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.persian.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 datePersian = new dojox.date.persian.Date(1429,11,20);
295 // | var dateGregorian = datePersian.toGregorian();
296
297 var hYear = this._year;
298 var date,j;
299 j = this.persian_to_jd(this._year,this._month+1,this._date);
300 date = this.jd_to_gregorian(j,this._month+1);
301 weekday = this.jwday(j);
302 var _21=new Date(date[0],date[1]-1,date[2],this._hours,this._minutes,this._seconds,this._milliseconds);
303 return _21;
304 },
305
306
307 // ported from the Java class com.ibm.icu.util.PersianCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
308 fromGregorian:function(/*Date*/gdate){
309 // summary:
310 // This function returns the equivalent Persian Date value for the Gregorian Date
311 // example:
312 // | var datePersian = new dojox.date.persian.Date();
313 // | var dateGregorian = new Date(2008,10,12);
314 // | datePersian.fromGregorian(dateGregorian);
315
316 var _23=new Date(gdate);
317 var _24=_23.getFullYear(),_25=_23.getMonth(),_26=_23.getDate();
318 var persian = this.calcGregorian(_24,_25,_26);
319 this._date=persian[2];
320 this._month=persian[1];
321 this._year=persian[0];
322 this._hours=_23.getHours();
323 this._minutes=_23.getMinutes();
324 this._seconds=_23.getSeconds();
325 this._milliseconds=_23.getMilliseconds();
326 this._day=_23.getDay();
327 return this;
328 },
329 // calcGregorian -- Perform calculation starting with a Gregorian date
330 calcGregorian:function (year,month,day){
331 var j, weekday;
332 // Update Julian day
333 j = this.gregorian_to_jd(year, month + 1, day) +(Math.floor(0 + 60 * (0 + 60 * 0) + 0.5) / 86400.0);
334 // Update Persian Calendar
335 perscal = this.jd_to_persian(j);
336 weekday = this.jwday(j);
337 return new Array(perscal[0], perscal[1], perscal[2],weekday);
338 },
339 // JD_TO_PERSIAN -- Calculate Persian date from Julian day
340 jd_to_persian: function (jd){
341 var year, month, day, depoch, cycle, cyear, ycycle, aux1, aux2, yday;
342 jd = Math.floor(jd) + 0.5;
343 depoch = jd - this.persian_to_jd(475, 1, 1);
344 cycle = Math.floor(depoch / 1029983);
345 cyear = this._mod(depoch, 1029983);
346 if (cyear == 1029982) {
347 ycycle = 2820;
348 } else {
349 aux1 = Math.floor(cyear / 366);
350 aux2 = this._mod(cyear, 366);
351 ycycle = Math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1;
352 }
353 year = ycycle + (2820 * cycle) + 474;
354 if (year <= 0) {
355 year--;
356 }
357 yday = (jd - this.persian_to_jd(year, 1, 1)) + 1;
358 month = (yday <= 186) ? Math.ceil(yday / 31) : Math.ceil((yday - 6) / 30);
359 day = (jd - this.persian_to_jd(year, month, 1)) + 1;
360
361 return new Array(year, month-1, day);
362 },
363 // PERSIAN_TO_JD -- Determine Julian day from Persian date
364 persian_to_jd: function (year, month, day){
365 var epbase, epyear;
366 epbase = year - ((year >= 0) ? 474 : 473);
367 epyear = 474 + this._mod(epbase, 2820);
368 return day +((month <= 7) ?((month - 1) * 31) :(((month - 1) * 30) + 6)) + Math.floor(((epyear * 682) - 110) / 2816) +(epyear - 1) * 365 + Math.floor(epbase / 2820) * 1029983 +(this._PERSIAN_EPOCH - 1);
369 },
370 // GREGORIAN_TO_JD -- Determine Julian day number from Gregorian calendar date
371 gregorian_to_jd: function (year, month, day){
372 return (this._GREGORIAN_EPOCH - 1) + (365 * (year - 1)) + Math.floor((year - 1) / 4) +(-Math.floor((year - 1) / 100)) + Math.floor((year - 1) / 400) + Math.floor((((367 * month) - 362) / 12) +
373 ((month <= 2) ? 0 :(this.leap_gregorian(year) ? -1 : -2)) +day);
374 },
375
376 // JD_TO_GREGORIAN -- Calculate Gregorian calendar date from Julian day
377 jd_to_gregorian : function (jd,jmonth) {
378 var wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad, yindex, dyindex, year, yearday, leapadj;
379 wjd = Math.floor(jd - 0.5) + 0.5;
380 depoch = wjd - this._GREGORIAN_EPOCH;
381 quadricent = Math.floor(depoch / 146097);
382 dqc = this._mod(depoch, 146097);
383 cent = Math.floor(dqc / 36524);
384 dcent = this._mod(dqc, 36524);
385 quad = Math.floor(dcent / 1461);
386 dquad = this._mod(dcent, 1461);
387 yindex = Math.floor(dquad / 365);
388 year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex;
389 if (!((cent == 4) || (yindex == 4))) {
390 year++;
391 }
392 yearday = wjd - this.gregorian_to_jd(year, 1, 1);
393 leapadj = ((wjd < this.gregorian_to_jd(year, 3, 1)) ? 0:(this.leap_gregorian(year) ? 1 : 2));
394 month = Math.floor((((yearday + leapadj) * 12) + 373) / 367);
395 day = (wjd - this.gregorian_to_jd(year, month, 1)) + 1;
396 return new Array(year, month, day);
397 }, valueOf:function(){
398 // summary:
399 // This function returns The stored time value in milliseconds
400 // since midnight, January 1, 1970 UTC
401
402 return this.toGregorian().valueOf();
403 },jwday: function (j)
404 {
405 return this._mod(Math.floor((j + 1.5)), 7);
406 },
407
408 // ported from the Java class com.ibm.icu.util.PersianCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
409 _yearStart:function(/*Number*/year){
410 // summary:
411 // return start of Persian year
412 return (year-1)*354 + Math.floor((3+11*year)/30.0);
413 },
414
415 // ported from the Java class com.ibm.icu.util.PersianCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
416 _monthStart:function(/*Number*/year, /*Number*/month){
417 // summary:
418 // return the start of Persian Month
419 return Math.ceil(29.5*month) +
420 (year-1)*354 + Math.floor((3+11*year)/30.0);
421 },
422 // LEAP_GREGORIAN -- Is a given year in the Gregorian calendar a leap year ?
423 leap_gregorian: function (year)
424 {
425 return ((year % 4) == 0) &&
426 (!(((year % 100) == 0) && ((year % 400) != 0)));
427 },
428
429 // LEAP_PERSIAN -- Is a given year a leap year in the Persian calendar ?
430 isLeapYear:function(j_y,j_m,j_d){
431 // summary:
432 // return Boolean value if Persian leap year
433 return !(j_y < 0 || j_y > 32767 || j_m < 1 || j_m > 12 || j_d < 1 || j_d >(this.daysInMonth[j_m-1] + (j_m == 12 && !((j_y-979)%33%4))));
434
435 },
436
437 // ported from the Java class com.ibm.icu.util.PersianCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
438 getDaysInPersianMonth:function(/*Number*/month, /*Number*/ year){
439 // summary:
440 // returns the number of days in the given Persian Month
441 var days=this.daysInMonth[month];
442 if(month==11 && this.isLeapYear(year,month+1,30)){
443 days++;
444 }
445 return days;
446 },
447
448 _mod:function(a, b){
449 return a - (b * Math.floor(a / b));
450 }
451 });
452
453
454 IDate.getDaysInPersianMonth = function(/*dojox/date/persian.Date*/month){
455 return new IDate().getDaysInPersianMonth(month.getMonth(),month.getFullYear()); // dojox.date.persian.Date
456 };
457 return IDate;
458 });