• 时间的处理与网络时间同步


    时间准确的重要性不言而喻,有时候我们对时间的要求非常严格,有时候也要求不允许用户调整系统时间,有时候。。。等等情况下,我们都需要时间的同步,虽然Windows也有DOS命令来保持和时钟服务进行同步,但本文另辟途径,介绍另一种获取标准时间并同步时间的操作。

    首先,我们来看看一个网站:国家授时中心(http://www.time.ac.cn/stime.asp),这个是应该是时间的权威机构,里面有各国各地的时间,我们可以通过同步该时间来实现系统时间的更新。首先分两步,一步是获取“国家授时中心”的时间,一步是更新系统时间。下面的代码就是做这些工作。

    1. #region 获取网络时间 
    2. /// <summary> 
    3. /// 获取中国国家授时中心网络服务器时间发布的当前时间 
    4. /// </summary> 
    5. /// <returns></returns> 
    6. public static DateTime GetChineseDateTime() 
    7.     DateTime res = DateTime.MinValue; 
    8.     try 
    9.     { 
    10.         string url = "http://www.time.ac.cn/stime.asp"
    11.         HttpHelper helper = new HttpHelper(); 
    12.         helper.Encoding = Encoding.Default; 
    13.         string html = helper.GetHtml(url); 
    14.         string patDt = @"\d{4}年\d{1,2}月\d{1,2}日"
    15.         string patHr = @"hrs\s+=\s+\d{1,2}"
    16.         string patMn = @"min\s+=\s+\d{1,2}"
    17.         string patSc = @"sec\s+=\s+\d{1,2}"
    18.         Regex regDt = new Regex(patDt); 
    19.         Regex regHr = new Regex(patHr); 
    20.         Regex regMn = new Regex(patMn); 
    21.         Regex regSc = new Regex(patSc); 
    22.  
    23.         res = DateTime.Parse(regDt.Match(html).Value); 
    24.         int hr = GetInt(regHr.Match(html).Value, false); 
    25.         int mn = GetInt(regMn.Match(html).Value, false); 
    26.         int sc = GetInt(regSc.Match(html).Value, false); 
    27.         res = res.AddHours(hr).AddMinutes(mn).AddSeconds(sc); 
    28.     } 
    29.     catch { } 
    30.     return res; 
    31.  
    32. /// <summary> 
    33. /// 从指定的字符串中获取整数 
    34. /// </summary> 
    35. /// <param name="origin">原始的字符串</param> 
    36. /// <param name="fullMatch">是否完全匹配,若为false,则返回字符串中的第一个整数数字</param> 
    37. /// <returns>整数数字</returns> 
    38. private static int GetInt(string origin, bool fullMatch) 
    39.     if (string.IsNullOrEmpty(origin)) 
    40.     { 
    41.         return 0; 
    42.     } 
    43.     origin = origin.Trim(); 
    44.     if (!fullMatch) 
    45.     { 
    46.         string pat = @"-?\d+"
    47.         Regex reg = new Regex(pat); 
    48.         origin = reg.Match(origin.Trim()).Value; 
    49.     } 
    50.     int res = 0; 
    51.     int.TryParse(origin, out res); 
    52.     return res; 
    53. #endregion 
    1. #region P/Invoke 设置本地时间 
    2.  
    3. [DllImport("kernel32.dll")] 
    4. private static extern bool SetLocalTime(ref SYSTEMTIME time); 
    5.  
    6. [StructLayout(LayoutKind.Sequential)] 
    7. private struct SYSTEMTIME 
    8.     public short year; 
    9.     public short month; 
    10.     public short dayOfWeek; 
    11.     public short day; 
    12.     public short hour; 
    13.     public short minute; 
    14.     public short second; 
    15.     public short milliseconds; 
    16.  
    17. /// <summary> 
    18. /// 设置本地计算机时间 
    19. /// </summary> 
    20. /// <param name="dt">DateTime对象</param> 
    21. public static void SetLocalTime(DateTime dt) 
    22.     SYSTEMTIME st; 
    23.  
    24.     st.year = (short)dt.Year; 
    25.     st.month = (short)dt.Month; 
    26.     st.dayOfWeek = (short)dt.DayOfWeek; 
    27.     st.day = (short)dt.Day; 
    28.     st.hour = (short)dt.Hour; 
    29.     st.minute = (short)dt.Minute; 
    30.     st.second = (short)dt.Second; 
    31.     st.milliseconds = (short)dt.Millisecond; 
    32.  
    33.     SetLocalTime(ref st); 
    34.  
    35. #endregion 

    两步操作就可以搞定时间的同步,测试效果还是不错,不过不建议频繁进行时间的同步处理,间隔一段时间检查一次即可

  • 相关阅读:
    P1587 [NOI2016]循环之美 杜教筛
    【学习笔记】省选动态规划类型选讲
    【模板】结构体重载高精度
    SP1716 GSS3
    SP1043 GSS1
    P1890 gcd区间 线段树
    【模板】(最小费用)最大流
    【模板】矩阵乘法
    P1073 最优贸易 DFS
    【2019.8.14】2019QB学堂DP图论班第一次考试 Problem C
  • 原文地址:https://www.cnblogs.com/wantingqiang/p/2104473.html
Copyright © 2020-2023  润新知