datetime
python3

【Python】指定間隔を5分区切りにする【datetime】

指定間隔の時間を5分区切りにする

from datetime import datetime, timedelta

def split_span_by_5min(start_time, end_time):
    """
    start_time, end_time 間を5分毎に区切ったリストにして返す
    (end_timeは含まない)
    param start_time: datetime.datetime
    param end_time:   datetime.datetime
    return list = [datetime.datetime,..]
    """
    span_list = list()
    handle_time = start_time
    while(True):
        span_list.append(handle_time)
        next_time = handle_time + timedelta(minutes=5)
        if next_time < end_time:
            handle_time = next_time
        else:
            break

    return span_list