From 0afa64ac07be8b4679c2e69b6898ee45ff5e802c Mon Sep 17 00:00:00 2001 From: wenxianping <931128603@qq.com> Date: Thu, 3 Jan 2019 10:41:57 +0800 Subject: [PATCH] =?UTF-8?q?ntp=E6=94=B9=E6=88=90=E9=98=BF=E9=87=8C?= =?UTF-8?q?=E4=BA=91=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/AutoSynchroTime.py | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/config/AutoSynchroTime.py b/config/AutoSynchroTime.py index 5a813a0..ba1ad8e 100644 --- a/config/AutoSynchroTime.py +++ b/config/AutoSynchroTime.py @@ -13,28 +13,26 @@ def autoSynchroTime(): """ c = ntplib.NTPClient() - hosts = ['edu.ntp.org.cn', 'tw.ntp.org.cn', 'us.ntp.org.cn', 'cn.pool.ntp.org', 'jp.ntp.org.cn'] - - for host in hosts: - - try: - - response = c.request(host) - - if response: - break - - except Exception as e: - print(u"时区获取异常:{0}".format(e)) - pass - current_time = response.tx_time - - _date, _time = str(datetime.datetime.fromtimestamp(current_time))[:22].split(' ') + hosts = ['ntp1.aliyun.com', 'ntp2.aliyun.com', 'ntp3.aliyun.com', 'ntp4.aliyun.com', 'cn.pool.ntp.org'] print(u"正在同步时间,请耐心等待30秒左右") print(u"系统当前时间{}".format(str(datetime.datetime.now())[:22])) system = platform.system() if system == "Windows": # windows 同步时间未测试过,参考地址:https://www.jianshu.com/p/92ec15da6cc3 + for host in hosts: + + try: + + response = c.request(host) + + if response: + break + + except Exception as e: + print(u"时区获取异常:{0}".format(e)) + current_time = response.tx_time + + _date, _time = str(datetime.datetime.fromtimestamp(current_time))[:22].split(' ') print(u"北京标准时间", _date, _time) a, b, c = _time.split(':') @@ -45,7 +43,10 @@ def autoSynchroTime(): os.system('date %s && time %s' % (_date, _time)) else: # mac同步地址,如果ntpdate未安装,brew install ntpdate linux 安装 yum install -y ntpdate - os.system('ntpdate time.apple.com') + for host in hosts: + sin = os.system('ntpdate {}'.format(host)) + if sin is 0: + break print(u"同步后时间:{}".format(str(datetime.datetime.now())[:22]))