From 5af6120be87d16302e83cc998e71e4a42fa8a3f8 Mon Sep 17 00:00:00 2001 From: KenanZhu <3471685733@qq.com> Date: Mon, 16 Mar 2026 21:15:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(TimerUtils):=20=E6=96=B0=E5=A2=9E=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E4=BB=BB=E5=8A=A1=E6=97=B6=E9=97=B4=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 TimerUtils.calculateNextRepeatTime 方法 - 支持基于重复日期和目标时间计算下次执行时间 - 如果当天在重复日期且目标时间未过,则返回今天;否则查找下一个匹配日期 --- src/utils/TimerUtils.py | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/utils/TimerUtils.py diff --git a/src/utils/TimerUtils.py b/src/utils/TimerUtils.py new file mode 100644 index 0000000..ffecafb --- /dev/null +++ b/src/utils/TimerUtils.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +""" +Copyright (c) 2025 - 2026 KenanZhu. +All rights reserved. + +This software is provided "as is", without any warranty of any kind. +You may use, modify, and distribute this file under the terms of the MIT License. +See the LICENSE file for details. +""" +from datetime import datetime, timedelta + + +def calculateNextRepeatTime( + repeat_days: list, + hour: int, + minute: int, + second: int +) -> datetime: + """ + Calculate the next repeat time based on repeat days and target time. + + This function calculates the next execution time for a repeatable task. + If the current day is in repeat_days and the target time has not passed, + it returns today's target time. Otherwise, it finds the next matching day. + + Args: + repeat_days (list): List of weekdays to repeat (0=Monday, 6=Sunday). + hour (int): Target hour (0-23). + minute (int): Target minute (0-59). + second (int): Target second (0-59). + + Returns: + datetime: The next repeat execution time. + """ + + current_time = datetime.now() + current_weekday = current_time.weekday() + target_time = current_time.replace(hour=hour, minute=minute, second=second, microsecond=0) + if current_weekday in repeat_days: + if target_time > current_time: + return target_time + repeat_days_sorted = sorted(repeat_days) + for day in repeat_days_sorted: + if day > current_weekday: + days_until = day - current_weekday + next_time = target_time + timedelta(days=days_until) + return next_time + days_until = 7 - current_weekday + repeat_days_sorted[0] + next_time = target_time + timedelta(days=days_until) + return next_time