与网络日期时间同步

24 篇文章 0 订阅
6 篇文章 0 订阅

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

答曰,自动与 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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
比较完整的命令大全 net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user:"用户名" 建立IPC非空链接 net use h: \\ip\c$ "密码" /user:"用户名" 直接登陆后映射对方C:到本地为H: net use h: \\ip\c$ 登陆后映射对方C:到本地为H: net use \\ip\ipc$ /del 删除IPC链接 net use h: /del 删除映射对方到本地的为H:的映射 net user 用户名 密码 /add 建立用户 net user guest /active:yes 激活guest用户 net user 查看有哪些用户 net user 帐户名 查看帐户的属性 net localgroup ***istrators 用户名 /add 把“用户”添加到管理员中使其具有管理员权限,注意:***istrator后加s用复数 net start 查看开启了哪些服务 net start 服务名  开启服务;(如:net start telnet, net start schedule) net stop 服务名 停止某服务 net time \\目标ip 查看对方时间 net time \\目标ip /set 设置本地计算机时间与“目标IP”主机的时间同步,加上参数/yes可取消确认信息 net view 查看本地局域网内开启了哪些共享 net view \\ip 查看对方局域网内开启了哪些共享 net config 显示系统网络设置 net logoff 断开连接的共享 net pause 服务名 暂停某服务 net send ip "文本信息" 向对方发信息 net ver 局域网内正在使用的网络连接类型和信息 net share 查看本地开启的共享 net share ipc$ 开启ipc$共享 net share ipc$ /del 删除ipc$共享 net share c$ /del 删除C:共享
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值