首页 > 解决方案 > 给定 2 个日期时间的 python 获取 24:00 - 06:00 之间的小时数

问题描述

我正在尝试计算给定的日期时间范围是否得到 24:00-06:00 的夜间时间。

例子:

如果给定日期是 8 月 27 日 23:00 - 8 月 29 日 01:00(25 小时)。所以答案应该是晚上 7 小时。

或者

如果给定的日期是 8 月 27 日 05:00 - 8 月 29 日 07:00(50 小时),那么答案应该是夜间 13 小时。

我知道我必须计算天数的差异

diff_days = abs(convert_end_date - convert_start_date).days

如果这些时间在 2 个给定日期时间的范围内,则创建一个条件。

我会把它放在一个while循环中,因为它取决于有多少天。

希望它足够清楚,因为我不知道还有什么比这更简单的解释......

这是我拥有的代码,但它不包括超过 24 小时...

convert_start_date = datetime.fromtimestamp(start_date)
convert_end_date = datetime.fromtimestamp(end_date)

min_midnight = datetime.combine(convert_start_date, time.min)
next_day = min_midnight + dt.timedelta(days=1)
six_oclock_next_day = next_day + dt.timedelta(hours=6)


# if between 00:00 - 06:00
    if min_midnight < convert_start_date < six_oclock_today and min_midnight < convert_end_date < six_oclock_today:
        night_hours = (convert_end_date - convert_start_date).seconds / 60
        print(night_hours / 60)
        print("If start date and end date between midnight and 6am")

    # if start date between 00:00 - 06:00 and end date not
    if min_midnight < convert_start_date < six_oclock_today and (
    not min_midnight < convert_end_date < six_oclock_today):
        night_hours = (six_oclock_today - convert_start_date).seconds / 60
        print(night_hours / 60)
        print("If start date between midnight and 6 am and not the end date")

    # if between 06:00 - 2400
    if six_oclock_today < convert_start_date < next_day and six_oclock_today < convert_end_date < next_day:
        day_hours = (convert_end_date - convert_start_date).seconds / 60
        print(day_hours / 60)
        print("If start date between 6am and midnight")

    # if start date between 06:00 - 24:00 and end date not
    if six_oclock_today < convert_start_date < next_day and (not six_oclock_today < convert_end_date < next_day):
        day_hours = (next_day - convert_start_date).seconds / 60
        print(day_hours / 60)
        print("If end date between 6am and midnight")

    # if start date between next day 00:00 - 06:00
    if next_day < convert_start_date < six_oclock_next_day:
        night_hours = (six_oclock_next_day - convert_start_date).seconds / 60
        print(night_hours / 60)
        print("If start date lower then next day 6 oclock")

    # if end date between next day 00:00 - 06:00
    if next_day < convert_end_date < six_oclock_next_day:
        night_hours = (convert_end_date - next_day).seconds / 60
        print(night_hours / 60)
        print("If end date between next day midnight and 6 oclock")

    # if end date next day after tomorrow 06:00-24:00
    if six_oclock_next_day < convert_end_date:
        night_hours = 6.0 * 60
        day_hours = day_hours + (convert_end_date - six_oclock_next_day).seconds / 60
        print(day_hours / 60 + night_hours)
        print("If end date the day after after 6 am")

感谢您有一个愉快的一天。

标签: python-3.x

解决方案


这段代码可以做到,但有一些限制:

def count_hours_between_00_and_06(start_date, end_date):
    if end_date < start_date:
        start_date, end_date = end_date, start_date

    start_hour = start_date.hour
    end_hour = end_date.hour
    if start_date.date() == end_date.date():
        #dates are in the same day

        if start_hour > 6:
            return 0
        else:
            end_hour = end_hour if end_hour < 6 else 6
            return end_hour - start_hour
    else:
        days_delta = (end_date.date() - start_date.date).days - 1
        total_hours = 0
        if start_hour < 6:
            total_hours += 6 - start_hour
        total_hours += end_hour if end_hour < 6 else 6
        total_hours += days_delta * 6
        return total_hours

请注意,它不考虑datetime对象的分钟和秒部分,它也不考虑时区和夏令时,但除了那些你想要的

我还应该注意,由于您的范围是早上 0 点到早上 6 点,所以完成的数学运算已经简化,如果您将要计算的时间更改为凌晨 1 点到早上 7 点,您将需要更复杂的数学来计算时间


推荐阅读