Advertisement
dmilicev

difference_between_two_time_periods_v1.c

Sep 7th, 2020
191
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     difference_between_two_time_periods_v1.c
  4.  
  5.     https://www.programiz.com/c-programming/examples/time-structure
  6.  
  7.  
  8.     You can find all my C programs at Dragan Milicev's pastebin:
  9.  
  10.     https://pastebin.com/u/dmilicev
  11.  
  12. */
  13.  
  14. #include <stdio.h>
  15.  
  16. struct TIME
  17. {
  18.    int hours;
  19.    int minutes;
  20.    int seconds;
  21. };
  22.  
  23. // Computes difference between time periods
  24. void differenceBetweenTimePeriod(struct TIME start,
  25.                                  struct TIME stop,
  26.                                  struct TIME *diff )
  27. {
  28.     while (stop.seconds > start.seconds)
  29.     {
  30.       --start.minutes;
  31.       start.seconds += 60;
  32.     }
  33.  
  34.     diff->seconds = start.seconds - stop.seconds;
  35.  
  36.     while (stop.minutes > start.minutes)
  37.     {
  38.       --start.hours;
  39.       start.minutes += 60;
  40.     }
  41.  
  42.     diff->minutes = start.minutes - stop.minutes;
  43.     diff->hours = start.hours - stop.hours;
  44. }
  45.  
  46.  
  47. int main(void)
  48. {
  49.     struct TIME startTime, stopTime, diff;
  50.  
  51. /*
  52.     printf("\n Enter the start time. \n");
  53.     printf("Enter hours, minutes and seconds: ");
  54.     scanf("%d %d %d", &startTime.hours,
  55.          &startTime.minutes,
  56.          &startTime.seconds);
  57.  
  58.     printf("\n Enter the stop time. \n");
  59.     printf("Enter hours, minutes and seconds: ");
  60.     scanf("%d %d %d", &stopTime.hours,
  61.          &stopTime.minutes,
  62.          &stopTime.seconds);
  63. */
  64.  
  65.     startTime.hours   = 12;
  66.     startTime.minutes = 34;
  67.     startTime.seconds = 55;
  68.  
  69.     stopTime.hours   =  8;
  70.     stopTime.minutes = 12;
  71.     stopTime.seconds = 15;
  72.  
  73.     // Difference between start and stop time
  74.     differenceBetweenTimePeriod(startTime, stopTime, &diff);
  75.  
  76.     printf("\nTime Difference: %d:%d:%d - ",
  77.             startTime.hours,
  78.             startTime.minutes,
  79.             startTime.seconds);
  80.  
  81.     printf("%d:%d:%d ",
  82.             stopTime.hours,
  83.             stopTime.minutes,
  84.             stopTime.seconds);
  85.  
  86.     printf("= %d:%d:%d \n",
  87.             diff.hours,
  88.             diff.minutes,
  89.             diff.seconds);
  90.  
  91.  
  92.     return 0;
  93.  
  94. } // main()
  95.  
Advertisement
RAW Paste Data Copied
Advertisement