Network school time (non-NTP)

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.