Flex Time Zone Management

This is my second post on the Flex time zone topic. The first post has a fix that deals with changing the timezones around.  The fix works but it turns into a lot more work than really is needed.  After writing the post I did some more research on Epoch Dates. I wanted to make this post quick and dirty with code so here is what I got.

First are my Actionscript 3 Date Util functions that I added to my DateUtils.as class.  The epochToDate function converts the Epoch number that I am receiving from ColdFusion and converts it into Flex datetime.  The dateToEpoch does the opposite and converts the Date to an Epoch number.


//Returns Date from Epoch Number
public static function epochToDate(Epoch:Number):Date {
var gmtDate:Date = new Date();
gmtDate.setTime(Math.round((Epoch*1000)+(gmtDate.timezoneOffset*60*1000)));
return gmtDate;
}

 

 

public static function dateToEpoch(fixDate:Date):Number{

var epoch:Number = Math.round(fixDate.time/1000);

return epoch;

}

 

 

Here are some more functions that I use to do conversions in ColdFusion and SQL.

 

 

<!--- HELPER FUNCTIONS --->

<!--- function to convert epoch date into normal date --->

<cffunction name="covertEpochToDate" access="public" returntype="string" output="false">

<cfargument name="eDate" type="string" required="true">

<cfreturn DateAdd("s",arguments.eDate,DateConvert("utc2Local", "January 1 1970 00:00")) />

</cffunction>

 

 

Example SQL Select statement that returns Date as Epoch Number

 

SELECT

DATEDIFF(s, '1970-01-01 00:00:00', a.YOURDATE) as EpochNumber

FROM YOUR_TABLE a

 

Here are some more resources if you need to do some more research.  Thanks and have a nice day! :)

http://www.coldfusioncookbook.com/entry/13/You-have-a-date,-time,-or-both-and-you-need-to-convert-it-to-Epoch-seconds

http://www.brooks-bilson.com/blogs/rob/index.cfm/2007/10/11/Some-Notes-on-Using-Epoch-Time-in-ColdFusion

 

 

del.icio.us Digg StumbleUpon Facebook Google Bookmarks DZone
| View count: 3555
blog comments powered by Disqus