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

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

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

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

img_33d02437d135341f0800e3d415312ae8.gif            
catch( System.Exception ex )
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                time 
= string.Empty;
img_33d02437d135341f0800e3d415312ae8.gif                Data 
= string.Empty;
img_33d02437d135341f0800e3d415312ae8.gif                MessageBox.Show( 
"获取时间出错:" + ex.Message );
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_05dd8d549cff04457a6366b0a7c9352a.gif        }

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

img_33d02437d135341f0800e3d415312ae8.gif                
else
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                    Data 
= DateTime.Parse( strData ).ToShortDateString();
img_105a1e124122b2abcee4ea8e9f5108f3.gif                }

img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch( System.Exception ex )
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                time 
= string.Empty;
img_33d02437d135341f0800e3d415312ae8.gif                Data 
= string.Empty;
img_33d02437d135341f0800e3d415312ae8.gif                MessageBox.Show( 
"获取时间出错:" + ex.Message );
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_05dd8d549cff04457a6366b0a7c9352a.gif        }

调用代码:
img_a6339ee3e57d1d52bc7d02b338e15a60.gif          // 获取美国时间按钮
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
         private   void  btnGetTime_Click( object  sender, System.EventArgs e)
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
string strTime = string.Empty;
img_33d02437d135341f0800e3d415312ae8.gif                
string strDate = string.Empty;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif                GetNistTimeUS( 
out strTime, out strDate );
img_33d02437d135341f0800e3d415312ae8.gif                txtTime.Text 
= strTime;
img_33d02437d135341f0800e3d415312ae8.gif                txtData.Text 
= strDate;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch( System.Exception ex )
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                MessageBox.Show( ex.Message );
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_05dd8d549cff04457a6366b0a7c9352a.gif        }

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif        
// 获取中国时间按钮
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
         private   void  btnGetTimeCN_Click( object  sender, System.EventArgs e)
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
string strTime = string.Empty;
img_33d02437d135341f0800e3d415312ae8.gif                
string strDate = string.Empty;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif                GetNistTimeCN( 
out strTime, out strDate );
img_33d02437d135341f0800e3d415312ae8.gif                txtTime.Text 
= strTime;
img_33d02437d135341f0800e3d415312ae8.gif                txtData.Text 
= strDate;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch( System.Exception ex )
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                MessageBox.Show( ex.Message );
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_05dd8d549cff04457a6366b0a7c9352a.gif        }
目录
相关文章
|
安全 Windows
微软:本周三个安全更新 暂不会修正Excel漏洞
上周微软接到公众报告称,发现Excel软件中存在一个安全漏洞,可导致黑客远程执行恶意代码。并有少量有目的攻击者已经利用了这个漏洞。但本周微软却表示,暂不会修正Excel出现的漏洞。 虽然微软本周提供了三个安全更新,其中一个是“高危”级补丁,但所有的三个更新都是关于Windows存在的漏洞的,微软并没有修复Excel存在的问题。
587 0
下一篇
无影云桌面