You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于时间的轮换脚本异常实现咨询:ID193234项需1小时轮换

时间轮换脚本异常规则实现方案

核心逻辑

解决这个问题的关键在于:每次计算下一个轮换时间前,检查当前活跃项的ID是否为"193234",如果是,则将下一次间隔设为60分钟,否则保持30分钟的默认间隔。同时要处理轮换的循环逻辑(即遍历到列表末尾后回到开头),并准确生成当前值及后续3个待轮换的值。

代码实现示例(Python)

假设你的rotation是包含ID标识的项列表,初始起始时间和起始值已知,以下是可直接复用的逻辑:

import datetime

# 定义轮换项列表(示例结构,可根据实际调整)
rotation = [
    {"id": "1001", "name": "项A"},
    {"id": "193234", "name": "特殊项"},
    {"id": "1003", "name": "项C"},
    {"id": "1004", "name": "项D"},
    {"id": "1005", "name": "项E"}
]

# 配置初始参数
start_time = datetime.datetime(2024, 5, 20, 9, 0)  # 指定起始时间
start_item_index = 0  # 指定起始项在rotation中的索引

def get_current_and_upcoming_items(current_time):
    # 计算从起始时间到当前时间的总分钟数
    total_minutes = (current_time - start_time).total_seconds() // 60
    current_index = start_item_index
    temp_time = start_time

    # 定位当前时间对应的项
    while temp_time < current_time:
        current_item = rotation[current_index]
        # 根据当前项设置间隔
        interval = 60 if current_item["id"] == "193234" else 30
        temp_time += datetime.timedelta(minutes=interval)
        if temp_time <= current_time:
            current_index = (current_index + 1) % len(rotation)

    # 收集当前值及后续3个值
    result = []
    # 添加当前项
    current_item = rotation[current_index]
    prev_interval = 60 if current_item["id"] == "193234" else 30
    result.append({
        "item": current_item,
        "start_time": temp_time - datetime.timedelta(minutes=prev_interval),
        "end_time": temp_time
    })

    # 添加后续3个项
    next_index = (current_index + 1) % len(rotation)
    next_time = temp_time
    for _ in range(3):
        prev_item = result[-1]["item"]
        # 以上一个项的ID决定当前间隔
        interval = 60 if prev_item["id"] == "193234" else 30
        next_start_time = next_time
        next_end_time = next_time + datetime.timedelta(minutes=interval)
        result.append({
            "item": rotation[next_index],
            "start_time": next_start_time,
            "end_time": next_end_time
        })
        # 更新下一轮的时间和索引
        next_time = next_end_time
        next_index = (next_index + 1) % len(rotation)

    return result

# 测试:传入当前时间
current_time = datetime.datetime.now()
items = get_current_and_upcoming_items(current_time)

# 输出结果
print("当前及后续3个轮换项:")
for idx, item_info in enumerate(items):
    status = "当前值" if idx == 0 else f"第{idx}个即将到来的值"
    print(f"{status}: ID={item_info['item']['id']},名称={item_info['item']['name']},时间段={item_info['start_time'].strftime('%Y-%m-%d %H:%M')} ~ {item_info['end_time'].strftime('%Y-%m-%d %H:%M')}")

关键细节说明

  1. 定位当前项:通过循环累加间隔时间,从起始时间逐步推进到当前时间,确保准确找到当前活跃的轮换项。
  2. 特殊间隔处理:每次计算下一个轮换时间时,以上一个项的ID作为判断依据——如果上一个项是"193234",则下一次间隔设为60分钟,否则用30分钟。
  3. 循环轮换:使用(index + 1) % len(rotation)实现列表末尾到开头的循环跳转。
  4. 结果输出:返回的结果包含每个项的ID、名称以及对应的时间区间,清晰展示当前和后续三个值的信息。

适配调整建议

  • 如果你的rotation是纯字符串列表(无ID字段),可以单独维护一个ID映射列表,比如rotation_ids = ["1001", "193234", "1003", ...],通过索引对应判断。
  • 如果起始值不是通过索引指定,而是直接指定项的ID,可以先遍历rotation找到对应的起始索引。

内容的提问来源于stack exchange,提问作者Zerers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 13:17:37