相關程式碼如下 ,複製 執行即可。
#-*- coding:utf-8 -*-import socketimport structimport timeimport win32apiTimeServer = "210.72.145.44" #國家授時中心ipPort = 123def getTime(): TIME_1970 = 2208988800L client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) data = "\x1b" + 47 * "\0" client.sendto(data, (TimeServer, Port)) data, address = client.recvfrom(1024) data_result = struct.unpack("!12I", data)[10] data_result -= TIME_1970 return data_resultdef setSystemTime(): tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst = time.gmtime(getTime()) win32api.SetSystemTime(tm_year, tm_mon, tm_wday, tm_mday, tm_hour, tm_min, tm_sec, 0) print "Set System OK!"if __name__ == "__main__": setSystemTime() print "%d-%d-%d %d:%d:%d" % time.localtime(getTime())[:6]
相關程式碼如下 ,複製 執行即可。
#-*- coding:utf-8 -*-import socketimport structimport timeimport win32apiTimeServer = "210.72.145.44" #國家授時中心ipPort = 123def getTime(): TIME_1970 = 2208988800L client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) data = "\x1b" + 47 * "\0" client.sendto(data, (TimeServer, Port)) data, address = client.recvfrom(1024) data_result = struct.unpack("!12I", data)[10] data_result -= TIME_1970 return data_resultdef setSystemTime(): tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst = time.gmtime(getTime()) win32api.SetSystemTime(tm_year, tm_mon, tm_wday, tm_mday, tm_hour, tm_min, tm_sec, 0) print "Set System OK!"if __name__ == "__main__": setSystemTime() print "%d-%d-%d %d:%d:%d" % time.localtime(getTime())[:6]