热门文章 | 热门软件| 热门源码 | 热门电影 | 知识库 | 联系我们
软件 源码 教程 影视 健康 招聘
  HTML | JavaScript | ASP | PHP | JSP | NET | VB | VC | VF | Windows | Linux | Mysql | Mssql | Oracle | Struts 
当前位置: 创世纪计算机资源网 -> 文章频道 ->flash 
站内搜索:
如何获取国际标准时间
作者:flexs.cn 来源:blog 整理日期:2007-11-15

为了制作奥运倒计时,得想个办法获得到国际标准时间。

首先想到的便是Webservices,通用,易用。

但是Flash CS3并没有提供Webservices组件。于是使用了Roading的《flash9/flash cs3(as3)通过soap访问Web Services》。
好了,方法有了,那么Service源呢,这个变难了,找了几个提供Webservices的网站
如:
http://www.webservicex.net/
http://www.webservices.org/

但都未找到这项服务。

继续上网冲浪...

得到了《获取Nist的美国官方标准时间的解决办法》
作者用的是C#实现的,仔细看了下,用AS3制作貌似差不多。
他使用的是http://nist.time.gov/timezone.cgi?UTC/s/0先得到标准时间,然后转为北京八区时间。
实际上有更容易的方法,就是将地址中的0换成8,就是东八区的标准时间了,也就是:
http://nist.time.gov/timezone.cgi?UTC/s/8

部分代码:

 程序代码

        private function getTheTime():void
        {
            var request:URLRequest = new URLRequest("http://nist.time.gov/timezone.cgi?UTC/s/8?rnd="+Math.random());
            request.contentType = "text/xml";
            request.method = URLRequestMethod.POST;
            loader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, loadComplete);
            try
            {
                loader.load(request);
            }
            catch (error:Error)
            {
                trace("Load Error!");
            }
        }
       
       
        private function loadComplete(event:Event):void
        {
            var result:String = String(loader.data);
            trace(result);
        }


代码中的rnd="+Math.random()是用来防止Flash读取缓存,得不到最新数据
loadComplete方法中的result就是得到的网页源码,通过字串匹配就可得到时间字符串了.

OK,回过头来接着研究Webservices。找到了http://www.worldtimeserver.com。
(http://www.worldtimeserver.com/current_time_in_CN.aspx也可以访问这个页,使用上面的方法提取出北京时间)

使用Roading的方法:

 程序代码

        private function getTheTime2():void
        {
            var soap:Namespace = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
            var req:URLRequest = new URLRequest("http://www.worldtimeserver.com/wtsservice/localtime.asmx?op=GetLocalDateTimeString");
            req.method = URLRequestMethod.POST;
            req.requestHeaders.push(new URLRequestHeader("Content-Type", "text/xml;charset=utf-8"));
            req.requestHeaders.push(new URLRequestHeader("SOAPAction",   "http://worldtimeserver/wtsservice/localtime/GetLocalDateTimeString"));
 
            var rXML:XML =
                <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                    <soap:Body/>
                </soap:Envelope>
                ;
           
            rXML.soap::Body.appendChild(
                <GetLocalDateTimeString xmlns="http://worldtimeserver/wtsservice/localtime">
                    <User>用户名</User>
                    <Password>密码</Password>
                    <WTSLocationID>US-NY</WTSLocationID>
                </GetLocalDateTimeString>
                );

            req.data =  rXML;

            loader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.TEXT;
            loader.addEventListener("ioError", ioSOAPError);
            loader.addEventListener(Event.COMPLETE, SOAPLoaded);
            loader.load(req);
        }
       
        private function ioSOAPError(event:Error):void
        {
            trace("Load Error!");
        }
       
        private function SOAPLoaded(d:Event):void
        {
            var _xml:XML = new XML(loader.data);
            trace(_xml);
        }


之所以用“用户名”和“密码”来填充,因为这个站的服务是收费的。

此外,还发现了一个更容易的方法,来测试一下北京奥委会网站:http://time.beijing2008.cn/mtime.php
(不能保证此链接永久有效),嘿嘿,知道怎么用了吧。

再来测试:http://www.time.ac.cn/timeflash.asp?user=flash
返回的是一个XML格式字串,处理起来更为容易些。

这样,目前有四个地址可以得到国际标准时间

以上,有知道其他方法的可以留言补充

相关文章