Windows is used for several years, and there is always a time to change the system time. However, it is easy to adjust the system time. It is not that easy to call up a correct one. A few years ago (when I was still in college, it was about a junior) I used C # To write an NTP Program But when the time passes, the ntp ip address changes too frequently (is it because there is no commercial profit ?); I have never tried "Internet time" in Windows (does anyone know why ?); What time has the Genie of the National Time Service Center (http://www.time.ac.cn) been unable to use on my computer since Windows became SP2-what time is that ?.
It seems that I had to find an HTTP server for network school? Things like us don't need that period of time. Let's talk about the time precision of windows .........
Import Socket, urllib2, WIN32API, time
Def Updatesystime ():
Try :
Srv = Urllib2.urlopen ( ' Http://www.baidu.com ' )
Date = SRV. headers. dict [ ' Date ' ]
GMT = Time. strptime (date [ 5 : 25 ], " % D % B % Y % H: % m: % s " )
# # The parameter of the setsystemtime function is the GMT time.
Return WIN32API. setsystemtime (GMT. tm_year, GMT. tm_mon, GMT. tm_wday, GMT. tm_mday, GMT. tm_hour, GMT. tm_min, GMT. tm_sec, 0)
Except :
Return 0
DefPrintcurtime ():
Now=Time. localtime (Time. Time ())
PrintTime. strftime ("% Z % Y-% m-% d % H: % m: % s", Now)
## Timeout time, in seconds
Timeout=0.3
Socket. setdefatimetimeout (timeout)
PrintU'Current System Time:'
Printcurtime ()
Print '\ R \ n'
I, maxtry = 1 , 6
While I < Maxtry And Updatesystime () = 0:
Print U ' Network timeout, % I retry ' % I
I = I + 1
If I < Maxtry:
Print U ' Network school success: '
Printcurtime ()
Else :
Print U ' \ R \ n network school failed. Check the network! '
Because the time-out time is set to only 0.3 seconds, I think this is acceptable in any case. In fact, in my test, even if timeout = 0.01 is set, there is no timeout error-of course, I tried it in Beijing.