#电脑时间用了一段时间后, the system time is not allowed, want to update
#在windows里面, update system time, often fail, and the speed is very busy.
#在网上拷贝的代码, the discovery is very easy to use, faster than the implementation of Windows self-belt.
#-*-coding:utf-8-*-
Import socket
import struct
import time
import win32api
timeserver = ' 210.72.145.44 ' #国家授时中心ip
Port = 123
def getTime ():
time_1970 = 2208988800L
client = Socket.socket ( Socket.af_inet, Socket. SOCK_DGRAM)
data = ' \x1b ' + * '
client.sendto ' (data, (Timeserver, Port)
data, address = CLIENT.RECVFR OM (1024)
Data_result = Struct.unpack ('!12i ', data) [a]
data_result-= time_1970 return
data_result
def setsystemtime ():
tm_year, Tm_mon, Tm_mday, Tm_hour, Tm_min, Tm_sec, Tm_wday, tm_yday, TM_ISDST = Time.gmti Me (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]
Fast, just double click on the py file.