Use Python to set the system time (to system school) __python

Source: Internet
Author: User

#电脑时间用了一段时间后, 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.

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.