annotate bindings/as3/ext/com/adobe/utils/DateUtil.as @ 770:c54bc2ffbf92 tip

update tags
author convert-repo
date Fri, 16 Dec 2011 11:34:01 +0000
parents 3a0b9700b3d2
children
rev   line source
mas01mj@732 1 /*
mas01mj@732 2 Copyright (c) 2008, Adobe Systems Incorporated
mas01mj@732 3 All rights reserved.
mas01mj@732 4
mas01mj@732 5 Redistribution and use in source and binary forms, with or without
mas01mj@732 6 modification, are permitted provided that the following conditions are
mas01mj@732 7 met:
mas01mj@732 8
mas01mj@732 9 * Redistributions of source code must retain the above copyright notice,
mas01mj@732 10 this list of conditions and the following disclaimer.
mas01mj@732 11
mas01mj@732 12 * Redistributions in binary form must reproduce the above copyright
mas01mj@732 13 notice, this list of conditions and the following disclaimer in the
mas01mj@732 14 documentation and/or other materials provided with the distribution.
mas01mj@732 15
mas01mj@732 16 * Neither the name of Adobe Systems Incorporated nor the names of its
mas01mj@732 17 contributors may be used to endorse or promote products derived from
mas01mj@732 18 this software without specific prior written permission.
mas01mj@732 19
mas01mj@732 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
mas01mj@732 21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
mas01mj@732 22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
mas01mj@732 23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
mas01mj@732 24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
mas01mj@732 25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
mas01mj@732 26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
mas01mj@732 27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
mas01mj@732 28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
mas01mj@732 29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
mas01mj@732 30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mas01mj@732 31 */
mas01mj@732 32
mas01mj@732 33 package com.adobe.utils
mas01mj@732 34 {
mas01mj@732 35 import mx.formatters.DateBase;
mas01mj@732 36
mas01mj@732 37 /**
mas01mj@732 38 * Class that contains static utility methods for manipulating and working
mas01mj@732 39 * with Dates.
mas01mj@732 40 *
mas01mj@732 41 * @langversion ActionScript 3.0
mas01mj@732 42 * @playerversion Flash 9.0
mas01mj@732 43 * @tiptext
mas01mj@732 44 */
mas01mj@732 45 public class DateUtil
mas01mj@732 46 {
mas01mj@732 47
mas01mj@732 48 /**
mas01mj@732 49 * Returns the English Short Month name (3 letters) for the Month that
mas01mj@732 50 * the Date represents.
mas01mj@732 51 *
mas01mj@732 52 * @param d The Date instance whose month will be used to retrieve the
mas01mj@732 53 * short month name.
mas01mj@732 54 *
mas01mj@732 55 * @return An English 3 Letter Month abbreviation.
mas01mj@732 56 *
mas01mj@732 57 * @langversion ActionScript 3.0
mas01mj@732 58 * @playerversion Flash 9.0
mas01mj@732 59 * @tiptext
mas01mj@732 60 *
mas01mj@732 61 * @see SHORT_MONTH
mas01mj@732 62 */
mas01mj@732 63 public static function getShortMonthName(d:Date):String
mas01mj@732 64 {
mas01mj@732 65 return DateBase.monthNamesShort[d.getMonth()];
mas01mj@732 66 }
mas01mj@732 67
mas01mj@732 68 /**
mas01mj@732 69 * Returns the index of the month that the short month name string
mas01mj@732 70 * represents.
mas01mj@732 71 *
mas01mj@732 72 * @param m The 3 letter abbreviation representing a short month name.
mas01mj@732 73 *
mas01mj@732 74 * @param Optional parameter indicating whether the search should be case
mas01mj@732 75 * sensitive
mas01mj@732 76 *
mas01mj@732 77 * @return A int that represents that month represented by the specifed
mas01mj@732 78 * short name.
mas01mj@732 79 *
mas01mj@732 80 * @langversion ActionScript 3.0
mas01mj@732 81 * @playerversion Flash 9.0
mas01mj@732 82 * @tiptext
mas01mj@732 83 *
mas01mj@732 84 * @see SHORT_MONTH
mas01mj@732 85 */
mas01mj@732 86 public static function getShortMonthIndex(m:String):int
mas01mj@732 87 {
mas01mj@732 88 return DateBase.monthNamesShort.indexOf(m);
mas01mj@732 89 }
mas01mj@732 90
mas01mj@732 91 /**
mas01mj@732 92 * Returns the English full Month name for the Month that
mas01mj@732 93 * the Date represents.
mas01mj@732 94 *
mas01mj@732 95 * @param d The Date instance whose month will be used to retrieve the
mas01mj@732 96 * full month name.
mas01mj@732 97 *
mas01mj@732 98 * @return An English full month name.
mas01mj@732 99 *
mas01mj@732 100 * @langversion ActionScript 3.0
mas01mj@732 101 * @playerversion Flash 9.0
mas01mj@732 102 * @tiptext
mas01mj@732 103 *
mas01mj@732 104 * @see FULL_MONTH
mas01mj@732 105 */
mas01mj@732 106 public static function getFullMonthName(d:Date):String
mas01mj@732 107 {
mas01mj@732 108 return DateBase.monthNamesLong[d.getMonth()];
mas01mj@732 109 }
mas01mj@732 110
mas01mj@732 111 /**
mas01mj@732 112 * Returns the index of the month that the full month name string
mas01mj@732 113 * represents.
mas01mj@732 114 *
mas01mj@732 115 * @param m A full month name.
mas01mj@732 116 *
mas01mj@732 117 * @return A int that represents that month represented by the specifed
mas01mj@732 118 * full month name.
mas01mj@732 119 *
mas01mj@732 120 * @langversion ActionScript 3.0
mas01mj@732 121 * @playerversion Flash 9.0
mas01mj@732 122 * @tiptext
mas01mj@732 123 *
mas01mj@732 124 * @see FULL_MONTH
mas01mj@732 125 */
mas01mj@732 126 public static function getFullMonthIndex(m:String):int
mas01mj@732 127 {
mas01mj@732 128 return DateBase.monthNamesLong.indexOf(m);
mas01mj@732 129 }
mas01mj@732 130
mas01mj@732 131 /**
mas01mj@732 132 * Returns the English Short Day name (3 letters) for the day that
mas01mj@732 133 * the Date represents.
mas01mj@732 134 *
mas01mj@732 135 * @param d The Date instance whose day will be used to retrieve the
mas01mj@732 136 * short day name.
mas01mj@732 137 *
mas01mj@732 138 * @return An English 3 Letter day abbreviation.
mas01mj@732 139 *
mas01mj@732 140 * @langversion ActionScript 3.0
mas01mj@732 141 * @playerversion Flash 9.0
mas01mj@732 142 * @tiptext
mas01mj@732 143 *
mas01mj@732 144 * @see SHORT_DAY
mas01mj@732 145 */
mas01mj@732 146 public static function getShortDayName(d:Date):String
mas01mj@732 147 {
mas01mj@732 148 return DateBase.dayNamesShort[d.getDay()];
mas01mj@732 149 }
mas01mj@732 150
mas01mj@732 151 /**
mas01mj@732 152 * Returns the index of the day that the short day name string
mas01mj@732 153 * represents.
mas01mj@732 154 *
mas01mj@732 155 * @param m A short day name.
mas01mj@732 156 *
mas01mj@732 157 * @return A int that represents that short day represented by the specifed
mas01mj@732 158 * full month name.
mas01mj@732 159 *
mas01mj@732 160 * @langversion ActionScript 3.0
mas01mj@732 161 * @playerversion Flash 9.0
mas01mj@732 162 * @tiptext
mas01mj@732 163 *
mas01mj@732 164 * @see SHORT_DAY
mas01mj@732 165 */
mas01mj@732 166 public static function getShortDayIndex(d:String):int
mas01mj@732 167 {
mas01mj@732 168 return DateBase.dayNamesShort.indexOf(d);
mas01mj@732 169 }
mas01mj@732 170
mas01mj@732 171 /**
mas01mj@732 172 * Returns the English full day name for the day that
mas01mj@732 173 * the Date represents.
mas01mj@732 174 *
mas01mj@732 175 * @param d The Date instance whose day will be used to retrieve the
mas01mj@732 176 * full day name.
mas01mj@732 177 *
mas01mj@732 178 * @return An English full day name.
mas01mj@732 179 *
mas01mj@732 180 * @langversion ActionScript 3.0
mas01mj@732 181 * @playerversion Flash 9.0
mas01mj@732 182 * @tiptext
mas01mj@732 183 *
mas01mj@732 184 * @see FULL_DAY
mas01mj@732 185 */
mas01mj@732 186 public static function getFullDayName(d:Date):String
mas01mj@732 187 {
mas01mj@732 188 return DateBase.dayNamesLong[d.getDay()];
mas01mj@732 189 }
mas01mj@732 190
mas01mj@732 191 /**
mas01mj@732 192 * Returns the index of the day that the full day name string
mas01mj@732 193 * represents.
mas01mj@732 194 *
mas01mj@732 195 * @param m A full day name.
mas01mj@732 196 *
mas01mj@732 197 * @return A int that represents that full day represented by the specifed
mas01mj@732 198 * full month name.
mas01mj@732 199 *
mas01mj@732 200 * @langversion ActionScript 3.0
mas01mj@732 201 * @playerversion Flash 9.0
mas01mj@732 202 * @tiptext
mas01mj@732 203 *
mas01mj@732 204 * @see FULL_DAY
mas01mj@732 205 */
mas01mj@732 206 public static function getFullDayIndex(d:String):int
mas01mj@732 207 {
mas01mj@732 208 return DateBase.dayNamesLong.indexOf(d);
mas01mj@732 209 }
mas01mj@732 210
mas01mj@732 211 /**
mas01mj@732 212 * Returns a two digit representation of the year represented by the
mas01mj@732 213 * specified date.
mas01mj@732 214 *
mas01mj@732 215 * @param d The Date instance whose year will be used to generate a two
mas01mj@732 216 * digit string representation of the year.
mas01mj@732 217 *
mas01mj@732 218 * @return A string that contains a 2 digit representation of the year.
mas01mj@732 219 * Single digits will be padded with 0.
mas01mj@732 220 *
mas01mj@732 221 * @langversion ActionScript 3.0
mas01mj@732 222 * @playerversion Flash 9.0
mas01mj@732 223 * @tiptext
mas01mj@732 224 */
mas01mj@732 225 public static function getShortYear(d:Date):String
mas01mj@732 226 {
mas01mj@732 227 var dStr:String = String(d.getFullYear());
mas01mj@732 228
mas01mj@732 229 if(dStr.length < 3)
mas01mj@732 230 {
mas01mj@732 231 return dStr;
mas01mj@732 232 }
mas01mj@732 233
mas01mj@732 234 return (dStr.substr(dStr.length - 2));
mas01mj@732 235 }
mas01mj@732 236
mas01mj@732 237 /**
mas01mj@732 238 * Compares two dates and returns an integer depending on their relationship.
mas01mj@732 239 *
mas01mj@732 240 * Returns -1 if d1 is greater than d2.
mas01mj@732 241 * Returns 1 if d2 is greater than d1.
mas01mj@732 242 * Returns 0 if both dates are equal.
mas01mj@732 243 *
mas01mj@732 244 * @param d1 The date that will be compared to the second date.
mas01mj@732 245 * @param d2 The date that will be compared to the first date.
mas01mj@732 246 *
mas01mj@732 247 * @return An int indicating how the two dates compare.
mas01mj@732 248 *
mas01mj@732 249 * @langversion ActionScript 3.0
mas01mj@732 250 * @playerversion Flash 9.0
mas01mj@732 251 * @tiptext
mas01mj@732 252 */
mas01mj@732 253 public static function compareDates(d1:Date, d2:Date):int
mas01mj@732 254 {
mas01mj@732 255 var d1ms:Number = d1.getTime();
mas01mj@732 256 var d2ms:Number = d2.getTime();
mas01mj@732 257
mas01mj@732 258 if(d1ms > d2ms)
mas01mj@732 259 {
mas01mj@732 260 return -1;
mas01mj@732 261 }
mas01mj@732 262 else if(d1ms < d2ms)
mas01mj@732 263 {
mas01mj@732 264 return 1;
mas01mj@732 265 }
mas01mj@732 266 else
mas01mj@732 267 {
mas01mj@732 268 return 0;
mas01mj@732 269 }
mas01mj@732 270 }
mas01mj@732 271
mas01mj@732 272 /**
mas01mj@732 273 * Returns a short hour (0 - 12) represented by the specified date.
mas01mj@732 274 *
mas01mj@732 275 * If the hour is less than 12 (0 - 11 AM) then the hour will be returned.
mas01mj@732 276 *
mas01mj@732 277 * If the hour is greater than 12 (12 - 23 PM) then the hour minus 12
mas01mj@732 278 * will be returned.
mas01mj@732 279 *
mas01mj@732 280 * @param d1 The Date from which to generate the short hour
mas01mj@732 281 *
mas01mj@732 282 * @return An int between 0 and 13 ( 1 - 12 ) representing the short hour.
mas01mj@732 283 *
mas01mj@732 284 * @langversion ActionScript 3.0
mas01mj@732 285 * @playerversion Flash 9.0
mas01mj@732 286 * @tiptext
mas01mj@732 287 */
mas01mj@732 288 public static function getShortHour(d:Date):int
mas01mj@732 289 {
mas01mj@732 290 var h:int = d.hours;
mas01mj@732 291
mas01mj@732 292 if(h == 0 || h == 12)
mas01mj@732 293 {
mas01mj@732 294 return 12;
mas01mj@732 295 }
mas01mj@732 296 else if(h > 12)
mas01mj@732 297 {
mas01mj@732 298 return h - 12;
mas01mj@732 299 }
mas01mj@732 300 else
mas01mj@732 301 {
mas01mj@732 302 return h;
mas01mj@732 303 }
mas01mj@732 304 }
mas01mj@732 305
mas01mj@732 306 /**
mas01mj@732 307 * Returns a string indicating whether the date represents a time in the
mas01mj@732 308 * ante meridiem (AM) or post meridiem (PM).
mas01mj@732 309 *
mas01mj@732 310 * If the hour is less than 12 then "AM" will be returned.
mas01mj@732 311 *
mas01mj@732 312 * If the hour is greater than 12 then "PM" will be returned.
mas01mj@732 313 *
mas01mj@732 314 * @param d1 The Date from which to generate the 12 hour clock indicator.
mas01mj@732 315 *
mas01mj@732 316 * @return A String ("AM" or "PM") indicating which half of the day the
mas01mj@732 317 * hour represents.
mas01mj@732 318 *
mas01mj@732 319 * @langversion ActionScript 3.0
mas01mj@732 320 * @playerversion Flash 9.0
mas01mj@732 321 * @tiptext
mas01mj@732 322 */
mas01mj@732 323 public static function getAMPM(d:Date):String
mas01mj@732 324 {
mas01mj@732 325 return (d.hours > 11)? "PM" : "AM";
mas01mj@732 326 }
mas01mj@732 327
mas01mj@732 328 /**
mas01mj@732 329 * Parses dates that conform to RFC822 into Date objects. This method also
mas01mj@732 330 * supports four-digit years (not supported in RFC822), but two-digit years
mas01mj@732 331 * (referring to the 20th century) are fine, too.
mas01mj@732 332 *
mas01mj@732 333 * This function is useful for parsing RSS .91, .92, and 2.0 dates.
mas01mj@732 334 *
mas01mj@732 335 * @param str
mas01mj@732 336 *
mas01mj@732 337 * @returns
mas01mj@732 338 *
mas01mj@732 339 * @langversion ActionScript 3.0
mas01mj@732 340 * @playerversion Flash 9.0
mas01mj@732 341 * @tiptext
mas01mj@732 342 *
mas01mj@732 343 * @see http://asg.web.cmu.edu/rfc/rfc822.html
mas01mj@732 344 */
mas01mj@732 345 public static function parseRFC822(str:String):Date
mas01mj@732 346 {
mas01mj@732 347 var finalDate:Date;
mas01mj@732 348 try
mas01mj@732 349 {
mas01mj@732 350 var dateParts:Array = str.split(" ");
mas01mj@732 351 var day:String = null;
mas01mj@732 352
mas01mj@732 353 if (dateParts[0].search(/\d/) == -1)
mas01mj@732 354 {
mas01mj@732 355 day = dateParts.shift().replace(/\W/, "");
mas01mj@732 356 }
mas01mj@732 357
mas01mj@732 358 var date:Number = Number(dateParts.shift());
mas01mj@732 359 var month:Number = Number(DateUtil.getShortMonthIndex(dateParts.shift()));
mas01mj@732 360 var year:Number = Number(dateParts.shift());
mas01mj@732 361 var timeParts:Array = dateParts.shift().split(":");
mas01mj@732 362 var hour:Number = int(timeParts.shift());
mas01mj@732 363 var minute:Number = int(timeParts.shift());
mas01mj@732 364 var second:Number = (timeParts.length > 0) ? int(timeParts.shift()): 0;
mas01mj@732 365
mas01mj@732 366 var milliseconds:Number = Date.UTC(year, month, date, hour, minute, second, 0);
mas01mj@732 367
mas01mj@732 368 var timezone:String = dateParts.shift();
mas01mj@732 369 var offset:Number = 0;
mas01mj@732 370
mas01mj@732 371 if (timezone.search(/\d/) == -1)
mas01mj@732 372 {
mas01mj@732 373 switch(timezone)
mas01mj@732 374 {
mas01mj@732 375 case "UT":
mas01mj@732 376 offset = 0;
mas01mj@732 377 break;
mas01mj@732 378 case "UTC":
mas01mj@732 379 offset = 0;
mas01mj@732 380 break;
mas01mj@732 381 case "GMT":
mas01mj@732 382 offset = 0;
mas01mj@732 383 break;
mas01mj@732 384 case "EST":
mas01mj@732 385 offset = (-5 * 3600000);
mas01mj@732 386 break;
mas01mj@732 387 case "EDT":
mas01mj@732 388 offset = (-4 * 3600000);
mas01mj@732 389 break;
mas01mj@732 390 case "CST":
mas01mj@732 391 offset = (-6 * 3600000);
mas01mj@732 392 break;
mas01mj@732 393 case "CDT":
mas01mj@732 394 offset = (-5 * 3600000);
mas01mj@732 395 break;
mas01mj@732 396 case "MST":
mas01mj@732 397 offset = (-7 * 3600000);
mas01mj@732 398 break;
mas01mj@732 399 case "MDT":
mas01mj@732 400 offset = (-6 * 3600000);
mas01mj@732 401 break;
mas01mj@732 402 case "PST":
mas01mj@732 403 offset = (-8 * 3600000);
mas01mj@732 404 break;
mas01mj@732 405 case "PDT":
mas01mj@732 406 offset = (-7 * 3600000);
mas01mj@732 407 break;
mas01mj@732 408 case "Z":
mas01mj@732 409 offset = 0;
mas01mj@732 410 break;
mas01mj@732 411 case "A":
mas01mj@732 412 offset = (-1 * 3600000);
mas01mj@732 413 break;
mas01mj@732 414 case "M":
mas01mj@732 415 offset = (-12 * 3600000);
mas01mj@732 416 break;
mas01mj@732 417 case "N":
mas01mj@732 418 offset = (1 * 3600000);
mas01mj@732 419 break;
mas01mj@732 420 case "Y":
mas01mj@732 421 offset = (12 * 3600000);
mas01mj@732 422 break;
mas01mj@732 423 default:
mas01mj@732 424 offset = 0;
mas01mj@732 425 }
mas01mj@732 426 }
mas01mj@732 427 else
mas01mj@732 428 {
mas01mj@732 429 var multiplier:Number = 1;
mas01mj@732 430 var oHours:Number = 0;
mas01mj@732 431 var oMinutes:Number = 0;
mas01mj@732 432 if (timezone.length != 4)
mas01mj@732 433 {
mas01mj@732 434 if (timezone.charAt(0) == "-")
mas01mj@732 435 {
mas01mj@732 436 multiplier = -1;
mas01mj@732 437 }
mas01mj@732 438 timezone = timezone.substr(1, 4);
mas01mj@732 439 }
mas01mj@732 440 oHours = Number(timezone.substr(0, 2));
mas01mj@732 441 oMinutes = Number(timezone.substr(2, 2));
mas01mj@732 442 offset = (((oHours * 3600000) + (oMinutes * 60000)) * multiplier);
mas01mj@732 443 }
mas01mj@732 444
mas01mj@732 445 finalDate = new Date(milliseconds - offset);
mas01mj@732 446
mas01mj@732 447 if (finalDate.toString() == "Invalid Date")
mas01mj@732 448 {
mas01mj@732 449 throw new Error("This date does not conform to RFC822.");
mas01mj@732 450 }
mas01mj@732 451 }
mas01mj@732 452 catch (e:Error)
mas01mj@732 453 {
mas01mj@732 454 var eStr:String = "Unable to parse the string [" +str+ "] into a date. ";
mas01mj@732 455 eStr += "The internal error was: " + e.toString();
mas01mj@732 456 throw new Error(eStr);
mas01mj@732 457 }
mas01mj@732 458 return finalDate;
mas01mj@732 459 }
mas01mj@732 460
mas01mj@732 461 /**
mas01mj@732 462 * Returns a date string formatted according to RFC822.
mas01mj@732 463 *
mas01mj@732 464 * @param d
mas01mj@732 465 *
mas01mj@732 466 * @returns
mas01mj@732 467 *
mas01mj@732 468 * @langversion ActionScript 3.0
mas01mj@732 469 * @playerversion Flash 9.0
mas01mj@732 470 * @tiptext
mas01mj@732 471 *
mas01mj@732 472 * @see http://asg.web.cmu.edu/rfc/rfc822.html
mas01mj@732 473 */
mas01mj@732 474 public static function toRFC822(d:Date):String
mas01mj@732 475 {
mas01mj@732 476 var date:Number = d.getUTCDate();
mas01mj@732 477 var hours:Number = d.getUTCHours();
mas01mj@732 478 var minutes:Number = d.getUTCMinutes();
mas01mj@732 479 var seconds:Number = d.getUTCSeconds();
mas01mj@732 480 var sb:String = new String();
mas01mj@732 481 sb += DateBase.dayNamesShort[d.getUTCDay()];
mas01mj@732 482 sb += ", ";
mas01mj@732 483
mas01mj@732 484 if (date < 10)
mas01mj@732 485 {
mas01mj@732 486 sb += "0";
mas01mj@732 487 }
mas01mj@732 488 sb += date;
mas01mj@732 489 sb += " ";
mas01mj@732 490 //sb += DateUtil.SHORT_MONTH[d.getUTCMonth()];
mas01mj@732 491 sb += DateBase.monthNamesShort[d.getUTCMonth()];
mas01mj@732 492 sb += " ";
mas01mj@732 493 sb += d.getUTCFullYear();
mas01mj@732 494 sb += " ";
mas01mj@732 495 if (hours < 10)
mas01mj@732 496 {
mas01mj@732 497 sb += "0";
mas01mj@732 498 }
mas01mj@732 499 sb += hours;
mas01mj@732 500 sb += ":";
mas01mj@732 501 if (minutes < 10)
mas01mj@732 502 {
mas01mj@732 503 sb += "0";
mas01mj@732 504 }
mas01mj@732 505 sb += minutes;
mas01mj@732 506 sb += ":";
mas01mj@732 507 if (seconds < 10)
mas01mj@732 508 {
mas01mj@732 509 sb += "0";
mas01mj@732 510 }
mas01mj@732 511 sb += seconds;
mas01mj@732 512 sb += " GMT";
mas01mj@732 513 return sb;
mas01mj@732 514 }
mas01mj@732 515
mas01mj@732 516 /**
mas01mj@732 517 * Parses dates that conform to the W3C Date-time Format into Date objects.
mas01mj@732 518 *
mas01mj@732 519 * This function is useful for parsing RSS 1.0 and Atom 1.0 dates.
mas01mj@732 520 *
mas01mj@732 521 * @param str
mas01mj@732 522 *
mas01mj@732 523 * @returns
mas01mj@732 524 *
mas01mj@732 525 * @langversion ActionScript 3.0
mas01mj@732 526 * @playerversion Flash 9.0
mas01mj@732 527 * @tiptext
mas01mj@732 528 *
mas01mj@732 529 * @see http://www.w3.org/TR/NOTE-datetime
mas01mj@732 530 */
mas01mj@732 531 public static function parseW3CDTF(str:String):Date
mas01mj@732 532 {
mas01mj@732 533 var finalDate:Date;
mas01mj@732 534 try
mas01mj@732 535 {
mas01mj@732 536 var dateStr:String = str.substring(0, str.indexOf("T"));
mas01mj@732 537 var timeStr:String = str.substring(str.indexOf("T")+1, str.length);
mas01mj@732 538 var dateArr:Array = dateStr.split("-");
mas01mj@732 539 var year:Number = Number(dateArr.shift());
mas01mj@732 540 var month:Number = Number(dateArr.shift());
mas01mj@732 541 var date:Number = Number(dateArr.shift());
mas01mj@732 542
mas01mj@732 543 var multiplier:Number;
mas01mj@732 544 var offsetHours:Number;
mas01mj@732 545 var offsetMinutes:Number;
mas01mj@732 546 var offsetStr:String;
mas01mj@732 547
mas01mj@732 548 if (timeStr.indexOf("Z") != -1)
mas01mj@732 549 {
mas01mj@732 550 multiplier = 1;
mas01mj@732 551 offsetHours = 0;
mas01mj@732 552 offsetMinutes = 0;
mas01mj@732 553 timeStr = timeStr.replace("Z", "");
mas01mj@732 554 }
mas01mj@732 555 else if (timeStr.indexOf("+") != -1)
mas01mj@732 556 {
mas01mj@732 557 multiplier = 1;
mas01mj@732 558 offsetStr = timeStr.substring(timeStr.indexOf("+")+1, timeStr.length);
mas01mj@732 559 offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":")));
mas01mj@732 560 offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1, offsetStr.length));
mas01mj@732 561 timeStr = timeStr.substring(0, timeStr.indexOf("+"));
mas01mj@732 562 }
mas01mj@732 563 else // offset is -
mas01mj@732 564 {
mas01mj@732 565 multiplier = -1;
mas01mj@732 566 offsetStr = timeStr.substring(timeStr.indexOf("-")+1, timeStr.length);
mas01mj@732 567 offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":")));
mas01mj@732 568 offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1, offsetStr.length));
mas01mj@732 569 timeStr = timeStr.substring(0, timeStr.indexOf("-"));
mas01mj@732 570 }
mas01mj@732 571 var timeArr:Array = timeStr.split(":");
mas01mj@732 572 var hour:Number = Number(timeArr.shift());
mas01mj@732 573 var minutes:Number = Number(timeArr.shift());
mas01mj@732 574 var secondsArr:Array = (timeArr.length > 0) ? String(timeArr.shift()).split(".") : null;
mas01mj@732 575 var seconds:Number = (secondsArr != null && secondsArr.length > 0) ? Number(secondsArr.shift()) : 0;
mas01mj@732 576 //var milliseconds:Number = (secondsArr != null && secondsArr.length > 0) ? Number(secondsArr.shift()) : 0;
mas01mj@732 577
mas01mj@732 578 var milliseconds:Number = (secondsArr != null && secondsArr.length > 0) ? 1000*parseFloat("0." + secondsArr.shift()) : 0;
mas01mj@732 579 var utc:Number = Date.UTC(year, month-1, date, hour, minutes, seconds, milliseconds);
mas01mj@732 580 var offset:Number = (((offsetHours * 3600000) + (offsetMinutes * 60000)) * multiplier);
mas01mj@732 581 finalDate = new Date(utc - offset);
mas01mj@732 582
mas01mj@732 583 if (finalDate.toString() == "Invalid Date")
mas01mj@732 584 {
mas01mj@732 585 throw new Error("This date does not conform to W3CDTF.");
mas01mj@732 586 }
mas01mj@732 587 }
mas01mj@732 588 catch (e:Error)
mas01mj@732 589 {
mas01mj@732 590 var eStr:String = "Unable to parse the string [" +str+ "] into a date. ";
mas01mj@732 591 eStr += "The internal error was: " + e.toString();
mas01mj@732 592 throw new Error(eStr);
mas01mj@732 593 }
mas01mj@732 594 return finalDate;
mas01mj@732 595 }
mas01mj@732 596
mas01mj@732 597 /**
mas01mj@732 598 * Returns a date string formatted according to W3CDTF.
mas01mj@732 599 *
mas01mj@732 600 * @param d
mas01mj@732 601 * @param includeMilliseconds Determines whether to include the
mas01mj@732 602 * milliseconds value (if any) in the formatted string.
mas01mj@732 603 *
mas01mj@732 604 * @returns
mas01mj@732 605 *
mas01mj@732 606 * @langversion ActionScript 3.0
mas01mj@732 607 * @playerversion Flash 9.0
mas01mj@732 608 * @tiptext
mas01mj@732 609 *
mas01mj@732 610 * @see http://www.w3.org/TR/NOTE-datetime
mas01mj@732 611 */
mas01mj@732 612 public static function toW3CDTF(d:Date,includeMilliseconds:Boolean=false):String
mas01mj@732 613 {
mas01mj@732 614 var date:Number = d.getUTCDate();
mas01mj@732 615 var month:Number = d.getUTCMonth();
mas01mj@732 616 var hours:Number = d.getUTCHours();
mas01mj@732 617 var minutes:Number = d.getUTCMinutes();
mas01mj@732 618 var seconds:Number = d.getUTCSeconds();
mas01mj@732 619 var milliseconds:Number = d.getUTCMilliseconds();
mas01mj@732 620 var sb:String = new String();
mas01mj@732 621
mas01mj@732 622 sb += d.getUTCFullYear();
mas01mj@732 623 sb += "-";
mas01mj@732 624
mas01mj@732 625 //thanks to "dom" who sent in a fix for the line below
mas01mj@732 626 if (month + 1 < 10)
mas01mj@732 627 {
mas01mj@732 628 sb += "0";
mas01mj@732 629 }
mas01mj@732 630 sb += month + 1;
mas01mj@732 631 sb += "-";
mas01mj@732 632 if (date < 10)
mas01mj@732 633 {
mas01mj@732 634 sb += "0";
mas01mj@732 635 }
mas01mj@732 636 sb += date;
mas01mj@732 637 sb += "T";
mas01mj@732 638 if (hours < 10)
mas01mj@732 639 {
mas01mj@732 640 sb += "0";
mas01mj@732 641 }
mas01mj@732 642 sb += hours;
mas01mj@732 643 sb += ":";
mas01mj@732 644 if (minutes < 10)
mas01mj@732 645 {
mas01mj@732 646 sb += "0";
mas01mj@732 647 }
mas01mj@732 648 sb += minutes;
mas01mj@732 649 sb += ":";
mas01mj@732 650 if (seconds < 10)
mas01mj@732 651 {
mas01mj@732 652 sb += "0";
mas01mj@732 653 }
mas01mj@732 654 sb += seconds;
mas01mj@732 655 if (includeMilliseconds && milliseconds > 0)
mas01mj@732 656 {
mas01mj@732 657 sb += ".";
mas01mj@732 658 sb += milliseconds;
mas01mj@732 659 }
mas01mj@732 660 sb += "-00:00";
mas01mj@732 661 return sb;
mas01mj@732 662 }
mas01mj@732 663
mas01mj@732 664 /**
mas01mj@732 665 * Converts a date into just after midnight.
mas01mj@732 666 */
mas01mj@732 667 public static function makeMorning(d:Date):Date
mas01mj@732 668 {
mas01mj@732 669 var d:Date = new Date(d.time);
mas01mj@732 670 d.hours = 0;
mas01mj@732 671 d.minutes = 0;
mas01mj@732 672 d.seconds = 0;
mas01mj@732 673 d.milliseconds = 0;
mas01mj@732 674 return d;
mas01mj@732 675 }
mas01mj@732 676
mas01mj@732 677 /**
mas01mj@732 678 * Converts a date into just befor midnight.
mas01mj@732 679 */
mas01mj@732 680 public static function makeNight(d:Date):Date
mas01mj@732 681 {
mas01mj@732 682 var d:Date = new Date(d.time);
mas01mj@732 683 d.hours = 23;
mas01mj@732 684 d.minutes = 59;
mas01mj@732 685 d.seconds = 59;
mas01mj@732 686 d.milliseconds = 999;
mas01mj@732 687 return d;
mas01mj@732 688 }
mas01mj@732 689
mas01mj@732 690 /**
mas01mj@732 691 * Sort of converts a date into UTC.
mas01mj@732 692 */
mas01mj@732 693 public static function getUTCDate(d:Date):Date
mas01mj@732 694 {
mas01mj@732 695 var nd:Date = new Date();
mas01mj@732 696 var offset:Number = d.getTimezoneOffset() * 60 * 1000;
mas01mj@732 697 nd.setTime(d.getTime() + offset);
mas01mj@732 698 return nd;
mas01mj@732 699 }
mas01mj@732 700 }
mas01mj@732 701 }