def next_delta(start_time, interval, phase1_delta, phase2_delta, ignore_weekend): interval_delta = TimeDelta(seconds=interval) phase1_start_delta, phase1_end_delta = phase1_delta phase2_start_delta, phase2_end_delta = phase2_delta today = Datetime.today() phase1_start, phase1_end = today + phase1_start_delta, today + phase1_end_delta phase2_start, phase2_end = today + phase2_start_delta, today + phase2_end_delta current_time = Datetime.now() maybe_time = current_time + interval_delta if phase1_start_delta == phase1_end_delta and phase2_start_delta == phase2_end_delta: # 两个时间周期各自的起止时间相等,则认为未做限制 delta = interval_delta - (current_time - start_time) elif maybe_time > phase2_end: # 如果预计的下一次时间大于周期2的结束日期, 则取下一日的周期1起始时间计算 next_time = today + TimeDelta(1) + phase1_start_delta if ignore_weekend and next_time.day_of_week() in (0, 6): next_time = today.next_week() + phase1_start_delta delta = next_time - start_time elif maybe_time in (phase1_start, phase1_end, phase2_start, phase2_end): # 如果下一次时间刚好等于时间周期的起止点,则直接返回预计的时间间隔 delta = interval_delta elif start_time < phase1_start and phase1_start < maybe_time < phase1_end: # 如果本次的时间小于周期1的起始时间且预计下一次的时间在phase1内,则取phase1起始时间计算 delta = phase1_start - start_time elif phase1_end < maybe_time < phase2_start: delta = phase2_start - start_time else: delta = interval_delta - (current_time - start_time) return delta
def next_time_delta(self): now = Datetime.now() next_time = Datetime(now.year, now.month, now.day, self.hour, self.minute) if next_time < now: current_date = Datetime(now.year, now.month, now.day) if current_date.day_of_week() == 5: next_time = next_time + TimeDelta(3) else: next_time = next_time + TimeDelta(1) return (next_time, next_time - now)
def run(self): self.logger.info("{} 数据采集同步线程启动".format(self.market)) self.db = SqliteMemDriver() hikyuu_init(self.hku_config_file, ignore_preload=True) stk_list = self.get_stock_list() hku_warn_if(not stk_list, "从数据库中获取的股票列表为空!", logger=self.logger) self.mutex.tryLock() end_delta = self._phase1_end_time - self._phase1_end_time.start_of_day( ) start_delta = self._phase1_start_time - self._phase1_start_time.start_of_day( ) start = Datetime.now() if self._phase1_start_time >= self._phase1_end_time: delta = TimeDelta(0) elif start >= self._phase1_end_time: delta = (self._phase1_start_time + TimeDelta(1) - start) elif start < self._phase1_start_time: delta = (self._phase1_start_time - start) else: delta = self._interval * ceil( (start - self._phase1_start_time) / self._interval) - ( start - self._phase1_start_time) hku_info('{} 下次采集时间:{}', self.market, start + delta, logger=self.logger) delta = int(delta.total_milliseconds()) while self.working and not self.cond.wait(self.mutex, int(delta)): last_time = Datetime.today() + end_delta start = Datetime.now() if start >= last_time: next_time = Datetime.today() + TimeDelta(1) + start_delta hku_info('{} 明日采集时间:{}', self.market, next_time, logger=self.logger) delta = next_time - start delta = int(delta.total_milliseconds()) continue hku_trace("start:{}".format(start)) self.collect(stk_list) end = Datetime.now() x = end - start if x + TimeDelta(seconds=1) < self._interval: delta = self._interval - x - TimeDelta(seconds=1) delta = int(delta.total_milliseconds()) self.logger.info("{} {:<.2f} 秒后重新采集".format( self.market, delta / 1000)) #self.sleep(delta) self.db.close() self.logger.info("{} 数据采集同步线程终止!".format(self.market))
def __init__(self, config, hku_config_file, market='SH'): super(self.__class__, self).__init__() self.working = True self._config = config self.hku_config_file = hku_config_file self.market = market.lower() self.marketid = None self._interval = TimeDelta(seconds=config.getint('collect', 'interval', fallback=60 * 60)) self._phase1_start_time = Datetime( datetime.datetime.combine( datetime.date.today(), datetime.time.fromisoformat( (config.get('collect', 'phase1_start', fallback='09:05')) ) ) ) self._phase1_end_time = Datetime( datetime.datetime.combine( datetime.date.today(), datetime.time.fromisoformat( (config.get('collect', 'phase1_end', fallback='09:05')) ) ) ) self._use_zhima_proxy = config.getboolean('collect', 'use_zhima_proxy', fallback=False) self.cond = QWaitCondition() self.mutex = QMutex()
def __init__(self, config, market='SH'): super(self.__class__, self).__init__() self.working = True self._config = config self.market = market.lower() self.marketid = None assert config.getboolean("mysql", "enable", fallback=False) self._db_config = { 'user': config['mysql']['usr'], 'password': config['mysql']['pwd'], 'host': config['mysql']['host'], 'port': config['mysql']['port'] } self._connect = None self.quotations = [ 'stock', ] # sina 不支持基金,qq支持,此处屏蔽基金 #if config['quotation']['stock']: # self.quotations.append('stock') #if config['quotation']['fund']: # self.quotations.append('fund') #self.logger.info(self.quotations) self._interval = TimeDelta( seconds=config.getint('collect', 'interval', fallback=60 * 60)) self._phase1_start_time = Datetime( datetime.datetime.combine( datetime.date.today(), datetime.time.fromisoformat((config.get('collect', 'phase1_start', fallback='09:05'))))) self._phase1_end_time = Datetime( datetime.datetime.combine( datetime.date.today(), datetime.time.fromisoformat((config.get('collect', 'phase1_end', fallback='09:05'))))) self._use_zhima_proxy = config.getboolean('collect', 'use_zhima_proxy', fallback=False) self.cond = QWaitCondition() self.mutex = QMutex()
def parse_phase(phase): return [ TimeDelta(hours=int(a[0]), minutes=int(a[1])) for a in [x.split(':') for x in phase.split('-')] ]