| 1 |
package { |
|---|
| 2 |
|
|---|
| 3 |
import flash.utils.Proxy; |
|---|
| 4 |
import flash.utils.flash_proxy; |
|---|
| 5 |
|
|---|
| 6 |
/** |
|---|
| 7 |
* SmartDate |
|---|
| 8 |
* @author tanabe |
|---|
| 9 |
*/ |
|---|
| 10 |
public dynamic class SmartDate extends Proxy { |
|---|
| 11 |
|
|---|
| 12 |
public static const SUNDAY:String = "sunday"; |
|---|
| 13 |
public static const MONDAY:String = "monday"; |
|---|
| 14 |
public static const TUESDAY:String = "tuesday"; |
|---|
| 15 |
public static const WEDNESDAY:String = "wednesday"; |
|---|
| 16 |
public static const THURSDAY:String = "thursday"; |
|---|
| 17 |
public static const FRIDAY:String = "friday"; |
|---|
| 18 |
public static const SATURDAY:String = "saturday"; |
|---|
| 19 |
|
|---|
| 20 |
private static var DAY_OF_WEEKS:Array = [SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]; |
|---|
| 21 |
private var _date:Date; |
|---|
| 22 |
|
|---|
| 23 |
/** |
|---|
| 24 |
* コンストラクタ |
|---|
| 25 |
* @param args Dateクラスの引数 |
|---|
| 26 |
*/ |
|---|
| 27 |
public function SmartDate(... args) { |
|---|
| 28 |
switch (args.length) { |
|---|
| 29 |
case 1: |
|---|
| 30 |
_date = new Date(args[0]); |
|---|
| 31 |
break; |
|---|
| 32 |
case 2: |
|---|
| 33 |
_date = new Date(args[0], args[1]); |
|---|
| 34 |
break; |
|---|
| 35 |
case 3: |
|---|
| 36 |
_date = new Date(args[0], args[1], args[2]); |
|---|
| 37 |
break; |
|---|
| 38 |
case 4: |
|---|
| 39 |
_date = new Date(args[0], args[1], args[2], args[3]); |
|---|
| 40 |
break; |
|---|
| 41 |
case 5: |
|---|
| 42 |
_date = new Date(args[0], args[1], args[2], args[3], args[4]); |
|---|
| 43 |
break; |
|---|
| 44 |
case 6: |
|---|
| 45 |
_date = new Date(args[0], args[1], args[2], args[3], args[4], args[5]); |
|---|
| 46 |
break; |
|---|
| 47 |
case 7: |
|---|
| 48 |
_date = new Date(args[0], args[1], args[2], args[3], args[4], args[5], args[6]); |
|---|
| 49 |
break; |
|---|
| 50 |
default: |
|---|
| 51 |
_date = new Date(); |
|---|
| 52 |
break; |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
/** |
|---|
| 57 |
* プロキシメソッド |
|---|
| 58 |
* @param methodName メソッド名 |
|---|
| 59 |
* @param args メソッドに渡す引数 |
|---|
| 60 |
* @return DateオブジェクトまたはSmartDateオブジェクト |
|---|
| 61 |
*/ |
|---|
| 62 |
override flash_proxy function callProperty(methodName:*, ... args):* { |
|---|
| 63 |
var result:*; |
|---|
| 64 |
if (_date[methodName]) { |
|---|
| 65 |
result = _date[methodName.toString()].apply(_date, args); |
|---|
| 66 |
} else { |
|---|
| 67 |
result = this[methodName.toString()].apply(this, args); |
|---|
| 68 |
} |
|---|
| 69 |
return result; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
/** |
|---|
| 73 |
* プロパティゲッタ |
|---|
| 74 |
* @param name プロパティ名 |
|---|
| 75 |
* @return プロパティ |
|---|
| 76 |
*/ |
|---|
| 77 |
override flash_proxy function getProperty(name:*):* { |
|---|
| 78 |
return _date[name]; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
/** |
|---|
| 82 |
* プロパティセッタ |
|---|
| 83 |
* @param name プロパティ名 |
|---|
| 84 |
*/ |
|---|
| 85 |
override flash_proxy function setProperty(name:*, value:*):void { |
|---|
| 86 |
_date[name] = value; |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
/** |
|---|
| 90 |
* 今を得る |
|---|
| 91 |
* @return SmartDateオブジェクト |
|---|
| 92 |
*/ |
|---|
| 93 |
public function now():SmartDate { |
|---|
| 94 |
return new SmartDate(); |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
/** |
|---|
| 98 |
* 明日を得る |
|---|
| 99 |
* @return SmartDateオブジェクト |
|---|
| 100 |
*/ |
|---|
| 101 |
public function tomorrow():SmartDate { |
|---|
| 102 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 103 |
d.setDate(_date.getDate() + 1); |
|---|
| 104 |
return d; |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
/** |
|---|
| 108 |
* 昨日を得る |
|---|
| 109 |
* @return SmartDateオブジェクト |
|---|
| 110 |
*/ |
|---|
| 111 |
public function yesterday():SmartDate { |
|---|
| 112 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 113 |
d.setDate(_date.getDate() - 1); |
|---|
| 114 |
return d; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
/** |
|---|
| 118 |
* 次の週を得る |
|---|
| 119 |
* @return SmartDateオブジェクト |
|---|
| 120 |
*/ |
|---|
| 121 |
public function nextWeek():SmartDate { |
|---|
| 122 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 123 |
d.setDate(_date.getDate() + 7); |
|---|
| 124 |
return d; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
/** |
|---|
| 128 |
* 先週を得る |
|---|
| 129 |
* @return SmartDateオブジェクト |
|---|
| 130 |
*/ |
|---|
| 131 |
public function lastWeek():SmartDate { |
|---|
| 132 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 133 |
d.setDate(_date.getDate() - 7); |
|---|
| 134 |
return d; |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
/** |
|---|
| 138 |
* 次の月を得る |
|---|
| 139 |
* @return SmartDateオブジェクト |
|---|
| 140 |
*/ |
|---|
| 141 |
public function nextMonth():SmartDate { |
|---|
| 142 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 143 |
d.setMonth(_date.getMonth() + 1); |
|---|
| 144 |
return d; |
|---|
| 145 |
}; |
|---|
| 146 |
|
|---|
| 147 |
/** |
|---|
| 148 |
* 先月を得る |
|---|
| 149 |
* @return SmartDateオブジェクト |
|---|
| 150 |
*/ |
|---|
| 151 |
public function lastMonth():SmartDate { |
|---|
| 152 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 153 |
d.setMonth(_date.getMonth() - 1); |
|---|
| 154 |
return d; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
/** |
|---|
| 158 |
* 来年を得る |
|---|
| 159 |
* @return SmartDateオブジェクト |
|---|
| 160 |
*/ |
|---|
| 161 |
public function nextYear():SmartDate { |
|---|
| 162 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 163 |
d.setFullYear(_date.getFullYear() + 1); |
|---|
| 164 |
return d; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
/** |
|---|
| 168 |
* 去年を得る |
|---|
| 169 |
* @return SmartDateオブジェクト |
|---|
| 170 |
*/ |
|---|
| 171 |
public function lastYear():SmartDate { |
|---|
| 172 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 173 |
d.setFullYear(_date.getFullYear() - 1); |
|---|
| 174 |
return d; |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
/** |
|---|
| 178 |
* 月の始めを得る |
|---|
| 179 |
* @return SmartDateオブジェクト |
|---|
| 180 |
*/ |
|---|
| 181 |
public function startOfMonth():SmartDate { |
|---|
| 182 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 183 |
d.setDate(1); |
|---|
| 184 |
return d; |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
/** |
|---|
| 188 |
* 月末を得る |
|---|
| 189 |
* @return SmartDateオブジェクト |
|---|
| 190 |
*/ |
|---|
| 191 |
public function endOfMonth():SmartDate { |
|---|
| 192 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 193 |
d.setMonth(d.getMonth() + 1); |
|---|
| 194 |
d.setDate(0); |
|---|
| 195 |
return d; |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
/** |
|---|
| 199 |
* 次の日曜を得る |
|---|
| 200 |
* @return SmartDateオブジェクト |
|---|
| 201 |
*/ |
|---|
| 202 |
public function nextSunday():SmartDate { |
|---|
| 203 |
return calcNextDay(SUNDAY); |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
/** |
|---|
| 207 |
* 次の月曜を得る |
|---|
| 208 |
* @return SmartDateオブジェクト |
|---|
| 209 |
*/ |
|---|
| 210 |
public function nextMonday():SmartDate { |
|---|
| 211 |
return calcNextDay(MONDAY); |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
/** |
|---|
| 215 |
* 次の火曜を得る |
|---|
| 216 |
* @return SmartDateオブジェクト |
|---|
| 217 |
*/ |
|---|
| 218 |
public function nextTuesday():SmartDate { |
|---|
| 219 |
return calcNextDay(TUESDAY); |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
/** |
|---|
| 223 |
* 次の水曜を得る |
|---|
| 224 |
* @return SmartDateオブジェクト |
|---|
| 225 |
*/ |
|---|
| 226 |
public function nextWednesday():SmartDate { |
|---|
| 227 |
return calcNextDay(WEDNESDAY); |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
/** |
|---|
| 231 |
* 次の木曜を得る |
|---|
| 232 |
* @return SmartDateオブジェクト |
|---|
| 233 |
*/ |
|---|
| 234 |
public function nextThursday():SmartDate { |
|---|
| 235 |
return calcNextDay(THURSDAY); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
/** |
|---|
| 239 |
* 次の金曜を得る |
|---|
| 240 |
* @return SmartDateオブジェクト |
|---|
| 241 |
*/ |
|---|
| 242 |
public function nextFriday():SmartDate { |
|---|
| 243 |
return calcNextDay(FRIDAY); |
|---|
| 244 |
} |
|---|
| 245 |
|
|---|
| 246 |
/** |
|---|
| 247 |
* 次の土曜を得る |
|---|
| 248 |
* @return SmartDateオブジェクト |
|---|
| 249 |
*/ |
|---|
| 250 |
public function nextSaturday():SmartDate { |
|---|
| 251 |
return calcNextDay(SATURDAY); |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
/** |
|---|
| 255 |
* 前の日曜を得る |
|---|
| 256 |
* @return SmartDateオブジェクト |
|---|
| 257 |
*/ |
|---|
| 258 |
public function lastSunday():SmartDate { |
|---|
| 259 |
return calcLastDay(SUNDAY); |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
/** |
|---|
| 263 |
* 前の月曜を得る |
|---|
| 264 |
* @return SmartDateオブジェクト |
|---|
| 265 |
*/ |
|---|
| 266 |
public function lastMonday():SmartDate { |
|---|
| 267 |
return calcLastDay(MONDAY); |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
/** |
|---|
| 271 |
* 前の火曜を得る |
|---|
| 272 |
* @return SmartDateオブジェクト |
|---|
| 273 |
*/ |
|---|
| 274 |
public function lastTuesday():SmartDate { |
|---|
| 275 |
return calcLastDay(TUESDAY); |
|---|
| 276 |
} |
|---|
| 277 |
|
|---|
| 278 |
/** |
|---|
| 279 |
* 前の水曜を得る |
|---|
| 280 |
* @return SmartDateオブジェクト |
|---|
| 281 |
*/ |
|---|
| 282 |
public function lastWednesday():SmartDate { |
|---|
| 283 |
return calcLastDay(WEDNESDAY); |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
/** |
|---|
| 287 |
* 前の木曜を得る |
|---|
| 288 |
* @return SmartDateオブジェクト |
|---|
| 289 |
*/ |
|---|
| 290 |
public function lastThursday():SmartDate { |
|---|
| 291 |
return calcLastDay(THURSDAY); |
|---|
| 292 |
} |
|---|
| 293 |
|
|---|
| 294 |
/** |
|---|
| 295 |
* 前の金曜を得る |
|---|
| 296 |
* @return SmartDateオブジェクト |
|---|
| 297 |
*/ |
|---|
| 298 |
public function lastFriday():SmartDate { |
|---|
| 299 |
return calcLastDay(FRIDAY); |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
/** |
|---|
| 303 |
* 前の土曜を得る |
|---|
| 304 |
* @return SmartDateオブジェクト |
|---|
| 305 |
*/ |
|---|
| 306 |
public function lastSaturday():SmartDate { |
|---|
| 307 |
return calcLastDay(SATURDAY); |
|---|
| 308 |
} |
|---|
| 309 |
|
|---|
| 310 |
/** |
|---|
| 311 |
* 次の曜日を計算する |
|---|
| 312 |
* @return SmartDateオブジェクト |
|---|
| 313 |
*/ |
|---|
| 314 |
private function calcNextDay(dayOfWeek:String):SmartDate { |
|---|
| 315 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 316 |
var delta:Number = DAY_OF_WEEKS.indexOf(dayOfWeek) - d.getDay(); |
|---|
| 317 |
if (delta <= 0) { |
|---|
| 318 |
d.setDate(d.getDate() + 7 - d.getDay() + DAY_OF_WEEKS.indexOf(dayOfWeek)); |
|---|
| 319 |
} else { |
|---|
| 320 |
d.setDate(d.getDate() + delta); |
|---|
| 321 |
} |
|---|
| 322 |
return d; |
|---|
| 323 |
} |
|---|
| 324 |
|
|---|
| 325 |
/** |
|---|
| 326 |
* 前の曜日を計算する |
|---|
| 327 |
* @return SmartDateオブジェクト |
|---|
| 328 |
*/ |
|---|
| 329 |
private function calcLastDay(dayOfWeek:String):SmartDate { |
|---|
| 330 |
var d:SmartDate = new SmartDate(_date.getTime()); |
|---|
| 331 |
var delta:Number = d.getDay() - DAY_OF_WEEKS.indexOf(dayOfWeek); |
|---|
| 332 |
if (delta <= 0) { |
|---|
| 333 |
d.setDate(d.getDate() - 7 - d.getDay() + DAY_OF_WEEKS.indexOf(dayOfWeek)); |
|---|
| 334 |
} else { |
|---|
| 335 |
d.setDate(d.getDate() - delta); |
|---|
| 336 |
} |
|---|
| 337 |
return d; |
|---|
| 338 |
} |
|---|
| 339 |
} |
|---|
| 340 |
} |
|---|