如何获取国际标准时间

如何获取国际标准时间

  (2007-10-29 23:53:44)
标签: 

知识/探索



转自:http://blog.sina.com.cn/s/blog_4ca652f901000cj7.html
  为了制作 奥运倒计时,得想个办法获得到国际标准时间。

首先想到的便是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-instancexmlns:xsd=" http://www.w3.org/2001/XMLSchemaxmlns: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格式字串,处理起来更为容易些。

这样,目前有四个地址可以得到国际标准时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值