How to exclude certain hours between two date differenece?

Viewed 366

I want to get the total hours difference between two timestamps but I want to exclude certain hour in there . I want to count only these hours from 9AM-6PM everyday and other non working hours should be ignored.

I am ready to use panda as well if its not possible with python datetime module only.

  from datetime import datetime
  def get_total_hours(model_instance):
      datetime = model_instance.datetime
      now = datetime.now()
      diff = now - datetime
      total_secs = diff.total_seconds()
      hours =  total_secs // 3600
      return hours
5 Answers

I'd envision the problem geometrically:

          day n     day n+1   etc...
        _______________________________________________________________________
00:00   |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
EXCLUDED|         |         |         |         |         |-- t2 ---|         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
10:00   |.........|.........|.........|.........|.........|.........|.........|
        |         |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|         |         |
        |         |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|         |         |
        |         |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|         |         |
COUNTED |-- t1 ---|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|         |         |
        |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|         |         |
        |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|         |         |
        |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|         |         |
17:00   |.........|.........|.........|.........|.........|.........|.........|
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
EXCLUDED|         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
        |         |         |         |         |         |         |         |
23:59   |_________|_________|_________|_________|_________|_________|_________|

Whatever t1 and t2 might be, your answer is the area of a rectangle (height x width) plus some edge case handling at the ends.

First, check the remaining hours for each date.

Then, check the day gap between the start and end dates.

import datetime

dt_start = datetime.datetime(year=2022, month=7, day=31, hour=20)
dt_end = datetime.datetime(year=2022, month=8, day=2, hour=10, minute=30)

dt_start_gap_lb = dt_start.replace(hour=10, minute=0, second=0, microsecond=0)
dt_start_gap_ub = dt_start.replace(hour=17, minute=0, second=0, microsecond=0)
dt_end_gap_lb = dt_end.replace(hour=10, minute=0, second=0, microsecond=0)
dt_end_gap_ub = dt_end.replace(hour=17, minute=0, second=0, microsecond=0)

dt_start_remain = dt_start_gap_ub - max(dt_start_gap_lb, dt_start)
dt_end_remain = min(dt_end_gap_ub, dt_end) - dt_end_gap_lb

dt_start_remain_hours = 0 if dt_start_remain.days < 0 else dt_start_remain.seconds / 3600.0
dt_end_remain_hours = 0 if dt_end_remain.days < 0 else dt_end_remain.seconds / 3600.0

gap_hours = max((dt_end_gap_lb - dt_start_gap_ub).days * (17-10) + dt_start_remain_hours + dt_end_remain_hours, 0)

Results:

dt_start = datetime.datetime(year=2022, month=7, day=31, hour=20)
dt_end = datetime.datetime(year=2022, month=8, day=2, hour=10, minute=30)
> 7.5
dt_start = datetime.datetime(year=2022, month=7, day=31, hour=16, minute=30)
dt_end = datetime.datetime(year=2022, month=8, day=2, hour=10, minute=30)
> 8.0
dt_start = datetime.datetime(year=2022, month=7, day=31, hour=5, minute= 30)
dt_end = datetime.datetime(year=2022, month=7, day=31, hour=10, minute=30)
> 0.5

Given a model Foo with a field timestamp, and t1 and t2 defined as per the question:

Foo.objects.filter(timestamp__gte=t1, timestamp__lte=t2
   ).exclude( timestamp__hour__gte=17 # up to 23 by definition
   ).exclude( timestamp__hour__lte=9  # down to 0 by definition
   )

Alternatively,

    .exclude( timestamp__hour__in=[
        0,1,2,3,4,5,6,7,8,9,17,18,19,20,21,22,23 ])
from BusinessHours import BusinessHours
import datetime

startTime = datetime.datetime(2022, 5, 1, 21, 35, 15)
endTime = datetime.datetime(2022, 5, 15, 11, 00, 10)

businessHours = BusinessHours(startTime , endTime , worktiming=[9, 18], weekends=[6, 7], holidayfile=None)
print(businessHours.gethours())

According to the requirements in the question

  • get the total hours difference between two timestamps
  • Want to exclude certain range of hours from each day like include only 0900 - 1800

Example code scenario.

given that there is only 24 hours in a day and you want to exclude certain hours from each day this example considers for a difference of 100 days between two timestamps and exclude the hours outside of 0900 to 1800 expected_answer = 900 hours

from datetime import datetime, timedelta
HOURS_IN_A_DAY = 24
SECONDS_IN_A_HOUR = 3600
HOURS_IN_A_DAY_TO_EXCLUDE = 9

def convert_seconds_to_hours(seconds):
    return seconds / SECONDS_IN_A_HOUR


expected_answer = 900
datetime_from = datetime.utcnow()
datetime_to = datetime_from + timedelta(days=100)


datetime_difference = datetime_to - datetime_from
total_hours_to_exclude = timedelta(hours=(HOURS_IN_A_DAY - HOURS_IN_A_DAY_TO_EXCLUDE) * datetime_difference.days)
datetime_difference_with_excluded_hours = datetime_difference - total_hours_to_exclude
total_datetime_difference_in_hours = convert_seconds_to_hours(datetime_difference_with_excluded_hours.total_seconds())
print(total_datetime_difference_in_hours)
assert total_datetime_difference_in_hours == expected_answer
Related