Learn how to a build a cloud-first strategyRegister Now


calculating work shift time rather than actual time.

Posted on 2004-11-04
Medium Priority
Last Modified: 2008-03-03
I am working with a proprietery language of CA & it's very much similar to C and hence I find this a good place to put my question.
I want to make some workshift calculations in a function that takes start date & end date as arguments and calculates the difference in time taking into account the shift timings. for e.g. time difference between 11/4/2004 09:00:00 & 11/5/2004 09:00:00 should be  28800 seconds (8 hours) and not 24 hours (assuming my shift timings for that day are defined as 09:00-17:00).
I have "date" data type and some in-build functions that can be used. e.g. now() returns current date in "mm/dd/yyyy hh:mm:dd" format and typecasting a date datatype to integer gives number of seconds elapsed since some reference time. e.g.
(int)now() will return 231321....(some integer value).
This is all i have for date time manipulation. I have other standard c operators like if, else, ==, etc. which i can use.
The shift times are defined in another table and right now i am only concerned with regular shif times. i.e. Mon-Fri 09:00-17:00, Sat-Sun 00:00-00:00
I will be glad if someone can help me out.
Thanks in advance.
Question by:picasothakkar
LVL 46

Expert Comment

by:Kent Olsen
ID: 12497337

Hi Picaso,

It sounds like this can be broken down into the difference of two easy steps.

Compute the difference between the start and end datetimes.
Compute the shift difference.
Subtract them.

Or is this more complicated than that?

Author Comment

ID: 12497767
It's more complicated esp. since we have to keep track of the week day & the workshift hours defined for that day.
LVL 46

Expert Comment

by:Kent Olsen
ID: 12498137

Ok.  From your first post I don't know where you're going.  What calculations are you trying to solve?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 12499409
is there any way you can find what day a particular date is?

-- Adil

Author Comment

ID: 12499667
That is the major calculation. We don't have provision to find out what day a particular date is. I started with something like taking a reference date which we know is Sunday & then carry out iterations till the point it reaches the date we are interested in.
I think i need to do following:
lets say we are calculating time difference between 1st Nov 10 am & 2nd  Nov 10 am.
1. Take a reference date which falls on sunday and cast it to int.
2. Start increamenting it by adding total seconds in week and arrive to sunday of "start date & time" (1st Nov 10 am) we are interested in. Lets say we got the "weekly offset" which gives us integer value of sunday 00:00:00.
I am still into middle of it....Let me know if something clicks you

Assisted Solution

booki earned 750 total points
ID: 12501642
Calculating the day of the week is not that difficult.  There's quite a few implematations out there of Zeller's Congruence.  Here's one you can use.

