This article for you to share two Python scripts to set the system time method, for your reference, the specific content is as follows
the first method, which uses Python to set the system time, is to give the system
#电脑时间用了一段时间后, the system time is not allowed, want to update the # in Windows, update the system time, often failed, and very busy. #在网上拷贝的代码, found to be very useful, faster than the implementation of Windows comes with. #-*-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.recvfrom (1024x768) Data_result = Struct.unpack ('!12i ', data) [ten] 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.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 ()
Fast, just double-click on the py file can be
second method,Python third-party library recommended, sync time on Windows via Ntplib
Many times we have the need to calibrate the time of Beijing by synchronizing the program scripts.
Synchronization time on Linux is more convenient, install a ntpdate software on the line.
But the time to sync in Windows is more troublesome.
The idea is to get an accurate time from the network and then call the DOS command to modify the time.
Where do you get it from? Of course, it's the National Timing Center.
The site of the timing Center is cn.pool.ntp.org (note that the widely spread 210.72.145.44 this IP has been invalidated, directly with the domain name. )
However, the time taken from the timing Center requires NTP protocol parsing.
Ntplib is doing it.
It is also worth mentioning that the DOS modification date time to be implemented by 2 commands, the date command modification dates, Time command modification times.
Install Ntplib:
Easy_install ntplib or pip install Ntplib
The code below.
The above is the entire content of this article, two Python scripts to set the system time method, you learned it?