Sunday, March 15, 2009

Getting The Greenwich Mean Time (GMT)



Someone asked this on a Flash BBS:
"How do I get the Greenwich mean time instead of the local time on the client's PC?"

My first thought was to find a web page that gives the current GMT.

And I found this!
http://www.timeanddate.com/worldclock/

The rest is rather simple.
Just load this page using the URLLoader class and do whatever you can come up with to splice out the GMT information.

My approach is through regular expression:


d{1,2}:\d{1,2}:\d{1,2}
//This means two colons splitting three blocks of one to two digits.

So here's the complete code for getting the GMT:

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, traceTime);
loader.load(new URLRequest("http://www.timeanddate.com/worldclock/"));

function traceTime(e:Event):void {
var str:String = URLLoader(e.target).data;
var time:String = str.match(/\d{1,2}:\d{1,2}:\d{1,2}/)[0];
trace(time);
}

Oops, later on I found out that Google and Yahoo already provide a GMT inquiry service.
So my method was an overkill :p