python windows时间同步工具
由于某种原因(BIOS电池没电),电脑的系统时间会与我们的北京时间不同步,将会导致如下问题:
1. 抢火车票的时候已经过时间了
2.别的同事都走了,你还以为没下班
……
规避问题的方法:同步系统时间
一. 获取时间
在这里,我们有两种方法
1. 通过系统请求网站服务器头部返回Respones Headers
获取Date 参数值,修改系统时间
def getTime(self,url):
conn = http.client.HTTPConnection(url)
conn.request("GET", "/")
r = conn.getresponse()
# r.getheaders() #获取所有的http头
ts = r.getheader('date') # 获取http头date部分
# 将GMT时间转换成北京时间
ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S") # 格式ts
ttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60) # +东八区
dat = "date %u-%02u-%02u" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday)
tm = "time %02u:%02u:%02u" % (ttime.tm_hour, ttime.tm_min, ttime.tm_sec)
return [dat, tm]
在这里解释下strptime函数中,时间字符串的格式化说明
|
%y 两位数的年份表示(00-99) %a 本地简化星期名称 |
代码中的%参数解析
%[flags][width][.precision][size]type
|
%02u : |
2. 通过获取授时中心的标准时间修改系统时间
授时中心的网址:
http://www.pool.ntp.org/
ntplib 下载地址:
https://pypi.python.org/pypi/ntplib/
def getTime(self):
c = ntplib.NTPClient()
response = c.request('pool.ntp.org') # 授时中心的网址
ts_stamp = response.tx_time
ts = time.localtime(ts_stamp)
ttime = time.localtime(time.mktime(ts) + 8 * 60 * 60) # +东八区
return ts
二. 同步时间
获取时间后,我们要同步时间,同步时间也有两种方法
1.通过os.system 同步
def setTime(self,time):
os.system(time[0])
os.system(time[1])
2.通过win32api.SetSystimeTime 同步
def setTime(self,time_cls):
ttime = time.localtime(time.mktime(time_cls) - 8 * 60 * 60) # 本为东八区,却set后多出8小时 UTC多了8个小时 , 所以……
time_cls = ttime
win32api.SetSystemTime(time_cls.tm_year, time_cls.tm_mon, time_cls.tm_wday, time_cls.tm_mday, time_cls.tm_hour, time_cls.tm_min, time_cls.tm_sec, 0)
完整代码如下:
# -*- coding:utf-8 -*-
# 1.获取网络时间
# 2.修改系统时间
import http.client
import time
import os,sys
import ntplib
import win32api
'''
'同步时间类
'''
class syctimes():
def __init__(self):
if sys.argv.__len__() == 1:
try:
time_list = self.getTime1()
self.setTime1(time_list)
except:
time_list = self.getTime2('www.baidu.com')
self.setTime2(time_list)
for arg in sys.argv:
if arg == '-1':
time_list = self.getTime1()
self.setTime1(time_list)
if arg == '-2':
time_list = self.getTime2('www.baidu.com')
self.setTime2(time_list)
sys.exit()
def getTime1(self):
c = ntplib.NTPClient()
response = c.request('pool.ntp.org') # 授时中心的网址
ts_stamp = response.tx_time
ts = time.localtime(ts_stamp)
ttime = time.localtime(time.mktime(ts) + 8 * 60 * 60) # +东八区
return ts
def getTime2(self,url):
conn = http.client.HTTPConnection(url)
conn.request("GET", "/")
r = conn.getresponse()
# r.getheaders() #获取所有的http头
ts = r.getheader('date') # 获取http头date部分
# 将GMT时间转换成北京时间
ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S") # 格式ts
ttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60) # +东八区
dat = "date %u-%02u-%02u" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday)
tm = "time %02u:%02u:%02u" % (ttime.tm_hour, ttime.tm_min, ttime.tm_sec)
return [dat, tm]
'''
修改时间1
'''
def setTime1(self,time_cls):
ttime = time.localtime(time.mktime(time_cls) - 8 * 60 * 60) # 本为东八区,却set后多出8小时 UTC多了8个小时 , 所以……
time_cls = ttime
win32api.SetSystemTime(time_cls.tm_year, time_cls.tm_mon, time_cls.tm_wday, time_cls.tm_mday, time_cls.tm_hour, time_cls.tm_min, time_cls.tm_sec, 0)
'''
修改时间2
'''
def setTime2(self,time):
os.system(time[0])
os.system(time[1])
if __name__ == "__main__":
classSyc = syctimes()
# 方法1:
#time_list = classSyc.getTime1()
#classSyc.setTime1(time_list)
# 方法2:
# time_list = classSyc.getTime2('www.baidu.com')
# classSyc.setTime2(time_list)
打包代码为exe 添加随系统启动执行
import sys
if __name__ == '__main__':
from PyInstaller import __main__
params = ['-F', '-w', '--icon=favicon.ico', 'times.py']
__main__.run(params)
# -*- coding:utf-8 -*-
import win32api
import win32con
name = 'SycTimeTool' # 要添加的项值名称
path = 'E:\dcb3688\时间同步工具.exe -1' # 要添加的exe路径
# 注册表项名
KeyName = 'Software\\Microsoft\\Windows\\CurrentVersion\\Run'
# 异常处理
try:
key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, KeyName, 0, win32con.KEY_ALL_ACCESS)
win32api.RegSetValueEx(key, name, 0, win32con.REG_SZ, path)
win32api.RegCloseKey(key)
except:
print('error')
print('添加成功!')





浙公网安备 33010602011771号