How to calculate specific hours between 2 dates

Hi there,

I am trying to find out a way to calculate how many hours are between 2 dates but from a specific time range, actually working hours (Monday to Friday 09:00 - 18:00).

What I mean is for example
date1 = Monday 21 July 2008 22:00:00 so in python 2008-07-21 22:00:00
date2 = Wednesday 23 July 2008 12:00:00 so in python 2008-07-23 12:00:00

and I want to find a way to say
date2 - date1 = 12 hours

Always the date2 is in working hours.
Anybody has an idea how to do this? Actually I don't care about the programming language I just want to find the best logic to do this and then I will write it in python

Still some quirks below, like adjusting start & end times to 900 or 1800 (your input example was outside this range). However, perhaps this gives you a basis to begin thinking about a process.

curr_day = day1
while [ curr_day .le day2 ]
  do
  if [ day_of_wk in {MTWTF} ]
    then
#assume full day; hours in hundreds; over-write as needed
    day_hr=900
    if [ curr_day .eq day1 ]
      then
      day_hr=(1800 - day1(hr))
    fi
    if [ curr_day .eq day2 ]
      then
      day_hr=(day2(hr) - 900)
    fi
    hr_tot=(hr_tot + day_hr)
  fi
  curr_day=(curr_day+1)
done

I thought the same way, with multiple ifs and adjusting the starting date (the closing date is always within the working times) but I thought maybe there is a more effective way to do this.

Perhaps multiply the number of days difference by 9 (for 9 working hours) and subtract the working hours not included on the first day and last day? Similarly subtract 9 from days which are weekends.

There are other reasons to do this one day at a time:
(a) account for holidays, vacations
(b) allow for lookups when special conditions exist for a day

By this, think of a day as 20080725. Now, while inside your main loop, do the following; although with a $var and not a fixed #.

if [ -s /work/calendar/20080725 ]    
  then
  read the file to learn the special condition for the day
fi

The file may contain various fields you could read, like
max_hrs=400 ;scheduled half day
max_hrs=0 ;no work day
etc..

Good one, but actually I don't want to take it so far. The only thing I will do is to subtract the weekends. I found a really interesting article how to find how many weekends are between two dates. http://dirtsimple.org/2004/10/nineteen-hundred-weekends.html

So what I thought so far is:
I have the startdate and the closing date.
I wrote this to adjust the startdate to working hours

    if (startdate.day_of_week in (5,6)) or (startdate.day_of_week == 4 and startdate.hour > 18):
        print 'I am in the first if'
        while True:
            startdate = startdate + mx.DateTime.RelativeDateTime(days=+1)
            print startdate.day_of_week
            if startdate.day_of_week == 0:
                startdate = startdate + mx.DateTime.RelativeDateTime(hour=9,minute=0,second=0)
                break
    elif startdate.day_of_week in (0,1,2,3,4) and startdate.hour < 9:
        startdate = startdate + mx.DateTime.RelativeDateTime(hour=9,minute=0,second=0)
    elif startdate.day_of_week in (0,1,2,3) and startdate.hour > 18:
        startdate = startdate + mx.DateTime.RelativeDateTime(days=+1,hour=9,minute=0,second=0)

but this is only if the startdate is out of the working hours.
It becomes harder when the starting date is for example 17:00. Then I have to keep this 1hour in a temp value calculate for the closing date and add it at the end.

Edit:
If the startdate is within the working times I guess I just have to subtract 15 hours