int DayOfWeek(int y, int m, int d) { // y += 7e8 // Sun = 0
      if (m<3) { m += 12 ; y--;}
      return (2 + d + (int)((13*m-2)/5) + y + (int)(y/4) - (int)(y/100) + (int)(y/400)) % 7;

LVL 46

Expert Comment

by:Kent Olsen
ID: 12503803
Hi Picasco,

Going back to your original post, you said that now() returns the number of seconds to a base date.

Problem solved.

Determine what date is the base date and what day of the week that this day falls on.

Now you can compute the day of week with:

DayOfWeek = ((now () / 86400) % 7);


Author Comment

ID: 12505124
Thanks so much.. Now i have day of week. From day of week, I can find out what were the shift timings for that day. Now how do i actually calculate then difference? e.g. lets take the case wherein i need to calculate the difference in time for 11/3/2004 12:00:00 & 11/4/2004 09:00:00 & my shift timings for both these days are 09:00 - 17:00.
so my answer should be  18000 seconds. How do i arrive to this figure?

Author Comment

ID: 12505393
Hi Kent,
Can you tell me workaround for using "%" operator? I don't have it!!!!
LVL 46

Accepted Solution

Kent Olsen earned 750 total points
ID: 12505503
Hi Picaso,

You may have to "roll up" the total times until you get there.  If your language allows you to do so.  Here's the steps to computing shift time spanning different days.

Calculate the first time, in seconds.  Noon is 12 hours * 3600 seconds, or 43200 seconds.

Subtract this value from the shift end time.  Shift end is 17 hours * 3600, or 61200.  The difference is 18000.

Add 8 * 3600, or 28800 for every WHOLE day between the start and end dates.  In this case, the days are consecutive so there is nothing to add.

Calculate the end time, in seconds.  9AM is 9 hours * 3600 seconds, or 32400.

Subtract the shift start time from the end-time.  The shift start time is 9AM, 9 hours * 3600, or 32400.

In this case, the end time and the shift start time are the same, so the result is zero.

Add the last calculation to the number that has been rolled up.  (First days calculation + 2800 * whole days.)

The result is the shift time, in this case, 18000.



Author Comment

ID: 12506405
Hi kent plz check this out. can you check the results with c compiler? it will really help.
plz ignore TZC for now. you can assume it constant.

   int iSdate;
   int iEdate;

   int DayOffset;
   int NumberOfWeeks;
   int FrTimes[21];
   int ToTimes[21];
   int count;
   int todate;
   int accum;
   int numdayper;

   int WeekOffset; // Should be a Sunday at 00:00 before the StartDate
   int efound;
   int iInterval;
   int DOW;
   int totime;
   int fromtime;
   int numweek;
   date TempDate1;
   date TempDate2;
   int TempDate3;

   TempDate3 = (int)((date)"01/01/1999 00:00:00");
   count = 0;
//check "day"From values, which are part of object.
   if (!is_empty(SundayFrom)) {
      TempDate1 = (date)format("01/01/1999 %s:00", SundayFrom);
      TempDate2 = (date)format("01/01/1999 %s:00", SundayTo);
      FrTimes[count] = (int)TempDate1 - TempDate3 + 0 + TZC;
      ToTimes[count] = (int)TempDate2 - TempDate3 + 0 + TZC;
      count = count + 1;
   if (!is_empty(MondayFrom)) {
      TempDate1 = (date)format("01/01/1999 %s:00", MondayFrom);
      TempDate2 = (date)format("01/01/1999 %s:00", MondayTo);
      FrTimes[count] = (int)TempDate1 - TempDate3 + 86400 + TZC;
      ToTimes[count] = (int)TempDate2 - TempDate3 + 86400 + TZC;
      count = count + 1;
   if (!is_empty(TuesdayFrom)) {
      TempDate1 = (date)format("01/01/1999 %s:00", TuesdayFrom);
      TempDate2 = (date)format("01/01/1999 %s:00", TuesdayTo);
      FrTimes[count] = (int)TempDate1 - TempDate3 + 172800 + TZC;
      ToTimes[count] = (int)TempDate2 - TempDate3 + 172800 + TZC;
      count = count + 1;
   if (!is_empty(WednesdayFrom)) {
      TempDate1 = (date)format("01/01/1999 %s:00", WednesdayFrom);
      TempDate2 = (date)format("01/01/1999 %s:00", WednesdayTo);
      FrTimes[count] = (int)TempDate1 - TempDate3 + 259200 + TZC;
      ToTimes[count] = (int)TempDate2 - TempDate3 + 259200 + TZC;
      count = count + 1;
   if (!is_empty(ThursdayFrom)) {
      TempDate1 = (date)format("01/01/1999 %s:00", ThursdayFrom);
      TempDate2 = (date)format("01/01/1999 %s:00", ThursdayTo);
      FrTimes[count] = (int)TempDate1 - TempDate3 + 345600 + TZC;
      ToTimes[count] = (int)TempDate2 - TempDate3 + 345600 + TZC;
      count = count + 1;
   if (!is_empty(FridayFrom)) {
      TempDate1 = (date)format("01/01/1999 %s:00", FridayFrom);
      TempDate2 = (date)format("01/01/1999 %s:00", FridayTo);
      FrTimes[count] = (int)TempDate1 - TempDate3 + 432000 + TZC;
      ToTimes[count] = (int)TempDate2 - TempDate3 + 432000 + TZC;
      count = count + 1;
   if (!is_empty(SaturdayFrom)) {
      TempDate1 = (date)format("01/01/1999 %s:00", SaturdayFrom);
      TempDate2 = (date)format("01/01/1999 %s:00", SaturdayTo);
      FrTimes[count] = (int)TempDate1 - TempDate3 + 518400 + TZC;
      ToTimes[count] = (int)TempDate2 - TempDate3 + 518400 + TZC;
      count = count + 1;
   numdayper = count;

   // Lets find out what the start day is
   WeekOffset=943747200; // This was a Sunday at 00:00
      iInterval = 0;
      FrTimes[numdayper] = 604800 + TZC;
      while (iSdate < iEdate) {
            numweek = iSdate - (WeekOffset + TZC);
            numweek = numweek / 604800;
            WeekOffset = WeekOffset + (604800 * numweek);
            // find a FromTime >= iSdate
            DOW = numdayper;
            for (count=0;count<numdayper;count++) {
               if (((FrTimes[count] + WeekOffset) <= iSdate) && ((FrTimes[count+1] + WeekOffset) > iSdate)) {
                  DOW = count;
            if (DOW == numdayper) {
            // StartDate is not between any Fr/To pair, advance to next week
               iSdate = WeekOffset + 604800;
            } else {
               if (iSdate < (FrTimes[DOW] + WeekOffset)) {
                  // less than start of this period
                  todate = FrTimes[DOW] + WeekOffset ;
                  accum = 0;
               } else if (iSdate < (ToTimes[DOW] + WeekOffset)) {
                  // more than the start but less than the end
                  todate = ToTimes[DOW] + WeekOffset + 1 ;
                  accum = 1;
               } else if (iSdate < (FrTimes[DOW+1] + WeekOffset)) {
                  // after the end of this one but before the beginning of the next
                  todate = FrTimes[DOW+1] + WeekOffset + 1;
                  accum = 1;
               if (iEdate < todate) {
                  // Dont go too far...
                  todate = iEdate;
               if (accum == 1) {
                  iInterval = iInterval + todate - iSdate;
               iSdate = todate;
LVL 46

Expert Comment

by:Kent Olsen
ID: 12507333
Not much that a C compiler will do to help, here.

The 'date' data type, format() function, etc. will keep the program from compiling and running.


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question