1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- # -*- coding:utf-8 -*-
- import time
- import requests
- import json
- from datetime import datetime
- class Timer(object):
- def __init__(self, sleep_interval=0.5):
- # '2018-09-28 22:45:50.000'
- # buy_time = 2020-12-22 09:59:59.500
- buy_time_everyday = global_config.getRaw('config', 'buy_time').__str__()
- localtime = time.localtime(time.time())
- self.buy_time = datetime.strptime(
- localtime.tm_year.__str__() + '-' + localtime.tm_mon.__str__() + '-' + localtime.tm_mday.__str__()
- + ' ' + buy_time_everyday,
- "%Y-%m-%d %H:%M:%S.%f")
- self.buy_time_ms = int(time.mktime(self.buy_time.timetuple()) * 1000.0 + self.buy_time.microsecond / 1000)
- self.sleep_interval = sleep_interval
- self.diff_time = self.local_jd_time_diff()
- def jd_time(self):
- """
- 从京东服务器获取时间毫秒
- :return:
- """
- url = 'https://a.jd.com//ajax/queryServerData.html'
- ret = requests.get(url).text
- js = json.loads(ret)
- return int(js["serverTime"])
- def local_time(self):
- """
- 获取本地毫秒时间
- :return:
- """
- return int(round(time.time() * 1000))
- def local_jd_time_diff(self):
- """
- 计算本地与京东服务器时间差
- :return:
- """
- return self.local_time() - self.jd_time()
- def start(self):
- while True:
- # 本地时间减去与京东的时间差,能够将时间误差提升到0.1秒附近
- # 具体精度依赖获取京东服务器时间的网络时间损耗
- if self.local_time() - self.diff_time >= self.buy_time_ms:
- break
- else:
- time.sleep(self.sleep_interval)
- if __name__ == "__main__":
- timer = Timer()
- print(timer.buy_time)
|