Omega
Omega

Reputation: 267

python given 2 datetimes get how many hours are between 24:00 - 06:00

I am trying to calculate if the range of datetimes given, get how many night time hours goes through 24:00-06:00.

Example:

If given dates are Aug 27, 23:00 - Aug 29, 01:00 ( 25 hours). so the answer should be 7 hours night time.

OR

If given dates are Aug 27, 05:00 - Aug 29, 07:00 (50 hours ), so the answer should be 13 hours night time.

I know i have to calculate the difference in days

diff_days = abs(convert_end_date - convert_start_date).days

and then make a condition if those times are in the range of the 2 given datetimes.

i will place it in a while loop because it will depend of how many days are.

Hope it is clear enough cause i don't know how else to explain more simple then this...

This is the code that i have, but it doesn't include if its more then 24 hours...

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")

Thank you, have a nice day.

Upvotes: 0

Views: 66

Answers (1)

AntiMatterDynamite
AntiMatterDynamite

Reputation: 1512

this code can do it, with some limitations:

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

note that it doesn't factor in the minutes and seconds part of the datetime object, it also doesn't take care of timezones and daylight savings but other than those it does what you want

i should also note that the math done is simplified since your range is 0am-6am if you would change the hours you want to count say to 1am-7am you would need more complex math to figure out the hours

Upvotes: 1

Related Questions