2   Copyright (c) 2008, Adobe Systems Incorporated
 
   5   Redistribution and use in source and binary forms, with or without 
 
   6   modification, are permitted provided that the following conditions are
 
   9   * Redistributions of source code must retain the above copyright notice, 
 
  10     this list of conditions and the following disclaimer.
 
  12   * Redistributions in binary form must reproduce the above copyright
 
  13     notice, this list of conditions and the following disclaimer in the 
 
  14     documentation and/or other materials provided with the distribution.
 
  16   * Neither the name of Adobe Systems Incorporated nor the names of its 
 
  17     contributors may be used to endorse or promote products derived from 
 
  18     this software without specific prior written permission.
 
  20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 
  21   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
  22   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
  23   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 
  24   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
  25   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
  26   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
  27   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
  28   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
  29   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
  30   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
  33 package com.adobe.utils
 
  35         import mx.formatters.DateBase;
 
  38         *       Class that contains static utility methods for manipulating and working
 
  41         *       @langversion ActionScript 3.0
 
  42         *       @playerversion Flash 9.0
 
  49                 *       Returns the English Short Month name (3 letters) for the Month that
 
  50                 *       the Date represents.    
 
  52                 *       @param d The Date instance whose month will be used to retrieve the
 
  55                 *       @return An English 3 Letter Month abbreviation.
 
  57                 *       @langversion ActionScript 3.0
 
  58                 *       @playerversion Flash 9.0
 
  63                 public static function getShortMonthName(d:Date):String
 
  65                         return DateBase.monthNamesShort[d.getMonth()];
 
  69                 *       Returns the index of the month that the short month name string
 
  72                 *       @param m The 3 letter abbreviation representing a short month name.
 
  74                 *       @param Optional parameter indicating whether the search should be case
 
  77                 *       @return A int that represents that month represented by the specifed
 
  80                 *       @langversion ActionScript 3.0
 
  81                 *       @playerversion Flash 9.0
 
  86                 public static function getShortMonthIndex(m:String):int
 
  88                         return DateBase.monthNamesShort.indexOf(m);
 
  92                 *       Returns the English full Month name for the Month that
 
  93                 *       the Date represents.    
 
  95                 *       @param d The Date instance whose month will be used to retrieve the
 
  98                 *       @return An English full month name.
 
 100                 *       @langversion ActionScript 3.0
 
 101                 *       @playerversion Flash 9.0
 
 106                 public static function getFullMonthName(d:Date):String
 
 108                         return DateBase.monthNamesLong[d.getMonth()];   
 
 112                 *       Returns the index of the month that the full month name string
 
 115                 *       @param m A full month name.
 
 117                 *       @return A int that represents that month represented by the specifed
 
 120                 *       @langversion ActionScript 3.0
 
 121                 *       @playerversion Flash 9.0
 
 126                 public static function getFullMonthIndex(m:String):int
 
 128                         return DateBase.monthNamesLong.indexOf(m);
 
 132                 *       Returns the English Short Day name (3 letters) for the day that
 
 133                 *       the Date represents.    
 
 135                 *       @param d The Date instance whose day will be used to retrieve the
 
 138                 *       @return An English 3 Letter day abbreviation.
 
 140                 *       @langversion ActionScript 3.0
 
 141                 *       @playerversion Flash 9.0
 
 146                 public static function getShortDayName(d:Date):String
 
 148                         return DateBase.dayNamesShort[d.getDay()];      
 
 152                 *       Returns the index of the day that the short day name string
 
 155                 *       @param m A short day name.
 
 157                 *       @return A int that represents that short day represented by the specifed
 
 160                 *       @langversion ActionScript 3.0
 
 161                 *       @playerversion Flash 9.0
 
 166                 public static function getShortDayIndex(d:String):int
 
 168                         return DateBase.dayNamesShort.indexOf(d);
 
 172                 *       Returns the English full day name for the day that
 
 173                 *       the Date represents.    
 
 175                 *       @param d The Date instance whose day will be used to retrieve the
 
 178                 *       @return An English full day name.
 
 180                 *       @langversion ActionScript 3.0
 
 181                 *       @playerversion Flash 9.0
 
 186                 public static function getFullDayName(d:Date):String
 
 188                         return DateBase.dayNamesLong[d.getDay()];       
 
 192                 *       Returns the index of the day that the full day name string
 
 195                 *       @param m A full day name.
 
 197                 *       @return A int that represents that full day represented by the specifed
 
 200                 *       @langversion ActionScript 3.0
 
 201                 *       @playerversion Flash 9.0
 
 206                 public static function getFullDayIndex(d:String):int
 
 208                         return DateBase.dayNamesLong.indexOf(d);
 
 212                 *       Returns a two digit representation of the year represented by the 
 
 215                 *       @param d The Date instance whose year will be used to generate a two
 
 216                 *       digit string representation of the year.
 
 218                 *       @return A string that contains a 2 digit representation of the year.
 
 219                 *       Single digits will be padded with 0.
 
 221                 *       @langversion ActionScript 3.0
 
 222                 *       @playerversion Flash 9.0
 
 225                 public static function getShortYear(d:Date):String
 
 227                         var dStr:String = String(d.getFullYear());
 
 234                         return (dStr.substr(dStr.length - 2));
 
 238                 *       Compares two dates and returns an integer depending on their relationship.
 
 240                 *       Returns -1 if d1 is greater than d2.
 
 241                 *       Returns 1 if d2 is greater than d1.
 
 242                 *       Returns 0 if both dates are equal.
 
 244                 *       @param d1 The date that will be compared to the second date.
 
 245                 *       @param d2 The date that will be compared to the first date.
 
 247                 *       @return An int indicating how the two dates compare.
 
 249                 *       @langversion ActionScript 3.0
 
 250                 *       @playerversion Flash 9.0
 
 253                 public static function compareDates(d1:Date, d2:Date):int
 
 255                         var d1ms:Number = d1.getTime();
 
 256                         var d2ms:Number = d2.getTime();
 
 273                 *       Returns a short hour (0 - 12) represented by the specified date.
 
 275                 *       If the hour is less than 12 (0 - 11 AM) then the hour will be returned.
 
 277                 *       If the hour is greater than 12 (12 - 23 PM) then the hour minus 12
 
 280                 *       @param d1 The Date from which to generate the short hour
 
 282                 *       @return An int between 0 and 13 ( 1 - 12 ) representing the short hour.
 
 284                 *       @langversion ActionScript 3.0
 
 285                 *       @playerversion Flash 9.0
 
 288                 public static function getShortHour(d:Date):int
 
 292                         if(h == 0 || h == 12)
 
 307                 *       Returns a string indicating whether the date represents a time in the
 
 308                 *       ante meridiem (AM) or post meridiem (PM).
 
 310                 *       If the hour is less than 12 then "AM" will be returned.
 
 312                 *       If the hour is greater than 12 then "PM" will be returned.
 
 314                 *       @param d1 The Date from which to generate the 12 hour clock indicator.
 
 316                 *       @return A String ("AM" or "PM") indicating which half of the day the 
 
 319                 *       @langversion ActionScript 3.0
 
 320                 *       @playerversion Flash 9.0
 
 323                 public static function getAMPM(d:Date):String
 
 325                         return (d.hours > 11)? "PM" : "AM";
 
 329                 * Parses dates that conform to RFC822 into Date objects. This method also
 
 330                 * supports four-digit years (not supported in RFC822), but two-digit years
 
 331                 * (referring to the 20th century) are fine, too.
 
 333                 * This function is useful for parsing RSS .91, .92, and 2.0 dates.
 
 339                 * @langversion ActionScript 3.0
 
 340                 * @playerversion Flash 9.0
 
 343                 * @see http://asg.web.cmu.edu/rfc/rfc822.html
 
 345                 public static function parseRFC822(str:String):Date
 
 350                                 var dateParts:Array = str.split(" ");
 
 351                                 var day:String = null;
 
 353                                 if (dateParts[0].search(/\d/) == -1)
 
 355                                         day = dateParts.shift().replace(/\W/, "");
 
 358                                 var date:Number = Number(dateParts.shift());
 
 359                                 var month:Number = Number(DateUtil.getShortMonthIndex(dateParts.shift()));
 
 360                                 var year:Number = Number(dateParts.shift());
 
 361                                 var timeParts:Array = dateParts.shift().split(":");
 
 362                                 var hour:Number = int(timeParts.shift());
 
 363                                 var minute:Number = int(timeParts.shift());
 
 364                                 var second:Number = (timeParts.length > 0) ? int(timeParts.shift()): 0;
 
 366                                 var milliseconds:Number = Date.UTC(year, month, date, hour, minute, second, 0);
 
 368                                 var timezone:String = dateParts.shift();
 
 369                                 var offset:Number = 0;
 
 371                                 if (timezone.search(/\d/) == -1)
 
 385                                                         offset = (-5 * 3600000);
 
 388                                                         offset = (-4 * 3600000);
 
 391                                                         offset = (-6 * 3600000);
 
 394                                                         offset = (-5 * 3600000);
 
 397                                                         offset = (-7 * 3600000);
 
 400                                                         offset = (-6 * 3600000);
 
 403                                                         offset = (-8 * 3600000);
 
 406                                                         offset = (-7 * 3600000);
 
 412                                                         offset = (-1 * 3600000);
 
 415                                                         offset = (-12 * 3600000);
 
 418                                                         offset = (1 * 3600000);
 
 421                                                         offset = (12 * 3600000);
 
 429                                         var multiplier:Number = 1;
 
 430                                         var oHours:Number = 0;
 
 431                                         var oMinutes:Number = 0;
 
 432                                         if (timezone.length != 4)
 
 434                                                 if (timezone.charAt(0) == "-")
 
 438                                                 timezone = timezone.substr(1, 4);
 
 440                                         oHours = Number(timezone.substr(0, 2));
 
 441                                         oMinutes = Number(timezone.substr(2, 2));
 
 442                                         offset = (((oHours * 3600000) + (oMinutes * 60000)) * multiplier);
 
 445                                 finalDate = new Date(milliseconds - offset);
 
 447                                 if (finalDate.toString() == "Invalid Date")
 
 449                                         throw new Error("This date does not conform to RFC822.");
 
 454                                 var eStr:String = "Unable to parse the string [" +str+ "] into a date. ";
 
 455                                 eStr += "The internal error was: " + e.toString();
 
 456                                 throw new Error(eStr);
 
 462                 * Returns a date string formatted according to RFC822.
 
 468                 * @langversion ActionScript 3.0
 
 469                 * @playerversion Flash 9.0
 
 472                 * @see http://asg.web.cmu.edu/rfc/rfc822.html
 
 474                 public static function toRFC822(d:Date):String
 
 476                         var date:Number = d.getUTCDate();
 
 477                         var hours:Number = d.getUTCHours();
 
 478                         var minutes:Number = d.getUTCMinutes();
 
 479                         var seconds:Number = d.getUTCSeconds();
 
 480                         var sb:String = new String();
 
 481                         sb += DateBase.dayNamesShort[d.getUTCDay()];
 
 490                         //sb += DateUtil.SHORT_MONTH[d.getUTCMonth()];
 
 491                         sb += DateBase.monthNamesShort[d.getUTCMonth()];
 
 493                         sb += d.getUTCFullYear();
 
 517                 * Parses dates that conform to the W3C Date-time Format into Date objects.
 
 519                 * This function is useful for parsing RSS 1.0 and Atom 1.0 dates.
 
 525                 * @langversion ActionScript 3.0
 
 526                 * @playerversion Flash 9.0
 
 529                 * @see http://www.w3.org/TR/NOTE-datetime
 
 531                 public static function parseW3CDTF(str:String):Date
 
 536                                 var dateStr:String = str.substring(0, str.indexOf("T"));
 
 537                                 var timeStr:String = str.substring(str.indexOf("T")+1, str.length);
 
 538                                 var dateArr:Array = dateStr.split("-");
 
 539                                 var year:Number = Number(dateArr.shift());
 
 540                                 var month:Number = Number(dateArr.shift());
 
 541                                 var date:Number = Number(dateArr.shift());
 
 543                                 var multiplier:Number;
 
 544                                 var offsetHours:Number;
 
 545                                 var offsetMinutes:Number;
 
 546                                 var offsetStr:String;
 
 548                                 if (timeStr.indexOf("Z") != -1)
 
 553                                         timeStr = timeStr.replace("Z", "");
 
 555                                 else if (timeStr.indexOf("+") != -1)
 
 558                                         offsetStr = timeStr.substring(timeStr.indexOf("+")+1, timeStr.length);
 
 559                                         offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":")));
 
 560                                         offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1, offsetStr.length));
 
 561                                         timeStr = timeStr.substring(0, timeStr.indexOf("+"));
 
 566                                         offsetStr = timeStr.substring(timeStr.indexOf("-")+1, timeStr.length);
 
 567                                         offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":")));
 
 568                                         offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1, offsetStr.length));
 
 569                                         timeStr = timeStr.substring(0, timeStr.indexOf("-"));
 
 571                                 var timeArr:Array = timeStr.split(":");
 
 572                                 var hour:Number = Number(timeArr.shift());
 
 573                                 var minutes:Number = Number(timeArr.shift());
 
 574                                 var secondsArr:Array = (timeArr.length > 0) ? String(timeArr.shift()).split(".") : null;
 
 575                                 var seconds:Number = (secondsArr != null && secondsArr.length > 0) ? Number(secondsArr.shift()) : 0;
 
 576                                 var milliseconds:Number = (secondsArr != null && secondsArr.length > 0) ? Number(secondsArr.shift()) : 0;
 
 577                                 var utc:Number = Date.UTC(year, month-1, date, hour, minutes, seconds, milliseconds);
 
 578                                 var offset:Number = (((offsetHours * 3600000) + (offsetMinutes * 60000)) * multiplier);
 
 579                                 finalDate = new Date(utc - offset);
 
 581                                 if (finalDate.toString() == "Invalid Date")
 
 583                                         throw new Error("This date does not conform to W3CDTF.");
 
 588                                 var eStr:String = "Unable to parse the string [" +str+ "] into a date. ";
 
 589                                 eStr += "The internal error was: " + e.toString();
 
 590                                 throw new Error(eStr);
 
 596                 * Returns a date string formatted according to W3CDTF.
 
 599                 * @param includeMilliseconds Determines whether to include the
 
 600                 * milliseconds value (if any) in the formatted string.
 
 604                 * @langversion ActionScript 3.0
 
 605                 * @playerversion Flash 9.0
 
 608                 * @see http://www.w3.org/TR/NOTE-datetime
 
 610                 public static function toW3CDTF(d:Date,includeMilliseconds:Boolean=false):String
 
 612                         var date:Number = d.getUTCDate();
 
 613                         var month:Number = d.getUTCMonth();
 
 614                         var hours:Number = d.getUTCHours();
 
 615                         var minutes:Number = d.getUTCMinutes();
 
 616                         var seconds:Number = d.getUTCSeconds();
 
 617                         var milliseconds:Number = d.getUTCMilliseconds();
 
 618                         var sb:String = new String();
 
 620                         sb += d.getUTCFullYear();
 
 623                         //thanks to "dom" who sent in a fix for the line below
 
 653                         if (includeMilliseconds && milliseconds > 0)
 
 663                  * Converts a date into just after midnight.
 
 665                 public static function makeMorning(d:Date):Date
 
 667                         var d:Date = new Date(d.time);
 
 676                  * Converts a date into just befor midnight.
 
 678                 public static function makeNight(d:Date):Date
 
 680                         var d:Date = new Date(d.time);
 
 684             d.milliseconds = 999;                               
 
 689                  * Sort of converts a date into UTC.
 
 691                 public static function getUTCDate(d:Date):Date
 
 693                         var nd:Date = new Date();
 
 694                         var offset:Number = d.getTimezoneOffset() * 60 * 1000; 
 
 695                         nd.setTime(d.getTime() + offset);