Schedule hours calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    Schedule hours calculator

    I'm trying to write a program for myself, allowing me to enter my schedule for work and have it calculate the total hours for the week. I'm storing the hours and minutes for each day as a byte, since the maximum value for either will never exceed 24. Since I frequently work until midnight (or later) I need it to be able to handle that. This is where I'm having issues. If I only work until midnight then I'm fine, since I made is so that midnight is actually read as 24 (whereas in real military time, midnight is the 0th hour.) So for example, a 3:30 pm to midnight shift will properly show as 8 hours and 30 minutes, but 3:30 pm to 12:01 am will show as a 9 hours and 227 minutes.

    I need to figure out a better way to determine the number of hours between a given start and end time, taking into account that the end time might be past midknight, or that for a short time the start time might actually be after the end time. I want to make sure the byte doesn't loop around and show something like 250 hours. Any suggestions?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I once had a 'midnight' problem with a program that controlled electromagnetic coils.
    Here a certain voltage had to be applied for a certain period.
    I nearly burnt them out once, when the program hung at midnight.
    The only solution I found was to continually check the time.
    Then at midnight call a bespoke function to do the neccesary calculations.
    Not tidy I admit, but it may be the only way.

    Comment

    • Christian Binder
      Recognized Expert New Member
      • Jan 2008
      • 218

      #3
      When you're saving your hours and minutes in 24-hours and minutes seperately, you just have to add 24 when the end-hours are less than the beginning-hours

      e.g. 3pm to 12:01am
      15:00 to 00:01
      00 < 15 so we add 24 to 00, than subtract 15
      24-15 is 9.

      For minutes we do the same, but there we add 60.

      Look at the following if that helps.

      Code:
      byte hours1, hours2, hoursDiff;
      byte mins1, mins2, minsDiff;
      
      hours1 = 15;  mins1 = 30; //15:30
      hours2 =  0;  mins2 = 15; //00:15
      
      if (hours2 < hours1)
        hours2 += 24;
      
      hoursDiff = (byte)(hours2 - hours1);
      
      if (mins2 < mins1) {
        hoursDiff--;
        mins2 += 60;
      }
      
      minsDiff = (byte)(mins2 - mins1);

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        I did that, and it seemed to work, but I noticed that if I set the start time to 3:30pm and the end time to 3:31pm, I get 255 hours and 59 minutes. The byte is still looping around. Here is my code:

        Code:
        // Get 24-based start hour
        if (ampmStart && startHours == 12)
            startHours = 0;
        else if (!ampmStart && startHours < 12)
             startHours += 12;
        
        // Get 24-based end hour
        if (ampmEnd && endHours == 12)
            endHours = 0;
        else if (!ampmEnd && endHours < 12)
            endHours += 12;
        
        // Correct for past-midnight ending hour
        if (endHours < startHours)
            endHours += 24;
        
        byte hoursDiff = (byte)(endHours - startHours);
        
        // Correct for past-midnight ending minutes
        if (endMins < startMins)
        {
            hoursDiff--;
            endMins += 60;
        }
        
        byte minsDiff = (byte)(endMins - startMins);
        
        totalHours = Math.Round((double)hoursDiff + ((double)minsDiff / (double)60), 2);
        
        lblTotalHours.Text = hoursDiff.ToString().PadLeft(2, '0') + ":" + minsDiff.ToString().PadLeft(2, '0') + " Hours";
        I tried changing this line:

        if(endHours < startHours)

        to:

        if(endHours <= startHours)

        But that made it so that if I had 3:00pm start and 3:01pm end, it will say that I've got 24 hours and 1 minute, it should say just 1 minute.

        Why is this so complicated!?

        Comment

        • Christian Binder
          Recognized Expert New Member
          • Jan 2008
          • 218

          #5
          I think, you've got something wrong with your conversion to 24-hours.
          I tried only the code for the date-diff and it works.

          Code:
          byte startHours = 03, startMins = 00; //3:00
          byte endHours   = 03, endMins   = 01; //3:01
          
          // Correct for past-midnight ending hour
          if (endHours < startHours)
            endHours += 24;
          byte hoursDiff = (byte)(endHours - startHours);
          
          // Correct for past-midnight ending minutes
          if (endMins < startMins) {
            hoursDiff--;
            endMins += 60;
          }
          
          byte minsDiff = (byte)(endMins - startMins);

          Comment

          Working...