| 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 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
var len = arguments.length; |
| 36 |
if(!len){ |
| 37 |
this.fromGregorian(new Date()); |
| 38 |
}else if(len == 1){ |
| 39 |
var arg0 = arguments[0]; |
| 40 |
if(typeof arg0 == "number"){ |
| 41 |
arg0 = new Date(arg0); |
| 42 |
} |
| 43 |
|
| 44 |
if(arg0 instanceof Date){ |
| 45 |
this.fromGregorian(arg0); |
| 46 |
}else if(arg0 == ""){ |
| 47 |
|
| 48 |
this._date = new Date(""); |
| 49 |
}else{ |
| 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 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
return this._date; |
| 77 |
}, |
| 78 |
|
| 79 |
getMonth:function(){ |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
return this._month; |
| 87 |
}, |
| 88 |
|
| 89 |
getFullYear:function(){ |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
return this._year; |
| 97 |
}, |
| 98 |
|
| 99 |
getDay:function(){ |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
return this.toGregorian().getDay(); |
| 107 |
}, |
| 108 |
|
| 109 |
getHours:function(){ |
| 110 |
|
| 111 |
|
| 112 |
return this._hours; |
| 113 |
}, |
| 114 |
|
| 115 |
getMinutes:function(){ |
| 116 |
|
| 117 |
|
| 118 |
return this._minutes; |
| 119 |
}, |
| 120 |
|
| 121 |
getSeconds:function(){ |
| 122 |
|
| 123 |
|
| 124 |
return this._seconds; |
| 125 |
}, |
| 126 |
|
| 127 |
getMilliseconds:function(){ |
| 128 |
|
| 129 |
|
| 130 |
return this._milliseconds; |
| 131 |
}, |
| 132 |
|
| 133 |
setDate: function(date){ |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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(year){ |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
this._year = +year; |
| 178 |
}, |
| 179 |
|
| 180 |
setMonth: function(month) { |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 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 |
|
| 197 |
|
| 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(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(seconds){ |
| 237 |
seconds += this._seconds; |
| 238 |
this.setSeconds(seconds); |
| 239 |
this._addMinutes(parseInt(seconds / 60)); |
| 240 |
return this; |
| 241 |
}, |
| 242 |
|
| 243 |
_addMilliseconds: function(milliseconds){ |
| 244 |
milliseconds += this._milliseconds; |
| 245 |
this.setMilliseconds(milliseconds); |
| 246 |
this._addSeconds(parseInt(milliseconds / 1000)); |
| 247 |
return this; |
| 248 |
}, |
| 249 |
|
| 250 |
setMinutes: function(minutes){ |
| 251 |
|
| 252 |
|
| 253 |
this._minutes = minutes % 60; |
| 254 |
return this; |
| 255 |
}, |
| 256 |
|
| 257 |
setSeconds: function(seconds){ |
| 258 |
|
| 259 |
|
| 260 |
this._seconds = seconds % 60; |
| 261 |
return this; |
| 262 |
}, |
| 263 |
|
| 264 |
setMilliseconds: function(milliseconds){ |
| 265 |
this._milliseconds = milliseconds % 1000; |
| 266 |
return this; |
| 267 |
}, |
| 268 |
|
| 269 |
toString:function(){ |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 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 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 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 |
|
| 308 |
fromGregorian:function(gdate){ |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 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 |
|
| 330 |
calcGregorian:function (year,month,day){ |
| 331 |
var j, weekday; |
| 332 |
|
| 333 |
j = this.gregorian_to_jd(year, month + 1, day) +(Math.floor(0 + 60 * (0 + 60 * 0) + 0.5) / 86400.0); |
| 334 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
return this.toGregorian().valueOf(); |
| 403 |
},jwday: function (j) |
| 404 |
{ |
| 405 |
return this._mod(Math.floor((j + 1.5)), 7); |
| 406 |
}, |
| 407 |
|
| 408 |
|
| 409 |
_yearStart:function(year){ |
| 410 |
|
| 411 |
|
| 412 |
return (year-1)*354 + Math.floor((3+11*year)/30.0); |
| 413 |
}, |
| 414 |
|
| 415 |
|
| 416 |
_monthStart:function(year, month){ |
| 417 |
|
| 418 |
|
| 419 |
return Math.ceil(29.5*month) + |
| 420 |
(year-1)*354 + Math.floor((3+11*year)/30.0); |
| 421 |
}, |
| 422 |
|
| 423 |
leap_gregorian: function (year) |
| 424 |
{ |
| 425 |
return ((year % 4) == 0) && |
| 426 |
(!(((year % 100) == 0) && ((year % 400) != 0))); |
| 427 |
}, |
| 428 |
|
| 429 |
|
| 430 |
isLeapYear:function(j_y,j_m,j_d){ |
| 431 |
|
| 432 |
|
| 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 |
|
| 438 |
getDaysInPersianMonth:function(month, year){ |
| 439 |
|
| 440 |
|
| 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(month){ |
| 455 |
return new IDate().getDaysInPersianMonth(month.getMonth(),month.getFullYear()); |
| 456 |
}; |
| 457 |
return IDate; |
| 458 |
}); |