时间准确的重要性不言而喻,有时候我们对时间的要求非常严格,有时候也要求不允许用户调整系统时间,有时候。。。等等情况下,我们都需要时间的同步,虽然Windows也有DOS命令来保持和时钟服务进行同步,但本文另辟途径,介绍另一种获取标准时间并同步时间的操作。
首先,我们来看看一个网站:国家授时中心(http://www.time.ac.cn/stime.asp),这个是应该是时间的权威机构,里面有各国各地的时间,我们可以通过同步该时间来实现系统时间的更新。首先分两步,一步是获取“国家授时中心”的时间,一步是更新系统时间。下面的代码就是做这些工作。
#region 获取网络时间
/// <summary>
/// 获取中国国家授时中心网络服务器时间发布的当前时间
/// </summary>
/// <returns></returns>
public static DateTime GetChineseDateTime()
{
DateTime res = DateTime.MinValue;
try
{
string url = "http://www.time.ac.cn/stime.asp";
HttpHelper helper = new HttpHelper();
helper.Encoding = Encoding.Default;
string html = helper.GetHtml(url);
string patDt = @"/d{4}年/d{1,2}月/d{1,2}日";
string patHr = @"hrs/s+=/s+/d{1,2}
C#与网络时间同步的时间处理函数代码
最新推荐文章于 2023-07-02 13:45:00 发布
![](https://img-home.csdnimg.cn/images/20240611030827.png)