W3CDateTime ActionScript Class

As part of the ActionScript 2 Atom class that I am putting together, I have created an ActionScript class that represents a W3CDateTime string. This is the format that Atom uses to represent dates.

A beta of the class and simple usage example is included below. If you have any questions or suggestions (especially parsing optimizations), or find any bugs, please post them in the comments.

Here is a simple example of the class in use:

[code]import com.macromedia.date.W3CDateTime;var timeString:String = “2004-02-05T16:36:35-05:00”;trace(timeString);var d:W3CDateTime = new W3CDateTime(timeString);trace(d);trace(new Date(d.getTime()));[/code]

Here is the entire class:

[code]/*W3CDateTime.asMike Chambersmesh@macromedia.comRepresents a date in the W3CDateTime format as described inhttp://www.w3.org/TR/NOTE-datetimeversion .60Known Issues:-The class does not support decimal seconds. Any decimal secondsspecificed in a W3CDateTime string will be lost / removed.TODO:-Look at optimize parsing code-Extensive Testing-Look into supporting decimal secondsThanks to:Tatsuo Kato, Peter Hall and Gary Grossman for help with determining theUTC offsets.*//*Extends the Date object*/class com.macromedia.date.W3CDateTime extends Date{/*Constructor. Optionally takes a WC3DateTime string that the instancewill represent.Otherwise, it represents the time that the instance was created.*/public function W3CDateTime(dateString:String){if(dateString != undefined){setDateTimeString(dateString);}}/*Takes a W3CDateTime formatted string and sets the instance to representthe date and time represented in that string*/public function setDateTimeString(dateString:String):Void{//this.dateString = dateString;setTime(W3CDateTime.parseString(dateString).getTime());}/*Returns a W3CDateTime formatted string that represents the date representedby the current instance.Takes 4 optional arguments which specifies the format of the string:incMonth : Whether the month should be included.incDay : Whether the day of the month should be included.incHours : Whether the hours and minutes should be included.incSeconds : Whether the seconds should be included.Note, that all output will be a valid W3CDataTime item.*/public function getDateTimeString(incMonth:Boolean,incDay:Boolean,incHours:Boolean,incSeconds:Boolean):String{return W3CDateTime.parseDate(this, incMonth, incDay, incHours, incSeconds);}/*Returns the full W3CDateTime string represented by the instance.*/public function toString(Void):String{return getDateTimeString(true, true, true, true);}/*Returns the Year the W3CDateTime represents in the following format:YYYY*/public function getW3CYear(Void):String{return getFullYear().toString();}/*Returns the Month the W3CDateTime represents in the following format:MMwhere 1 = January*/public function getW3CMonth(Void):String{return (getMonth() + 1).toString();}/*Returns the day of the month the W3CDateTime represents in thefollowing format:dd*/public function getW3CDate(Void):String{return getDate().toString();}/*Returns the hour the W3CDateTime represents in thefollowing format:hh*/public function getW3CHours(Void):String{//adjust for utc?return getHours().toString();}/*Returns the minutes the W3CDateTime represents in thefollowing format:mm*/public function getW3CMinutes(Void):String{return getMinutes().toString();}/*Returns the seconds the W3CDateTime represents in thefollowing format:ss*/public function getW3CSeconds(Void):String{return getMinutes().toString();}/*Returns the UTC offset the W3CDateTime represents in thefollowing format:+/-hh:mmNote, if there is no offset, it will returnZ*/public function getW3CUTCOffset(Void):String{return W3CDateTime.formatTimezoneOffset(this);}/*Static functions that takes a String representation of a number and addsa leading 0 if it is a single digit.*/public static function padDigit(digit:String):String{if(digit.length -1)? “+” : “-“;if(timezoneOffset < 0){timezoneOffset *= -1;}//what if offset if 0 or lessvar hours:String = (timezoneOffset / 60).toString();var seconds:String = (timezoneOffset – (hours * 60)).toString();return operator + W3CDateTime.padDigit(hours) +":" + W3CDateTime.padDigit(seconds);}/*Static method that takes a W3CDateTime string and returns anActionScript Date object that represents the string passed in.*/public static function parseString(dateString:String):Date{if(dateString == undefined || dateString.length < 1){return undefined;}var YYYY:String = dateString.substr(0, 4);var MM:String = dateString.substr(5, 2);var DD:String = dateString.substr(8, 2);var hh:String = dateString.substr(11, 2);var mm:String = dateString.substr(14, 2);var ss:String = dateString.substr(17, 2);var s:String = "";var offsetOperator:String;var offsetHours:String;var offsetMinutes:String;var offset:Number = 0;var operator:Number;var indexBump:Number = 0;if(dateString.substr(19, 1) == "."){//they have a decimal seconds = dateString.substr(20, 2);indexBump = 3;}else{indexBump = 0;//they dont have a decimal second}offsetOperator = dateString.substr(19 + indexBump,1);offsetHours = dateString.substr(20 + indexBump,2);offsetMinutes = dateString.substr(23 + indexBump,2);//this could be wrong if there is no operatoroperator = ((offsetOperator == "-") || (offsetOperator == undefined))? 1 : -1;//this could be wrong if offsethours or offsetminutes is undefinedoffset = operator * (Number(offsetHours) * 60) + Number(offsetMinutes);if(isNaN(offset)){offset = 0;}var minutes:Number;if(mm != ""){minutes = Number(mm) + offset;}var month:Number;if(MM != ""){month = Number(MM) – 1;}var year:Number;if(YYYY != ""){year = Number(YYYY);}var day:Number;if(DD != ""){day = Number(DD);}var hours:Number;if(hh != ""){hours = Number(hh);}var seconds:Number;if(ss != ""){seconds = Number(ss);}var utcTimeMS = Date.UTC(year,month,day,hours,minutes,seconds);return new Date(utcTimeMS);;}}[/code]

2 Responses to W3CDateTime ActionScript Class

  1. Gel says:

    Oh good grief! Now this is ridiculous when they start showing up in guestbooks and comment forms!

  2. Great class, It came very handy.I ran into a small problem when parsing time values with multiple decimal fractions of a second.Basically I changed this in the parseString() method:[code]//they have a decimal seconds = dateString.substr(20, 2);indexBump = 3;[/code]to this:[code]var sEnd = dateString.indexOf(“-“,20);if(sEnd == -1) sEnd = dateString.length*1;s = dateString.substring(20,sEnd);indexBump = s.length+1;[/code]Regards,