| 1 |
define([ |
| 2 |
"dojo/_base/lang", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/date" |
| 5 |
], function(lang, declare, dd){ |
| 6 |
|
| 7 |
var BDate = declare("dojox.date.buddhist.Date", null, { |
| 8 |
|
| 9 |
_date: 0, |
| 10 |
_month: 0, |
| 11 |
_year: 0, |
| 12 |
_hours: 0, |
| 13 |
_minutes: 0, |
| 14 |
_seconds: 0, |
| 15 |
_milliseconds: 0, |
| 16 |
_day: 0, |
| 17 |
|
| 18 |
constructor: function(){ |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
var len = arguments.length; |
| 29 |
if(!len){ |
| 30 |
this.fromGregorian(new Date()); |
| 31 |
}else if(len == 1){ |
| 32 |
var arg0 = arguments[0]; |
| 33 |
if(typeof arg0 == "number"){ |
| 34 |
arg0 = new Date(arg0); |
| 35 |
} |
| 36 |
|
| 37 |
if(arg0 instanceof Date){ |
| 38 |
this.fromGregorian(arg0); |
| 39 |
}else if(arg0 == ""){ |
| 40 |
this._date = new Date(""); |
| 41 |
}else{ |
| 42 |
this._year = arg0._year; |
| 43 |
this._month = arg0._month; |
| 44 |
this._date = arg0._date; |
| 45 |
this._hours = arg0._hours; |
| 46 |
this._minutes = arg0._minutes; |
| 47 |
this._seconds = arg0._seconds; |
| 48 |
this._milliseconds = arg0._milliseconds; |
| 49 |
} |
| 50 |
}else if(len >=3){ |
| 51 |
this._year += arguments[0]; |
| 52 |
this._month += arguments[1]; |
| 53 |
this._date += arguments[2]; |
| 54 |
|
| 55 |
if(this._month >11){ |
| 56 |
console.warn("the month is incorrect , set 0"); |
| 57 |
this._month = 0; |
| 58 |
} |
| 59 |
this._hours += arguments[3] || 0; |
| 60 |
this._minutes += arguments[4] || 0; |
| 61 |
this._seconds += arguments[5] || 0; |
| 62 |
this._milliseconds += arguments[6] || 0; |
| 63 |
} |
| 64 |
}, |
| 65 |
|
| 66 |
getDate: function(isNumber){ |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
return parseInt(this._date); |
| 73 |
}, |
| 74 |
|
| 75 |
getMonth: function(){ |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
return parseInt(this._month); |
| 82 |
}, |
| 83 |
|
| 84 |
|
| 85 |
getFullYear: function(){ |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
return parseInt(this._year); |
| 92 |
}, |
| 93 |
|
| 94 |
getHours: function(){ |
| 95 |
|
| 96 |
|
| 97 |
return this._hours; |
| 98 |
}, |
| 99 |
|
| 100 |
getMinutes: function(){ |
| 101 |
|
| 102 |
|
| 103 |
return this._minutes; |
| 104 |
}, |
| 105 |
|
| 106 |
getSeconds: function(){ |
| 107 |
|
| 108 |
|
| 109 |
return this._seconds; |
| 110 |
}, |
| 111 |
|
| 112 |
getMilliseconds: function(){ |
| 113 |
|
| 114 |
|
| 115 |
return this._milliseconds; |
| 116 |
}, |
| 117 |
|
| 118 |
setDate: function(date){ |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
date = parseInt(date); |
| 125 |
|
| 126 |
if(date > 0 && date <= this._getDaysInMonth(this._month, this._year)){ |
| 127 |
this._date = date; |
| 128 |
}else{ |
| 129 |
var mdays; |
| 130 |
if(date>0){ |
| 131 |
for(mdays = this._getDaysInMonth(this._month, this._year); |
| 132 |
date > mdays; |
| 133 |
date -= mdays,mdays = this._getDaysInMonth(this._month, this._year)){ |
| 134 |
this._month++; |
| 135 |
if(this._month >= 12){this._year++; this._month -= 12;} |
| 136 |
} |
| 137 |
|
| 138 |
this._date = date; |
| 139 |
}else{ |
| 140 |
for(mdays = this._getDaysInMonth((this._month-1)>=0 ?(this._month-1) :11 ,((this._month-1)>=0)? this._year: this._year-1); |
| 141 |
date <= 0; |
| 142 |
mdays = this._getDaysInMonth((this._month-1)>=0 ? (this._month-1) :11,((this._month-1)>=0)? this._year: this._year-1)){ |
| 143 |
this._month--; |
| 144 |
if(this._month < 0){this._year--; this._month += 12;} |
| 145 |
|
| 146 |
date+=mdays; |
| 147 |
} |
| 148 |
this._date = date; |
| 149 |
} |
| 150 |
} |
| 151 |
return this; |
| 152 |
}, |
| 153 |
|
| 154 |
setFullYear: function(year, month, date){ |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
this._year = parseInt(year); |
| 162 |
}, |
| 163 |
|
| 164 |
setMonth: function(month){ |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
this._year += Math.floor(month / 12); |
| 171 |
this._month = Math.floor(month % 12); |
| 172 |
for(; this._month < 0; this._month = this._month+12); |
| 173 |
}, |
| 174 |
|
| 175 |
setHours: function(){ |
| 176 |
|
| 177 |
|
| 178 |
var hours_arg_no = arguments.length; |
| 179 |
var hours = 0; |
| 180 |
if(hours_arg_no >= 1){ |
| 181 |
hours = parseInt(arguments[0]); |
| 182 |
} |
| 183 |
|
| 184 |
if(hours_arg_no >= 2){ |
| 185 |
this._minutes = parseInt(arguments[1]); |
| 186 |
} |
| 187 |
|
| 188 |
if(hours_arg_no >= 3){ |
| 189 |
this._seconds = parseInt(arguments[2]); |
| 190 |
} |
| 191 |
|
| 192 |
if(hours_arg_no == 4){ |
| 193 |
this._milliseconds = parseInt(arguments[3]); |
| 194 |
} |
| 195 |
|
| 196 |
while(hours >= 24){ |
| 197 |
this._date++; |
| 198 |
var mdays = this._getDaysInMonth(this._month, this._year); |
| 199 |
if(this._date > mdays){ |
| 200 |
this._month ++; |
| 201 |
if(this._month >= 12){this._year++; this._month -= 12;} |
| 202 |
this._date -= mdays; |
| 203 |
} |
| 204 |
hours -= 24; |
| 205 |
} |
| 206 |
this._hours = hours; |
| 207 |
}, |
| 208 |
|
| 209 |
_addMinutes: function(minutes){ |
| 210 |
minutes += this._minutes; |
| 211 |
this.setMinutes(minutes); |
| 212 |
this.setHours(this._hours + parseInt(minutes / 60)); |
| 213 |
return this; |
| 214 |
}, |
| 215 |
|
| 216 |
_addSeconds: function(seconds){ |
| 217 |
seconds += this._seconds; |
| 218 |
this.setSeconds(seconds); |
| 219 |
this._addMinutes(parseInt(seconds / 60)); |
| 220 |
return this; |
| 221 |
}, |
| 222 |
|
| 223 |
_addMilliseconds: function(milliseconds){ |
| 224 |
milliseconds += this._milliseconds; |
| 225 |
this.setMilliseconds(milliseconds); |
| 226 |
this._addSeconds(parseInt(milliseconds / 1000)); |
| 227 |
return this; |
| 228 |
}, |
| 229 |
|
| 230 |
setMinutes: function(minutes){ |
| 231 |
|
| 232 |
|
| 233 |
this._minutes = minutes % 60; |
| 234 |
return this; |
| 235 |
}, |
| 236 |
|
| 237 |
setSeconds: function(seconds){ |
| 238 |
|
| 239 |
|
| 240 |
this._seconds = seconds % 60; |
| 241 |
return this; |
| 242 |
}, |
| 243 |
|
| 244 |
setMilliseconds: function(milliseconds){ |
| 245 |
this._milliseconds = milliseconds % 1000; |
| 246 |
return this; |
| 247 |
}, |
| 248 |
|
| 249 |
toString: function(){ |
| 250 |
|
| 251 |
|
| 252 |
return isNaN(this._date)?"Invalid Date": |
| 253 |
this._date + ", " + this._month + ", " + this._year + " " + this._hours + ":" + this._minutes + ":" + this._seconds; |
| 254 |
}, |
| 255 |
|
| 256 |
|
| 257 |
_getDaysInMonth: function(month, year){ |
| 258 |
return dd.getDaysInMonth(new Date(year-543, month)); |
| 259 |
}, |
| 260 |
|
| 261 |
fromGregorian: function(gdate){ |
| 262 |
|
| 263 |
|
| 264 |
var date = new Date(gdate); |
| 265 |
this._date = date.getDate(); |
| 266 |
this._month = date.getMonth(); |
| 267 |
this._year = date.getFullYear()+543; |
| 268 |
this._hours = date.getHours(); |
| 269 |
this._minutes = date.getMinutes(); |
| 270 |
this._seconds = date.getSeconds(); |
| 271 |
this._milliseconds = date.getMilliseconds(); |
| 272 |
this._day = date.getDay(); |
| 273 |
return this; |
| 274 |
}, |
| 275 |
|
| 276 |
toGregorian: function(){ |
| 277 |
|
| 278 |
|
| 279 |
return new Date(this._year-543, this._month, this._date, this._hours, this._minutes, this._seconds, this._milliseconds); |
| 280 |
}, |
| 281 |
|
| 282 |
getDay: function(){ |
| 283 |
|
| 284 |
|
| 285 |
return this.toGregorian().getDay(); |
| 286 |
} |
| 287 |
}); |
| 288 |
|
| 289 |
BDate.prototype.valueOf = function(){ |
| 290 |
return this.toGregorian().valueOf(); |
| 291 |
}; |
| 292 |
|
| 293 |
return BDate; |
| 294 |
}); |