Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C Program to Calculate Difference Between Two Time Periods .

C Program to Calculate Difference Between Two Time Periods .

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    I am not able to understand the logic used in the program below. Can someone elaborate how does the logic works in this program.

    #include

    struct TIME
    {
    int seconds;
    int minutes;
    int hours;
    };
    void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);

    int main()
    {
    struct TIME startTime, stopTime, diff;

    printf("Enter start time: \\n");
    printf("Enter hours, minutes and seconds respectively: ");
    scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
    
    printf("Enter stop time: \\n");
    printf("Enter hours, minutes and seconds respectively: ");
    scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
    
    // Calculate the difference between the start and stop time period.
    differenceBetweenTimePeriod(startTime, stopTime, &diff);
    
    printf("\\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
    printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
    printf("= %d:%d:%d\\n", diff.hours, diff.minutes, diff.seconds);
    
    return 0;
    

    }

    //not able to understand the logic.
    void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
    {
    //how does this logic works??.
    if(stop.seconds > start.seconds){
    --start.minutes;
    start.seconds += 60;
    }

    diff->seconds = start.seconds - stop.seconds;
    if(stop.minutes > start.minutes){
        --start.hours;
        start.minutes += 60;
    }
    
    diff->minutes = start.minutes - stop.minutes;
    diff->hours = start.hours - stop.hours;
    

    }

    Thank you

    L J 2 Replies Last reply
    0
    • T Tarun Jha

      I am not able to understand the logic used in the program below. Can someone elaborate how does the logic works in this program.

      #include

      struct TIME
      {
      int seconds;
      int minutes;
      int hours;
      };
      void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);

      int main()
      {
      struct TIME startTime, stopTime, diff;

      printf("Enter start time: \\n");
      printf("Enter hours, minutes and seconds respectively: ");
      scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
      
      printf("Enter stop time: \\n");
      printf("Enter hours, minutes and seconds respectively: ");
      scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
      
      // Calculate the difference between the start and stop time period.
      differenceBetweenTimePeriod(startTime, stopTime, &diff);
      
      printf("\\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
      printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
      printf("= %d:%d:%d\\n", diff.hours, diff.minutes, diff.seconds);
      
      return 0;
      

      }

      //not able to understand the logic.
      void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
      {
      //how does this logic works??.
      if(stop.seconds > start.seconds){
      --start.minutes;
      start.seconds += 60;
      }

      diff->seconds = start.seconds - stop.seconds;
      if(stop.minutes > start.minutes){
          --start.hours;
          start.minutes += 60;
      }
      
      diff->minutes = start.minutes - stop.minutes;
      diff->hours = start.hours - stop.hours;
      

      }

      Thank you

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The logic does not work because it is subtracting the stop time from the start time. It should subtract the start time from the stop time to get the difference. For example a start time of 11:33:47 and a stop time of 15:21:10 gives a difference of -4:12:37. The correct answer is 3:47:23. You can work through the logic by using some random times and working through each line,changing values as necessary and doing the subtractions.

      1 Reply Last reply
      0
      • T Tarun Jha

        I am not able to understand the logic used in the program below. Can someone elaborate how does the logic works in this program.

        #include

        struct TIME
        {
        int seconds;
        int minutes;
        int hours;
        };
        void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);

        int main()
        {
        struct TIME startTime, stopTime, diff;

        printf("Enter start time: \\n");
        printf("Enter hours, minutes and seconds respectively: ");
        scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
        
        printf("Enter stop time: \\n");
        printf("Enter hours, minutes and seconds respectively: ");
        scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
        
        // Calculate the difference between the start and stop time period.
        differenceBetweenTimePeriod(startTime, stopTime, &diff);
        
        printf("\\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
        printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
        printf("= %d:%d:%d\\n", diff.hours, diff.minutes, diff.seconds);
        
        return 0;
        

        }

        //not able to understand the logic.
        void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
        {
        //how does this logic works??.
        if(stop.seconds > start.seconds){
        --start.minutes;
        start.seconds += 60;
        }

        diff->seconds = start.seconds - stop.seconds;
        if(stop.minutes > start.minutes){
            --start.hours;
            start.minutes += 60;
        }
        
        diff->minutes = start.minutes - stop.minutes;
        diff->hours = start.hours - stop.hours;
        

        }

        Thank you

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        It solves the following equation for X. {stop time} + {X} = {start time} If you walk through examples by hand you will see that the code appears to solve that equation correctly. However like the other response I too find it counter intuitive. As noted in the other response the expected difference would be to subtract start from stop. This solution ends up with negative values. It also treats a time period on a fixed clock - one that does not cross midnight. Which is nonsensical in human terms.

        L T 2 Replies Last reply
        0
        • J jschell

          It solves the following equation for X. {stop time} + {X} = {start time} If you walk through examples by hand you will see that the code appears to solve that equation correctly. However like the other response I too find it counter intuitive. As noted in the other response the expected difference would be to subtract start from stop. This solution ends up with negative values. It also treats a time period on a fixed clock - one that does not cross midnight. Which is nonsensical in human terms.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          jschell wrote:

          {stop time} + {X} = {start time}

          Really? Only if X is negative; or the interval stops before it starts.

          J 1 Reply Last reply
          0
          • J jschell

            It solves the following equation for X. {stop time} + {X} = {start time} If you walk through examples by hand you will see that the code appears to solve that equation correctly. However like the other response I too find it counter intuitive. As noted in the other response the expected difference would be to subtract start from stop. This solution ends up with negative values. It also treats a time period on a fixed clock - one that does not cross midnight. Which is nonsensical in human terms.

            T Offline
            T Offline
            Tarun Jha
            wrote on last edited by
            #5

            yes i find that problem too....

            1 Reply Last reply
            0
            • L Lost User

              jschell wrote:

              {stop time} + {X} = {start time}

              Really? Only if X is negative; or the interval stops before it starts.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Richard MacCutchan wrote:

              Only if X is negative;

              It is negative.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups