获取Nist的美国官方标准时间的解决办法

       今天在csdn上见网友在问如何获取Nist的美国官方标准时间,经摸索、查资料和自己实践后。
做出以下代码,已测试。思路其实还是很简单的:
       1、发出对于指定网页的访问请求
       2、获取返回的html文件,进行模式匹配定位和分割,即取得了对应的时间和日期
       此方法,对于其他地方需从网页获取信息有可参考的。

 命名空间需添加:
using System.Net;                                   
using System.IO;                                   
using System.Text.RegularExpressions;

方法代码:
None.gif // 获取美国官方时间和日期
None.gif
         private   void  GetNistTimeUS(  out   string  time,  out   string  Data )
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//nist.time.gov的url
InBlock.gif
                string strNistUrl = "http://nist.time.gov/timezone.cgi?UTC/s/0";
InBlock.gif
InBlock.gif                
//构造并实例化一个WebRequest
InBlock.gif
                System.Net.WebRequest myHttpWebRequest = System.Net.HttpWebRequest.Create( strNistUrl );
InBlock.gif                
//设置连接超时时间
InBlock.gif
                myHttpWebRequest.Timeout = 8000;
InBlock.gif                
//设置WebResponse,接收返回信息
InBlock.gif
                System.Net.WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse();
InBlock.gif                
//获取返回信息流信息
InBlock.gif
                Stream sr = myHttpWebResponse.GetResponseStream();
InBlock.gif                
//设置流阅读器
InBlock.gif
                StreamReader reader = new StreamReader(sr, System.Text.Encoding.ASCII);
InBlock.gif                
//流输出为字符串
InBlock.gif
                String srdata = reader.ReadToEnd();
InBlock.gif                
//将返回html文本中的双引号变为单引号
InBlock.gif
                srdata = srdata.Replace( "\"","\'");
InBlock.gif
                
InBlock.gif                
//用模式匹配加分割的方式定位和获取时间信息
InBlock.gif
                string strOut = Regex.Split( srdata, "color='white'><b>" , RegexOptions.IgnoreCase)[1];
InBlock.gif                
string strTime = Regex.Split( strOut , "<br>", RegexOptions.IgnoreCase )[0];
InBlock.gif                time 
= DateTime.Parse( strTime ).ToLongTimeString();
InBlock.gif                
InBlock.gif                
//用模式匹配加分割的方式定位和获取日期信息
InBlock.gif
                string strOut1 = Regex.Split( srdata, "'5' color='white'>" , RegexOptions.IgnoreCase)[1];
InBlock.gif                
string strData = Regex.Split( strOut1 , "<br>", RegexOptions.IgnoreCase )[0];
InBlock.gif                Data 
= DateTime.Parse( strData ).ToShortDateString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch( System.Exception ex )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                time 
= string.Empty;
InBlock.gif                Data 
= string.Empty;
InBlock.gif                MessageBox.Show( 
"获取时间出错:" + ex.Message );
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif        
None.gif        
// 获取中国标准时间(由美国官方时间折算)
None.gif
         private   void  GetNistTimeCN(  out   string  time,  out   string  Data )
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//nist.time.gov的url
InBlock.gif
                string strNistUrl = "http://nist.time.gov/timezone.cgi?UTC/s/0";
InBlock.gif
InBlock.gif                
//构造并实例化一个WebRequest
InBlock.gif
                System.Net.WebRequest myHttpWebRequest = System.Net.HttpWebRequest.Create( strNistUrl );
InBlock.gif                
//设置连接超时时间
InBlock.gif
                myHttpWebRequest.Timeout = 8000;
InBlock.gif                
//设置WebResponse,接收返回信息
InBlock.gif
                System.Net.WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse();
InBlock.gif                
//获取返回信息流信息
InBlock.gif
                Stream sr = myHttpWebResponse.GetResponseStream();
InBlock.gif                
//设置流阅读器
InBlock.gif
                StreamReader reader = new StreamReader(sr, System.Text.Encoding.ASCII);
InBlock.gif                
//流输出为字符串
InBlock.gif
                String srdata = reader.ReadToEnd();
InBlock.gif                
//将返回html文本中的双引号变为单引号
InBlock.gif
                srdata = srdata.Replace( "\"","\'");
InBlock.gif
                
InBlock.gif                
//用模式匹配加分割的方式定位和获取时间信息
InBlock.gif
                string strOut = Regex.Split( srdata, "color='white'><b>" , RegexOptions.IgnoreCase)[1];
InBlock.gif                
string strTime = Regex.Split( strOut , "<br>", RegexOptions.IgnoreCase )[0];
InBlock.gif                
//小时需调整,因时区相差8小时
InBlock.gif
                time = DateTime.Parse( strTime ).AddHours( 8 ).ToLongTimeString();
InBlock.gif
InBlock.gif                
//用模式匹配加分割的方式定位和获取日期信息
InBlock.gif
                string strOut1 = Regex.Split( srdata, "'5' color='white'>" , RegexOptions.IgnoreCase)[1];
InBlock.gif                
string strData = Regex.Split( strOut1 , "<br>", RegexOptions.IgnoreCase )[0];
InBlock.gif                
//获取中国时间-小时部分
InBlock.gif
                int intHourCn = int.Parse( time.Split( ':' )[0] );
InBlock.gif                
//获取美国时间-小时部分
InBlock.gif
                int intHourUS = int.Parse( DateTime.Parse( strTime ).ToLongTimeString().Split( ':' )[0] );
InBlock.gif                
//比较小时差异,调整日期差异
InBlock.gif
                if ( intHourCn < intHourUS )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Data 
= DateTime.Parse( strData ).AddDays(1).ToShortDateString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Data 
= DateTime.Parse( strData ).ToShortDateString();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch( System.Exception ex )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                time 
= string.Empty;
InBlock.gif                Data 
= string.Empty;
InBlock.gif                MessageBox.Show( 
"获取时间出错:" + ex.Message );
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

调用代码:
None.gif          // 获取美国时间按钮
None.gif
         private   void  btnGetTime_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strTime = string.Empty;
InBlock.gif                
string strDate = string.Empty;
InBlock.gif
InBlock.gif                GetNistTimeUS( 
out strTime, out strDate );
InBlock.gif                txtTime.Text 
= strTime;
InBlock.gif                txtData.Text 
= strDate;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch( System.Exception ex )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show( ex.Message );
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        
// 获取中国时间按钮
None.gif
         private   void  btnGetTimeCN_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strTime = string.Empty;
InBlock.gif                
string strDate = string.Empty;
InBlock.gif
InBlock.gif                GetNistTimeCN( 
out strTime, out strDate );
InBlock.gif                txtTime.Text 
= strTime;
InBlock.gif                txtData.Text 
= strDate;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch( System.Exception ex )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show( ex.Message );
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值