mas01mj@732: /* mas01mj@732: Copyright (c) 2008, Adobe Systems Incorporated mas01mj@732: All rights reserved. mas01mj@732: mas01mj@732: Redistribution and use in source and binary forms, with or without mas01mj@732: modification, are permitted provided that the following conditions are mas01mj@732: met: mas01mj@732: mas01mj@732: * Redistributions of source code must retain the above copyright notice, mas01mj@732: this list of conditions and the following disclaimer. mas01mj@732: mas01mj@732: * Redistributions in binary form must reproduce the above copyright mas01mj@732: notice, this list of conditions and the following disclaimer in the mas01mj@732: documentation and/or other materials provided with the distribution. mas01mj@732: mas01mj@732: * Neither the name of Adobe Systems Incorporated nor the names of its mas01mj@732: contributors may be used to endorse or promote products derived from mas01mj@732: this software without specific prior written permission. mas01mj@732: mas01mj@732: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS mas01mj@732: IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, mas01mj@732: THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR mas01mj@732: PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR mas01mj@732: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, mas01mj@732: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, mas01mj@732: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR mas01mj@732: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF mas01mj@732: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING mas01mj@732: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS mas01mj@732: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. mas01mj@732: */ mas01mj@732: mas01mj@732: package com.adobe.utils mas01mj@732: { mas01mj@732: import mx.formatters.DateBase; mas01mj@732: mas01mj@732: /** mas01mj@732: * Class that contains static utility methods for manipulating and working mas01mj@732: * with Dates. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public class DateUtil mas01mj@732: { mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns the English Short Month name (3 letters) for the Month that mas01mj@732: * the Date represents. mas01mj@732: * mas01mj@732: * @param d The Date instance whose month will be used to retrieve the mas01mj@732: * short month name. mas01mj@732: * mas01mj@732: * @return An English 3 Letter Month abbreviation. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see SHORT_MONTH mas01mj@732: */ mas01mj@732: public static function getShortMonthName(d:Date):String mas01mj@732: { mas01mj@732: return DateBase.monthNamesShort[d.getMonth()]; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns the index of the month that the short month name string mas01mj@732: * represents. mas01mj@732: * mas01mj@732: * @param m The 3 letter abbreviation representing a short month name. mas01mj@732: * mas01mj@732: * @param Optional parameter indicating whether the search should be case mas01mj@732: * sensitive mas01mj@732: * mas01mj@732: * @return A int that represents that month represented by the specifed mas01mj@732: * short name. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see SHORT_MONTH mas01mj@732: */ mas01mj@732: public static function getShortMonthIndex(m:String):int mas01mj@732: { mas01mj@732: return DateBase.monthNamesShort.indexOf(m); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns the English full Month name for the Month that mas01mj@732: * the Date represents. mas01mj@732: * mas01mj@732: * @param d The Date instance whose month will be used to retrieve the mas01mj@732: * full month name. mas01mj@732: * mas01mj@732: * @return An English full month name. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see FULL_MONTH mas01mj@732: */ mas01mj@732: public static function getFullMonthName(d:Date):String mas01mj@732: { mas01mj@732: return DateBase.monthNamesLong[d.getMonth()]; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns the index of the month that the full month name string mas01mj@732: * represents. mas01mj@732: * mas01mj@732: * @param m A full month name. mas01mj@732: * mas01mj@732: * @return A int that represents that month represented by the specifed mas01mj@732: * full month name. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see FULL_MONTH mas01mj@732: */ mas01mj@732: public static function getFullMonthIndex(m:String):int mas01mj@732: { mas01mj@732: return DateBase.monthNamesLong.indexOf(m); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns the English Short Day name (3 letters) for the day that mas01mj@732: * the Date represents. mas01mj@732: * mas01mj@732: * @param d The Date instance whose day will be used to retrieve the mas01mj@732: * short day name. mas01mj@732: * mas01mj@732: * @return An English 3 Letter day abbreviation. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see SHORT_DAY mas01mj@732: */ mas01mj@732: public static function getShortDayName(d:Date):String mas01mj@732: { mas01mj@732: return DateBase.dayNamesShort[d.getDay()]; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns the index of the day that the short day name string mas01mj@732: * represents. mas01mj@732: * mas01mj@732: * @param m A short day name. mas01mj@732: * mas01mj@732: * @return A int that represents that short day represented by the specifed mas01mj@732: * full month name. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see SHORT_DAY mas01mj@732: */ mas01mj@732: public static function getShortDayIndex(d:String):int mas01mj@732: { mas01mj@732: return DateBase.dayNamesShort.indexOf(d); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns the English full day name for the day that mas01mj@732: * the Date represents. mas01mj@732: * mas01mj@732: * @param d The Date instance whose day will be used to retrieve the mas01mj@732: * full day name. mas01mj@732: * mas01mj@732: * @return An English full day name. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see FULL_DAY mas01mj@732: */ mas01mj@732: public static function getFullDayName(d:Date):String mas01mj@732: { mas01mj@732: return DateBase.dayNamesLong[d.getDay()]; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns the index of the day that the full day name string mas01mj@732: * represents. mas01mj@732: * mas01mj@732: * @param m A full day name. mas01mj@732: * mas01mj@732: * @return A int that represents that full day represented by the specifed mas01mj@732: * full month name. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see FULL_DAY mas01mj@732: */ mas01mj@732: public static function getFullDayIndex(d:String):int mas01mj@732: { mas01mj@732: return DateBase.dayNamesLong.indexOf(d); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns a two digit representation of the year represented by the mas01mj@732: * specified date. mas01mj@732: * mas01mj@732: * @param d The Date instance whose year will be used to generate a two mas01mj@732: * digit string representation of the year. mas01mj@732: * mas01mj@732: * @return A string that contains a 2 digit representation of the year. mas01mj@732: * Single digits will be padded with 0. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function getShortYear(d:Date):String mas01mj@732: { mas01mj@732: var dStr:String = String(d.getFullYear()); mas01mj@732: mas01mj@732: if(dStr.length < 3) mas01mj@732: { mas01mj@732: return dStr; mas01mj@732: } mas01mj@732: mas01mj@732: return (dStr.substr(dStr.length - 2)); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Compares two dates and returns an integer depending on their relationship. mas01mj@732: * mas01mj@732: * Returns -1 if d1 is greater than d2. mas01mj@732: * Returns 1 if d2 is greater than d1. mas01mj@732: * Returns 0 if both dates are equal. mas01mj@732: * mas01mj@732: * @param d1 The date that will be compared to the second date. mas01mj@732: * @param d2 The date that will be compared to the first date. mas01mj@732: * mas01mj@732: * @return An int indicating how the two dates compare. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function compareDates(d1:Date, d2:Date):int mas01mj@732: { mas01mj@732: var d1ms:Number = d1.getTime(); mas01mj@732: var d2ms:Number = d2.getTime(); mas01mj@732: mas01mj@732: if(d1ms > d2ms) mas01mj@732: { mas01mj@732: return -1; mas01mj@732: } mas01mj@732: else if(d1ms < d2ms) mas01mj@732: { mas01mj@732: return 1; mas01mj@732: } mas01mj@732: else mas01mj@732: { mas01mj@732: return 0; mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns a short hour (0 - 12) represented by the specified date. mas01mj@732: * mas01mj@732: * If the hour is less than 12 (0 - 11 AM) then the hour will be returned. mas01mj@732: * mas01mj@732: * If the hour is greater than 12 (12 - 23 PM) then the hour minus 12 mas01mj@732: * will be returned. mas01mj@732: * mas01mj@732: * @param d1 The Date from which to generate the short hour mas01mj@732: * mas01mj@732: * @return An int between 0 and 13 ( 1 - 12 ) representing the short hour. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function getShortHour(d:Date):int mas01mj@732: { mas01mj@732: var h:int = d.hours; mas01mj@732: mas01mj@732: if(h == 0 || h == 12) mas01mj@732: { mas01mj@732: return 12; mas01mj@732: } mas01mj@732: else if(h > 12) mas01mj@732: { mas01mj@732: return h - 12; mas01mj@732: } mas01mj@732: else mas01mj@732: { mas01mj@732: return h; mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns a string indicating whether the date represents a time in the mas01mj@732: * ante meridiem (AM) or post meridiem (PM). mas01mj@732: * mas01mj@732: * If the hour is less than 12 then "AM" will be returned. mas01mj@732: * mas01mj@732: * If the hour is greater than 12 then "PM" will be returned. mas01mj@732: * mas01mj@732: * @param d1 The Date from which to generate the 12 hour clock indicator. mas01mj@732: * mas01mj@732: * @return A String ("AM" or "PM") indicating which half of the day the mas01mj@732: * hour represents. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function getAMPM(d:Date):String mas01mj@732: { mas01mj@732: return (d.hours > 11)? "PM" : "AM"; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Parses dates that conform to RFC822 into Date objects. This method also mas01mj@732: * supports four-digit years (not supported in RFC822), but two-digit years mas01mj@732: * (referring to the 20th century) are fine, too. mas01mj@732: * mas01mj@732: * This function is useful for parsing RSS .91, .92, and 2.0 dates. mas01mj@732: * mas01mj@732: * @param str mas01mj@732: * mas01mj@732: * @returns mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see http://asg.web.cmu.edu/rfc/rfc822.html mas01mj@732: */ mas01mj@732: public static function parseRFC822(str:String):Date mas01mj@732: { mas01mj@732: var finalDate:Date; mas01mj@732: try mas01mj@732: { mas01mj@732: var dateParts:Array = str.split(" "); mas01mj@732: var day:String = null; mas01mj@732: mas01mj@732: if (dateParts[0].search(/\d/) == -1) mas01mj@732: { mas01mj@732: day = dateParts.shift().replace(/\W/, ""); mas01mj@732: } mas01mj@732: mas01mj@732: var date:Number = Number(dateParts.shift()); mas01mj@732: var month:Number = Number(DateUtil.getShortMonthIndex(dateParts.shift())); mas01mj@732: var year:Number = Number(dateParts.shift()); mas01mj@732: var timeParts:Array = dateParts.shift().split(":"); mas01mj@732: var hour:Number = int(timeParts.shift()); mas01mj@732: var minute:Number = int(timeParts.shift()); mas01mj@732: var second:Number = (timeParts.length > 0) ? int(timeParts.shift()): 0; mas01mj@732: mas01mj@732: var milliseconds:Number = Date.UTC(year, month, date, hour, minute, second, 0); mas01mj@732: mas01mj@732: var timezone:String = dateParts.shift(); mas01mj@732: var offset:Number = 0; mas01mj@732: mas01mj@732: if (timezone.search(/\d/) == -1) mas01mj@732: { mas01mj@732: switch(timezone) mas01mj@732: { mas01mj@732: case "UT": mas01mj@732: offset = 0; mas01mj@732: break; mas01mj@732: case "UTC": mas01mj@732: offset = 0; mas01mj@732: break; mas01mj@732: case "GMT": mas01mj@732: offset = 0; mas01mj@732: break; mas01mj@732: case "EST": mas01mj@732: offset = (-5 * 3600000); mas01mj@732: break; mas01mj@732: case "EDT": mas01mj@732: offset = (-4 * 3600000); mas01mj@732: break; mas01mj@732: case "CST": mas01mj@732: offset = (-6 * 3600000); mas01mj@732: break; mas01mj@732: case "CDT": mas01mj@732: offset = (-5 * 3600000); mas01mj@732: break; mas01mj@732: case "MST": mas01mj@732: offset = (-7 * 3600000); mas01mj@732: break; mas01mj@732: case "MDT": mas01mj@732: offset = (-6 * 3600000); mas01mj@732: break; mas01mj@732: case "PST": mas01mj@732: offset = (-8 * 3600000); mas01mj@732: break; mas01mj@732: case "PDT": mas01mj@732: offset = (-7 * 3600000); mas01mj@732: break; mas01mj@732: case "Z": mas01mj@732: offset = 0; mas01mj@732: break; mas01mj@732: case "A": mas01mj@732: offset = (-1 * 3600000); mas01mj@732: break; mas01mj@732: case "M": mas01mj@732: offset = (-12 * 3600000); mas01mj@732: break; mas01mj@732: case "N": mas01mj@732: offset = (1 * 3600000); mas01mj@732: break; mas01mj@732: case "Y": mas01mj@732: offset = (12 * 3600000); mas01mj@732: break; mas01mj@732: default: mas01mj@732: offset = 0; mas01mj@732: } mas01mj@732: } mas01mj@732: else mas01mj@732: { mas01mj@732: var multiplier:Number = 1; mas01mj@732: var oHours:Number = 0; mas01mj@732: var oMinutes:Number = 0; mas01mj@732: if (timezone.length != 4) mas01mj@732: { mas01mj@732: if (timezone.charAt(0) == "-") mas01mj@732: { mas01mj@732: multiplier = -1; mas01mj@732: } mas01mj@732: timezone = timezone.substr(1, 4); mas01mj@732: } mas01mj@732: oHours = Number(timezone.substr(0, 2)); mas01mj@732: oMinutes = Number(timezone.substr(2, 2)); mas01mj@732: offset = (((oHours * 3600000) + (oMinutes * 60000)) * multiplier); mas01mj@732: } mas01mj@732: mas01mj@732: finalDate = new Date(milliseconds - offset); mas01mj@732: mas01mj@732: if (finalDate.toString() == "Invalid Date") mas01mj@732: { mas01mj@732: throw new Error("This date does not conform to RFC822."); mas01mj@732: } mas01mj@732: } mas01mj@732: catch (e:Error) mas01mj@732: { mas01mj@732: var eStr:String = "Unable to parse the string [" +str+ "] into a date. "; mas01mj@732: eStr += "The internal error was: " + e.toString(); mas01mj@732: throw new Error(eStr); mas01mj@732: } mas01mj@732: return finalDate; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns a date string formatted according to RFC822. mas01mj@732: * mas01mj@732: * @param d mas01mj@732: * mas01mj@732: * @returns mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see http://asg.web.cmu.edu/rfc/rfc822.html mas01mj@732: */ mas01mj@732: public static function toRFC822(d:Date):String mas01mj@732: { mas01mj@732: var date:Number = d.getUTCDate(); mas01mj@732: var hours:Number = d.getUTCHours(); mas01mj@732: var minutes:Number = d.getUTCMinutes(); mas01mj@732: var seconds:Number = d.getUTCSeconds(); mas01mj@732: var sb:String = new String(); mas01mj@732: sb += DateBase.dayNamesShort[d.getUTCDay()]; mas01mj@732: sb += ", "; mas01mj@732: mas01mj@732: if (date < 10) mas01mj@732: { mas01mj@732: sb += "0"; mas01mj@732: } mas01mj@732: sb += date; mas01mj@732: sb += " "; mas01mj@732: //sb += DateUtil.SHORT_MONTH[d.getUTCMonth()]; mas01mj@732: sb += DateBase.monthNamesShort[d.getUTCMonth()]; mas01mj@732: sb += " "; mas01mj@732: sb += d.getUTCFullYear(); mas01mj@732: sb += " "; mas01mj@732: if (hours < 10) mas01mj@732: { mas01mj@732: sb += "0"; mas01mj@732: } mas01mj@732: sb += hours; mas01mj@732: sb += ":"; mas01mj@732: if (minutes < 10) mas01mj@732: { mas01mj@732: sb += "0"; mas01mj@732: } mas01mj@732: sb += minutes; mas01mj@732: sb += ":"; mas01mj@732: if (seconds < 10) mas01mj@732: { mas01mj@732: sb += "0"; mas01mj@732: } mas01mj@732: sb += seconds; mas01mj@732: sb += " GMT"; mas01mj@732: return sb; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Parses dates that conform to the W3C Date-time Format into Date objects. mas01mj@732: * mas01mj@732: * This function is useful for parsing RSS 1.0 and Atom 1.0 dates. mas01mj@732: * mas01mj@732: * @param str mas01mj@732: * mas01mj@732: * @returns mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see http://www.w3.org/TR/NOTE-datetime mas01mj@732: */ mas01mj@732: public static function parseW3CDTF(str:String):Date mas01mj@732: { mas01mj@732: var finalDate:Date; mas01mj@732: try mas01mj@732: { mas01mj@732: var dateStr:String = str.substring(0, str.indexOf("T")); mas01mj@732: var timeStr:String = str.substring(str.indexOf("T")+1, str.length); mas01mj@732: var dateArr:Array = dateStr.split("-"); mas01mj@732: var year:Number = Number(dateArr.shift()); mas01mj@732: var month:Number = Number(dateArr.shift()); mas01mj@732: var date:Number = Number(dateArr.shift()); mas01mj@732: mas01mj@732: var multiplier:Number; mas01mj@732: var offsetHours:Number; mas01mj@732: var offsetMinutes:Number; mas01mj@732: var offsetStr:String; mas01mj@732: mas01mj@732: if (timeStr.indexOf("Z") != -1) mas01mj@732: { mas01mj@732: multiplier = 1; mas01mj@732: offsetHours = 0; mas01mj@732: offsetMinutes = 0; mas01mj@732: timeStr = timeStr.replace("Z", ""); mas01mj@732: } mas01mj@732: else if (timeStr.indexOf("+") != -1) mas01mj@732: { mas01mj@732: multiplier = 1; mas01mj@732: offsetStr = timeStr.substring(timeStr.indexOf("+")+1, timeStr.length); mas01mj@732: offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":"))); mas01mj@732: offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1, offsetStr.length)); mas01mj@732: timeStr = timeStr.substring(0, timeStr.indexOf("+")); mas01mj@732: } mas01mj@732: else // offset is - mas01mj@732: { mas01mj@732: multiplier = -1; mas01mj@732: offsetStr = timeStr.substring(timeStr.indexOf("-")+1, timeStr.length); mas01mj@732: offsetHours = Number(offsetStr.substring(0, offsetStr.indexOf(":"))); mas01mj@732: offsetMinutes = Number(offsetStr.substring(offsetStr.indexOf(":")+1, offsetStr.length)); mas01mj@732: timeStr = timeStr.substring(0, timeStr.indexOf("-")); mas01mj@732: } mas01mj@732: var timeArr:Array = timeStr.split(":"); mas01mj@732: var hour:Number = Number(timeArr.shift()); mas01mj@732: var minutes:Number = Number(timeArr.shift()); mas01mj@732: var secondsArr:Array = (timeArr.length > 0) ? String(timeArr.shift()).split(".") : null; mas01mj@732: var seconds:Number = (secondsArr != null && secondsArr.length > 0) ? Number(secondsArr.shift()) : 0; mas01mj@732: //var milliseconds:Number = (secondsArr != null && secondsArr.length > 0) ? Number(secondsArr.shift()) : 0; mas01mj@732: mas01mj@732: var milliseconds:Number = (secondsArr != null && secondsArr.length > 0) ? 1000*parseFloat("0." + secondsArr.shift()) : 0; mas01mj@732: var utc:Number = Date.UTC(year, month-1, date, hour, minutes, seconds, milliseconds); mas01mj@732: var offset:Number = (((offsetHours * 3600000) + (offsetMinutes * 60000)) * multiplier); mas01mj@732: finalDate = new Date(utc - offset); mas01mj@732: mas01mj@732: if (finalDate.toString() == "Invalid Date") mas01mj@732: { mas01mj@732: throw new Error("This date does not conform to W3CDTF."); mas01mj@732: } mas01mj@732: } mas01mj@732: catch (e:Error) mas01mj@732: { mas01mj@732: var eStr:String = "Unable to parse the string [" +str+ "] into a date. "; mas01mj@732: eStr += "The internal error was: " + e.toString(); mas01mj@732: throw new Error(eStr); mas01mj@732: } mas01mj@732: return finalDate; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns a date string formatted according to W3CDTF. mas01mj@732: * mas01mj@732: * @param d mas01mj@732: * @param includeMilliseconds Determines whether to include the mas01mj@732: * milliseconds value (if any) in the formatted string. mas01mj@732: * mas01mj@732: * @returns mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: * @tiptext mas01mj@732: * mas01mj@732: * @see http://www.w3.org/TR/NOTE-datetime mas01mj@732: */ mas01mj@732: public static function toW3CDTF(d:Date,includeMilliseconds:Boolean=false):String mas01mj@732: { mas01mj@732: var date:Number = d.getUTCDate(); mas01mj@732: var month:Number = d.getUTCMonth(); mas01mj@732: var hours:Number = d.getUTCHours(); mas01mj@732: var minutes:Number = d.getUTCMinutes(); mas01mj@732: var seconds:Number = d.getUTCSeconds(); mas01mj@732: var milliseconds:Number = d.getUTCMilliseconds(); mas01mj@732: var sb:String = new String(); mas01mj@732: mas01mj@732: sb += d.getUTCFullYear(); mas01mj@732: sb += "-"; mas01mj@732: mas01mj@732: //thanks to "dom" who sent in a fix for the line below mas01mj@732: if (month + 1 < 10) mas01mj@732: { mas01mj@732: sb += "0"; mas01mj@732: } mas01mj@732: sb += month + 1; mas01mj@732: sb += "-"; mas01mj@732: if (date < 10) mas01mj@732: { mas01mj@732: sb += "0"; mas01mj@732: } mas01mj@732: sb += date; mas01mj@732: sb += "T"; mas01mj@732: if (hours < 10) mas01mj@732: { mas01mj@732: sb += "0"; mas01mj@732: } mas01mj@732: sb += hours; mas01mj@732: sb += ":"; mas01mj@732: if (minutes < 10) mas01mj@732: { mas01mj@732: sb += "0"; mas01mj@732: } mas01mj@732: sb += minutes; mas01mj@732: sb += ":"; mas01mj@732: if (seconds < 10) mas01mj@732: { mas01mj@732: sb += "0"; mas01mj@732: } mas01mj@732: sb += seconds; mas01mj@732: if (includeMilliseconds && milliseconds > 0) mas01mj@732: { mas01mj@732: sb += "."; mas01mj@732: sb += milliseconds; mas01mj@732: } mas01mj@732: sb += "-00:00"; mas01mj@732: return sb; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Converts a date into just after midnight. mas01mj@732: */ mas01mj@732: public static function makeMorning(d:Date):Date mas01mj@732: { mas01mj@732: var d:Date = new Date(d.time); mas01mj@732: d.hours = 0; mas01mj@732: d.minutes = 0; mas01mj@732: d.seconds = 0; mas01mj@732: d.milliseconds = 0; mas01mj@732: return d; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Converts a date into just befor midnight. mas01mj@732: */ mas01mj@732: public static function makeNight(d:Date):Date mas01mj@732: { mas01mj@732: var d:Date = new Date(d.time); mas01mj@732: d.hours = 23; mas01mj@732: d.minutes = 59; mas01mj@732: d.seconds = 59; mas01mj@732: d.milliseconds = 999; mas01mj@732: return d; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Sort of converts a date into UTC. mas01mj@732: */ mas01mj@732: public static function getUTCDate(d:Date):Date mas01mj@732: { mas01mj@732: var nd:Date = new Date(); mas01mj@732: var offset:Number = d.getTimezoneOffset() * 60 * 1000; mas01mj@732: nd.setTime(d.getTime() + offset); mas01mj@732: return nd; mas01mj@732: } mas01mj@732: } mas01mj@732: }