How to calculate specific hours between 2 dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to calculate specific hours between 2 dates
# 1  
Old 07-25-2008
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
# 2  
Old 07-25-2008
Tools just thinking out loud on this - no real code yet

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.


Code:
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

# 3  
Old 07-25-2008
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.
# 4  
Old 07-25-2008
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.
# 5  
Old 07-25-2008
Hammer & Screwdriver I think the one-day-at-a-time is best

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 #.
Code:
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..
# 6  
Old 07-25-2008
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

Code:
    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

Last edited by sickboy; 07-25-2008 at 11:50 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Delete files older than 1week(dates need to be calculate based on file name)

Objective: We have multiple files in a folder and we want to delete all files except for last 1 week files. Note: We are copying these files from original location to this temporary location. So time shown for these files are time when we copied to this location. Not that when file was created.... (2 Replies)
Discussion started by: Agoyals1986
2 Replies

2. Shell Programming and Scripting

Find hours difference between two dates in given format

I have two dates in below format, how would I find the hours difference between the two dates. Im using AIX and ksh. Current date : Wed May 17 14:34:41 SGT 2017 File date : Thu Apr 27 20:52:41 SGT 2017 (3 Replies)
Discussion started by: simpltyansh
3 Replies

3. UNIX for Advanced & Expert Users

How to get ALL update dates and hours of a file?

Hello, I have a question please, How could I get ALL update dates and hours of a file ? For example: I modified a file on 09/04/2014 at 7:am for the first time. Then, I modified again the same file on 09/04/2014 at 9 am and finally, I modified the same file again on 10/04/2014 at... (3 Replies)
Discussion started by: nurinolo
3 Replies

4. Shell Programming and Scripting

Collect files for specific hours

I have to fetch files from a location hour wise. Eg files available at location /tmp/data/ are A20140205.1300-1315...... . . A20140205.1400-1415...... . . A20140205.1700-1715...... . . . . Below is the code I have prepared. But it works only for one hour. For instance... (1 Reply)
Discussion started by: Saidul
1 Replies

5. Shell Programming and Scripting

Calculate time difference between pst and pdt dates in perl

Hi, how to calculate the time difference between PST date and PDT date in perl scripting. date1: Mon Dec 31 16:00:01 PST 2015 date2: Tue Mar 19 06:09:30 PDT 2013 and also difference between PST-PST and PDT-PDT need difference in months or days (months prefereble). (3 Replies)
Discussion started by: praveen265
3 Replies

6. Shell Programming and Scripting

Shell script to calculate difference between 2 dates

shell script to calculate difference between 2 dates (3 Replies)
Discussion started by: gredpurushottam
3 Replies

7. Shell Programming and Scripting

How to Calculate the difference between two dates?

I want the difference between two following date using scripts in terms of no.of days. How I can accomplish this. lastdate=Tue Nov 13 10:30:56 2012 currdate=Wed Dec 15 15:58:21 PAKST 2012 Ouput should be like this: Your Password will expire after = 32 Days on Wed Dec 15 15:58:21 PAKST... (1 Reply)
Discussion started by: m_raheelahmed
1 Replies

8. Shell Programming and Scripting

Calculate the number of days between 2 dates - bash script

I wrote the day calculator also in bash. I would like to now, that is it good so? #!/bin/bash datum1=`date -d "1991/1/1" "+%s"` datum2=`date "+%s"` diff=$(($datum2-$datum1)) days=$(($diff/(60*60*24))) echo $days Thanks in advance for your help! (3 Replies)
Discussion started by: kovacsakos
3 Replies

9. Web Development

Calculate the number of days between 2 dates - PHP

Is this code good for this purpose? <?php $date1 = mktime(0,0,0,01,01,1991); $date2 = mktime(0,0,0,03,22,2012); $diff = $date2 - $date1; $days = $diff / (60*60*24); echo ($days . "<br />"); ?> (3 Replies)
Discussion started by: kovacsakos
3 Replies

10. Shell Programming and Scripting

Calculate days between yyyyMmmdd dates on Solaris

I extract dates from the log file and need to calculate days between two dates. My dates are in yyyyMmmdd format. Example: $d1=2011 Oct 21 $d2=2012 Feb 20 I need to calculate the number of days between $d2 and $d1. This is on Solaris. Any ideas? Thanks, djanu (4 Replies)
Discussion started by: djanu
4 Replies
Login or Register to Ask a Question