| 49 | |
| 50 | |
| 51 | class ClockMomentHandler: |
| 52 | def __init__(self, clock_engine, clock_type, moment=None, is_trading_date=True, makeup=False, call=None): |
| 53 | """ |
| 54 | :param clock_type: |
| 55 | :param moment: datetime.time |
| 56 | :param is_trading_date: bool(是否只有在交易日触发) |
| 57 | :param makeup: 注册时,如果已经过了触发时机,是否立即触发 |
| 58 | :return: |
| 59 | """ |
| 60 | self.clock_engine = clock_engine |
| 61 | self.clock_type = clock_type |
| 62 | self.moment = moment |
| 63 | self.is_trading_date = is_trading_date |
| 64 | self.makeup = makeup |
| 65 | self.call = call or (lambda: None) |
| 66 | #只在交易日执行定时任务(is_trading_date == True),当天为非交易日时,取最近一个交易日来设置next_time |
| 67 | if self.is_trading_date == True and not etime.is_trade_date(self.clock_engine.now_dt): |
| 68 | next_date = etime.get_next_trade_date(self.clock_engine.now_dt) |
| 69 | self.next_time = datetime.datetime.combine( |
| 70 | next_date, |
| 71 | self.moment |
| 72 | ) |
| 73 | else: |
| 74 | self.next_time = datetime.datetime.combine( |
| 75 | self.clock_engine.now_dt.date(), |
| 76 | self.moment, |
| 77 | ) |
| 78 | |
| 79 | if not self.makeup and self.is_active(): |
| 80 | self.update_next_time() |
| 81 | |
| 82 | def update_next_time(self): |
| 83 | """ |
| 84 | 下次激活时间 |
| 85 | :return: |
| 86 | """ |
| 87 | if self.is_active(): |
| 88 | if self.is_trading_date: |
| 89 | next_date = etime.get_next_trade_date(self.clock_engine.now_dt) |
| 90 | else: |
| 91 | next_date = self.next_time.date() + datetime.timedelta(days=1) |
| 92 | |
| 93 | self.next_time = datetime.datetime.combine( |
| 94 | next_date, |
| 95 | self.moment |
| 96 | ) |
| 97 | |
| 98 | def is_active(self): |
| 99 | if self.is_trading_date and not etime.is_trade_date(self.clock_engine.now_dt): |
| 100 | # 仅在交易日触发时的判断 |
| 101 | return False |
| 102 | return self.next_time <= self.clock_engine.now_dt |
| 103 | |
| 104 | |
| 105 | class ClockEngine: |
no outgoing calls