创立一个白件,保留上面那个类。
using system;using system.collections;using system.collections.generic;using system.data;using system.diagnostics;using system.io;using system.net;using system.net.sockets;using system.runtime.interopservices;public class daytime{ //internet time server class by alastair dallas 01/27/04 //number of seconds private const int threshold_seconds = 15; // that windows clock can deviate from nist and still be okay //server ip addresses from //http://www.boulder.nist.gov/timefreq/service/time-servers.html private static string[] servers = {"129.6.15.28","129.6.15.29","132.163.4.101","132.163.4.102","132.163.4.103","128.138.140.44","192.43.244.18","131.107.1.10","66.243.43.21","216.200.93.8","208.184.49.9","207.126.98.204","205.188.185.33"}; public static string lasthost = ""; public static datetime lastsystime; public static datetime gettime() { //returns utc/gmt using an nist server if possible,http://zh3.91919.info, // degrading to simply returning the system clock //if we are successful in getting nist time, then // lasthost indicates which server was used and // lastsystime contains the system time of the call // if lastsystime is not within 15 seconds of nist time, // the system clock may need to be reset // if lasthost is "", time is equal to system clock string host = null; datetime result = default(datetime); lasthost = ""; foreach (string host_loopvariable in servers) { host = host_loopvariable; result = getnisttime(host); if (result > datetime.minvalue) { lasthost = host; break; // todo: might not be correct. was : exit for } } if (string.isnullorempty(lasthost)) { //no server in list was successful so use system time result = datetime.utcnow; } return result; } public static int secondsdifference(datetime dt1, datetime dt2) { timespan span = dt1.subtract(dt2); return span.seconds + (span.minutes * 60) + (span.hours * 360); } public static bool windowsclockincorrect() { datetime nist = gettime(); if ((math.abs(secondsdifference(nist, lastsystime)) > threshold_seconds)) { return true; } return false; } private static datetime getnisttime(string host) { //returns datetime.minvalue if host unreachable or does not produce time datetime result = default(datetime); string timestr = null; try { streamreader reader = new streamreader(new tcpclient(host, 13).getstream()); lastsystime = datetime.utcnow; timestr = reader.readtoend(); reader.close(); } catch (socketexception ex) { //couldn't connect to server, transmission error debug.writeline("socket exception [" + host + "]"); return datetime.minvalue; } catch (exception ex) { //some other error, such as stream under/overflow return datetime.minvalue; } //parse timestr if ((timestr.substring(38, 9) != "utc(nist)")) { //this signature should be there return datetime.minvalue; } if ((timestr.substring(30, 1) != "0")) { //server reports non-optimum status, time off by as much as 5 seconds return datetime.minvalue; //try a different server } int jd = int.parse(timestr.substring(1, 5)); int yr = int.parse(timestr.substring(7, 2)); int mo = int.parse(timestr.substring(10, 2)); int dy = int.parse(timestr.substring(13, 2)); int hr = int.parse(timestr.substring(16, 2)); int mm = int.parse(timestr.substring(19, 2)); int sc = int.parse(timestr.substring(22, 2)); if ((jd < 15020)) { //date is before 1900 return datetime.minvalue; } if ((jd > 51544)) yr += 2000; else yr += 1900; return new datetime(yr, mo, dy, hr, mm,http://ga3.33gao.info, sc); } [structlayout(layoutkind.sequential)] public struct systemtime { public int16 wyear; public int16 wmonth; public int16 wdayofweek; public int16 wday; public int16 whour; public int16 wminute; public int16 wsecond; public int16 wmilliseconds; } [dllimport("kernel32.dll", charset = charset.ansi, setlasterror = true, exactspelling = true)] private static extern int32 getsystemtime(ref systemtime stru); [dllimport("kernel32.dll", charset = charset.ansi, setlasterror = true, exactspelling = true)] private static extern int32 setsystemtime(ref systemtime stru); public static void setwindowsclock(datetime dt) { //sets system time. note: use utc time; windows will apply time zone systemtime timestru = default(systemtime); int32 result = default(int32); timestru.wyear = (int16)dt.year; timestru.wmonth = (int16)dt.month; timestru.wday = (int16)dt.day; timestru.wdayofweek = (int16)dt.dayofweek; timestru.whour = (int16)dt.hour; timestru.wminute = (int16)dt.minute; timestru.wsecond = (int16)dt.second; timestru.wmilliseconds = (int16)dt.millisecond; result = setsystemtime(ref timestru); }}
调用方式
daytime.gettime().tolocaltime()//那个便非同步先正确的时光,datetime类型的。
相关的主题文章:
” 又到了一个道心
思“雅”
人念道的 非小刘确切没有对 |