1 Star 1 Fork 0

h3l/getNTPtime

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
getNTPtime.py 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
h3l 提交于 2019-07-03 19:15 +08:00 . 仓库初始化
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#
# 文件名称 : getNTPtime.py
# 文件描述 : 使用ntplib强制调整windows时间,只适合windows系统
# 编写人员 : HHL
# 修改人员 :
# 编修描述 : V0.01,时间2019.07.02
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#
__version__ = '0.01'
__status__ = 'beta'
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
import os
import sys
import time
import ntplib
import ctypes
import platform
# 判断获取权限
def running_as_admin(fun):
if ctypes.windll.shell32.IsUserAnAdmin():
pass
else:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
fun()
# 获取并设置系统时间
def get_NTP_time():
# import pdb; pdb.set_trace()
c = ntplib.NTPClient()
while True:
try:
response = c.request('pool.ntp.org')
ts = response.tx_time
_date = time.strftime('%Y-%m-%d',time.localtime(ts))
_time = time.strftime('%X',time.localtime(ts))
os.system('date {} && time {}'.format(_date,_time))
return
except:
time.sleep(6)
if __name__ == '__main__':
if 'Windows' == platform.system():
# 隐藏dos窗口
whnd = ctypes.windll.kernel32.GetConsoleWindow()
if whnd != 0:
ctypes.windll.user32.ShowWindow(whnd, 0)
ctypes.windll.kernel32.CloseHandle(whnd)
running_as_admin(get_NTP_time())
else:
print("worked under windows only")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/h3l/getNTPtime.git
git@gitee.com:h3l/getNTPtime.git
h3l
getNTPtime
getNTPtime
master

搜索帮助