From 9dd8be2c405934af93e5303fe4c5b905dbd67310 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Thu, 21 Sep 2023 02:19:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0TimerManager=E5=AF=B9?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=97=B6=E9=97=B4=E5=8F=91=E7=94=9F=E5=8F=98?= =?UTF-8?q?=E5=8C=96=E6=97=B6=E7=9A=84=E4=BF=9D=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hikyuu_cpp/hikyuu/utilities/TimerManager.h | 32 ++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/hikyuu_cpp/hikyuu/utilities/TimerManager.h b/hikyuu_cpp/hikyuu/utilities/TimerManager.h index 311ebc0d..0c4dbc56 100644 --- a/hikyuu_cpp/hikyuu/utilities/TimerManager.h +++ b/hikyuu_cpp/hikyuu/utilities/TimerManager.h @@ -309,6 +309,10 @@ private: } m_queue.pop(); + + // 重新获取当前时间 + now = Datetime::now(); + auto timer_iter = m_timers.find(s.m_timer_id); if (timer_iter == m_timers.end()) { continue; @@ -326,20 +330,38 @@ private: continue; } + // 计算下一次执行的时间点 Datetime today = now.startOfDay(); s.m_time_point = timer->m_start_time >= TimeDelta() ? s.m_time_point + timer->m_duration : s.m_time_point + TimeDelta(1); + + if (timer->m_start_time >= TimeDelta()) { + // 非指定时刻执行的定时器 + s.m_time_point = s.m_time_point + timer->m_duration; + if (s.m_time_point < now) { + // 系统时间发生向前调整 + s.m_time_point = now; + } + + // 如果限定了当日可执行的时间段,且下一执行时刻超出了当日的限定时间 + if (timer->m_start_time != timer->m_end_time && + s.m_time_point > today + timer->m_end_time) { + s.m_time_point = today + timer->m_start_time + TimeDelta(1); + } + + } else { + // 指定了每日运行时刻的定时器 + s.m_time_point = + s.m_time_point + (today - s.m_time_point.startOfDay() + TimeDelta(1)); + } + if (timer->m_end_date != Datetime::max() && s.m_time_point > timer->m_end_date + timer->m_end_time) { _removeTimer(s.m_timer_id); continue; } - if (timer->m_start_time >= TimeDelta() && timer->m_start_time != timer->m_end_time && - s.m_time_point > today + timer->m_end_time) { - s.m_time_point = today + timer->m_start_time + TimeDelta(1); - } - // HKU_TRACE("s.m_time_point: {}", s.m_time_point.repr()); + // 将下一运行时间推入队列 m_queue.push(s); } }