zoukankan      html  css  js  c++  java
  • 网络时间同步

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

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

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        #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}";
                    string patMn = @"min\s+=\s+\d{1,2}";
                    string patSc = @"sec\s+=\s+\d{1,2}";
                    Regex regDt = new Regex(patDt);
                    Regex regHr = new Regex(patHr);
                    Regex regMn = new Regex(patMn);
                    Regex regSc = new Regex(patSc);
    
                    res = DateTime.Parse(regDt.Match(html).Value);
                    int hr = GetInt(regHr.Match(html).Value, false);
                    int mn = GetInt(regMn.Match(html).Value, false);
                    int sc = GetInt(regSc.Match(html).Value, false);
                    res = res.AddHours(hr).AddMinutes(mn).AddSeconds(sc);
                }
                catch { }
                return res;
            }
    
            /// <summary>
            /// 从指定的字符串中获取整数
            /// </summary>
            /// <param name="origin">原始的字符串</param>
            /// <param name="fullMatch">是否完全匹配,若为false,则返回字符串中的第一个整数数字</param>
            /// <returns>整数数字</returns>
            private static int GetInt(string origin, bool fullMatch)
            {
                if (string.IsNullOrEmpty(origin))
                {
                    return 0;
                }
                origin = origin.Trim();
                if (!fullMatch)
                {
                    string pat = @"-?\d+";
                    Regex reg = new Regex(pat);
                    origin = reg.Match(origin.Trim()).Value;
                }
                int res = 0;
                int.TryParse(origin, out res);
                return res;
            }
            #endregion
    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        #region P/Invoke 设置本地时间
    
            [DllImport("kernel32.dll")]
            private static extern bool SetLocalTime(ref SYSTEMTIME time);
    
            [StructLayout(LayoutKind.Sequential)]
            private struct SYSTEMTIME
            {
                public short year;
                public short month;
                public short dayOfWeek;
                public short day;
                public short hour;
                public short minute;
                public short second;
                public short milliseconds;
            }
    
            /// <summary>
            /// 设置本地计算机时间
            /// </summary>
            /// <param name="dt">DateTime对象</param>
            public static void SetLocalTime(DateTime dt)
            {
                SYSTEMTIME st;
    
                st.year = (short)dt.Year;
                st.month = (short)dt.Month;
                st.dayOfWeek = (short)dt.DayOfWeek;
                st.day = (short)dt.Day;
                st.hour = (short)dt.Hour;
                st.minute = (short)dt.Minute;
                st.second = (short)dt.Second;
                st.milliseconds = (short)dt.Millisecond;
    
                SetLocalTime(ref st);
            }
    
            #endregion
  • 相关阅读:
    oracle数据库的乱码问题解决方案
    @HTML.checkboxFor()用法
    存储过程实现登录(.net)
    .net中大数据的处理
    xml总结
    获取指定日期是当前月的第几周
    JS中正则方法的使用
    获取指定日期的前一天日期
    通过Oracle函数实现.NET String.Format函数的简单版
    ReportViewer实现多语言
  • 原文地址:https://www.cnblogs.com/jordan2009/p/3006864.html
Copyright © 2011-2022 走看看