Hướng dẫn how do you check if a time is between two times in python? - làm cách nào để kiểm tra xem thời gian có nằm giữa hai lần trong python không?

Hãy nhìn vào gói py-time-Between: //pypi.org/project/py-time-beltween/

Trường hợp kiểm tra:

from datetime import time
from timebetween import is_time_between


def test_is_time_between[]:
    t, s, e = time[0], time[0], time[0]
    assert is_time_between[t, s, e]

    t, s, e = time[0, 0, 0, 1], time[0], time[0, 0, 0, 2]
    assert is_time_between[t, s, e]

    t, s, e = time[0, 0, 0, 1], time[0, 0, 0, 1], time[0, 0, 0, 2]
    assert is_time_between[t, s, e]

    t, s, e = time[0, 0, 0, 2], time[0, 0, 0, 1], time[0, 0, 0, 2]
    assert is_time_between[t, s, e]

    t, s, e = time[0, 0, 1], time[23, 59, 59], time[0, 0, 2]
    assert is_time_between[t, s, e]

    t, s, e = time[12, 0, 0], time[23, 59, 59], time[0, 0, 0]
    assert is_time_between[t, s, e]

    t, s, e = time[23, 59, 57], time[23, 59, 59], time[23, 59, 57]
    assert is_time_between[t, s, e]

    t, s, e = time[23, 59, 58], time[23, 59, 59], time[23, 59, 57]
    assert not is_time_between[t, s, e]

    t, s, e = time[22], time[22], time[5, 59, 59]
    assert is_time_between[t, s, e]

Làm thế nào để kiểm tra xem thời gian hiện tại có trong một phạm vi trong Python không?

Xây dựng vấn đề

Với dấu thời gian x như thời gian hiện tại và khoảng thời gian được đánh dấu bằng dấu thời gian startend.a timestamp x such as the current time and a time interval marked with start and end timestamps.

Mục tiêu: Tạo một chức năng kiểm tra xem dấu thời gian nhất định có rơi vào khoảng [start, end] hay không, do đó

import datetime

def time_in_range[start, end, current]:
    """Returns whether current is in the range [start, end]"""
    return start 

Bài Viết Liên Quan

Chủ Đề