| 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 |
|
| 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 |
_ISLAMIC_EPOCH : 1948439.5, |
| 23 |
|
| 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.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(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.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(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 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 |
|
| 342 |
|
| 343 |
fromGregorian:function(gdate){ |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 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 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
return this.toGregorian().valueOf(); |
| 385 |
}, |
| 386 |
|
| 387 |
|
| 388 |
_yearStart:function(year){ |
| 389 |
|
| 390 |
|
| 391 |
return (year-1)*354 + Math.floor((3+11*year)/30.0); |
| 392 |
}, |
| 393 |
|
| 394 |
|
| 395 |
_monthStart:function(year, month){ |
| 396 |
|
| 397 |
|
| 398 |
return Math.ceil(29.5*month) + |
| 399 |
(year-1)*354 + Math.floor((3+11*year)/30.0); |
| 400 |
}, |
| 401 |
|
| 402 |
|
| 403 |
_civilLeapYear:function(year){ |
| 404 |
|
| 405 |
|
| 406 |
return (14 + 11 * year) % 30 < 11; |
| 407 |
}, |
| 408 |
|
| 409 |
|
| 410 |
getDaysInIslamicMonth:function(month, year){ |
| 411 |
|
| 412 |
|
| 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 |
|
| 427 |
IDate.getDaysInIslamicMonth = function(month){ |
| 428 |
return new IDate().getDaysInIslamicMonth(month.getMonth(),month.getFullYear()); |
| 429 |
}; |
| 430 |
return IDate; |
| 431 |
}); |