现在的位置: 首页 > 综合 > 正文

与网络日期时间同步

2013年04月07日 ⁄ 综合 ⁄ 共 4511字 ⁄ 字号 评论关闭

一、如何与网络日期时间同步?

答曰,自动与 Internet 时间服务器同步,设置就好了:
20091027222905212

但问题不是这么简单:
办公室一台电脑,可能是主板电池没电了还是某个芯片坏了,一开机就是“2000-1-1 00:00:00”开始,与服务器时间相差太远(15个小时以外),是无法与服务器同步的!

怎么办?
若某个网页上有它所在服务器的日期和时间(特别是日期!),做一个程序访问得到它,再设置自己的时间,放在启动栏不就行了吗?

二、哪里有日期时间?

这个页面有一个日期时间:http://time.buaa.edu.cn/index.jsp
20091020184308063

看看源代码:
20091020184517551

第136行的“1256654223268”应该是它的日期时间吧?ticks?没有这么小的值的呀,从什么时候开始算起的呀?

三、算出它的起始日期时间?

已知:1256649229550,即为日期时间“2009-10-27  21:13:50”,绝不是Ticks,应该是Millisecond。试试看先:

long millisecond=1256649229550L;  //2009-10-27  21:13:50
Console.WriteLine(millisecond);

DateTime dt=new DateTime(2009,10,27,21,13,50,051);
long millisecond2 = dt.Ticks/TimeSpan.TicksPerMillisecond;
Console.WriteLine(millisecond2);//得出:63392274830051

long millisecond3=millisecond2-millisecond;
Console.WriteLine(millisecond3);

long startTicks=millisecond3*TimeSpan.TicksPerMillisecond;
Console.WriteLine(new DateTime(startTicks).ToString());//1970-1-1 8:00:00:000

哈哈,这是从1970-1-1 8:00:00:000开始算起的,反过来再试试看:

long millisecond=1256649300338L;
DateTime dt=new DateTime(new DateTime(1970,1,1,8,0,0,0).Ticks+(millisecond*TimeSpan.TicksPerMillisecond));
Console.WriteLine(dt);    //得出2009-10-27 21:15:00

就是!

四、写程序

using System;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;//DllImport

namespace SyncWebDatetime
{
    class Program
    {
        public const string DatetimeUrl = "http://time.buaa.edu.cn/index.jsp";
        public static DateTime DatetimeStart = new DateTime(1970, 1, 1, 8, 0, 0, 0);

        public const string DatetimeTag = @"^<div[/s/S]*id=""svT1"">[0-9]{12,}</div>$";  //@"id=""svT1"">1256654223268</div>";
        public const string DatetimeStrBegin = @"id=""svT1"">";
        public const string DatetimeStrEnd = @"</div>";

        static void Main(string[] args)
        {
            try
            {
                WebClient client = new WebClient();
                client.Headers.Add("user-agent",
                                   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

                Stream data = client.OpenRead(DatetimeUrl);
                StreamReader reader = new StreamReader(data);

                //read 1:
                //string s = reader.ReadToEnd();

                //read 2:
                string line;
                while ((line = reader.ReadLine()) != null)
                    if (Regex.Match(line, DatetimeTag).Success)
                        break;

                data.Close();
                reader.Close();
                if (!string.IsNullOrEmpty(line))
                {
                    //Console.WriteLine(line);
                    int iBegin = line.IndexOf(DatetimeStrBegin);
                    int iBeginLen = DatetimeStrBegin.Length;
                    int iEnd = line.IndexOf(DatetimeStrEnd);

                    string dtS = line.Substring(iBegin + iBeginLen, iEnd - iBegin - iBeginLen);

                    if (!string.IsNullOrEmpty(dtS))
                    {
                        long millisecond = 0L;
                        if (long.TryParse(dtS, out millisecond))
                        {
                            long serverDatetimeTicks = DatetimeStart.Ticks + millisecond*TimeSpan.TicksPerMillisecond;
                            DateTime serverDatetime = new DateTime(serverDatetimeTicks);
                            //Console.WriteLine(string.Format("服务器时间:{0}", serverDatetime));
                            SystemTime st=new SystemTime();
                            st.wYear = (ushort)serverDatetime.Year;
                            st.wMonth = (ushort) serverDatetime.Month;
                            st.wDay = (ushort) serverDatetime.Day;
                            st.wHour = (ushort) serverDatetime.Hour;
                            st.wMinute = (ushort) serverDatetime.Minute;
                            st.wSecond = (ushort) serverDatetime.Second;
                            st.wMilliseconds = (ushort) serverDatetime.Millisecond;
                            SetLocalTime(st);
                        }
                    }
                }
            }
            catch(Exception exception)
            {
                Console.WriteLine(exception);
                Console.ReadKey();
            }
            //Console.ReadKey();
        }

        //调用Kernel32.DLL
        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(SystemTime st);
        [DllImport("Kernel32.dll")]
        public static extern void SetLocalTime(SystemTime st);

        [StructLayout(LayoutKind.Sequential)]
        public class SystemTime
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }
    }
}

抱歉!评论已关闭.