From af5ece8c3e887ccd202927d8a7ca05caed721f73 Mon Sep 17 00:00:00 2001 From: wenxianping <931128603@qq.com> Date: Thu, 3 Jan 2019 09:25:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DautoSynchroTime=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=9C=AA=E6=8F=90=E4=BA=A4=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/AutoSynchroTime.py | 53 ++++++++++++++++++++++++++++++++++++++ init/select_ticket_info.py | 21 +++++++-------- 2 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 config/AutoSynchroTime.py diff --git a/config/AutoSynchroTime.py b/config/AutoSynchroTime.py new file mode 100644 index 0000000..5a813a0 --- /dev/null +++ b/config/AutoSynchroTime.py @@ -0,0 +1,53 @@ +# coding=utf-8 +import os +import platform + +import ntplib +import datetime + + +def autoSynchroTime(): + """ + 同步北京时间,执行时候,请务必用sudo,sudo,sudo 执行,否则会报权限错误,windows打开ide或者cmd请用管理员身份 + :return: + """ + 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(' ') + + print(u"正在同步时间,请耐心等待30秒左右") + print(u"系统当前时间{}".format(str(datetime.datetime.now())[:22])) + system = platform.system() + if system == "Windows": # windows 同步时间未测试过,参考地址:https://www.jianshu.com/p/92ec15da6cc3 + print(u"北京标准时间", _date, _time) + + a, b, c = _time.split(':') + + c = float(c) + 0.5 + + _time = "%s:%s:%s" % (a, b, c) + + 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') + print(u"同步后时间:{}".format(str(datetime.datetime.now())[:22])) + + +if __name__ == '__main__': + autoSynchroTime() \ No newline at end of file diff --git a/init/select_ticket_info.py b/init/select_ticket_info.py index 0d36ed0..4456d05 100755 --- a/init/select_ticket_info.py +++ b/init/select_ticket_info.py @@ -183,23 +183,20 @@ class select: try: num += 1 check_user.sendCheckUser() - if time.strftime('%H:%M:%S', time.localtime(time.time())) > "23:00:00" or time.strftime('%H:%M:%S', - time.localtime( - time.time())) < "06:00:00": - print(ticket.REST_TIME) - while 1: - time.sleep(1) - if "06:00:00" < time.strftime('%H:%M:%S', time.localtime(time.time())) < "23:00:00": - print(ticket.REST_TIME_PAST) - self.call_login() - break - start_time = datetime.datetime.now() + now = datetime.datetime.now() + if now.hour >= 23 or now.hour < 6: + print(u"12306休息时间,本程序自动停止,明天早上七点将自动运行") + open_time = datetime.datetime(now.year, now.month, now.day, 6) + if open_time < now: + open_time += datetime.timedelta(1) + time.sleep((open_time - now).seconds) + self.call_login() if self.order_model is 1: autoSynchroTime() sleep_time_s = 0.1 sleep_time_t = 0.5 # 测试了一下有微妙级的误差,应该不影响,测试结果:2019-01-02 22:30:00.004555 - if start_time.strftime("%M:%S") == "29:55" or start_time.strftime("%M:%S") == "59:55": + if now.strftime("%M:%S") == "29:55" or now.strftime("%M:%S") == "59:55": print(u"预售整点模式卡点中") time.sleep(5) print(u"预售模式执行")