Advertisement
dmilicev

difference_of_two_times_in_minutes_v1.c

May 25th, 2020
153
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     difference_of_two_times_in_minutes_v1.c
  4.  
  5.     Task from Choo Yong Han :
  6.     https://web.facebook.com/albertchooyonghan
  7.     https://web.facebook.com/photo.php?fbid=3902250669845726&set=pcb.1588682431290601&type=3&theater&ifg=1
  8.  
  9.     1030 - 0830 = 120 minutes
  10.  
  11. I see that you succeed to read data (numbers) to matrix,
  12. but difference of time is wrong.
  13.  
  14. Example:
  15.  
  16. 1030 - 0830
  17.  
  18. You have to extract first two digits, make number of them,
  19. multiply that number (represent hours) by 60 minutes and
  20. add to it minutes made of third and fourth digit.
  21.  
  22. 10*60+30 - 8*60+30 = 120 minutes
  23.  
  24. Then convert 120 minutes to hours and minutes.
  25.  
  26. 120 = 120/60 + 120%60
  27.  
  28. First / is integer division,
  29. second is rest (reminder) of division.
  30.  
  31. So,
  32.  
  33. 120 minutes is 2 hours and 0 minutes.
  34.  
  35. At last, if needed, convert that to
  36. 0200.
  37.  
  38. This will work if both times are from same day.
  39.  
  40.  
  41. Here is a better solution using structures with integers:
  42. https://www.includehelp.com/code-snippets/print-difference-of-two-times-in-hours-minutes-and-seconds-in-c-language.aspx
  43. https://www.programiz.com/c-programming/examples/time-structure
  44.  
  45.  
  46.     You can find all my C programs at Dragan Milicev's pastebin:
  47.  
  48.     https://pastebin.com/u/dmilicev
  49.  
  50. */
  51.  
  52. #include <stdio.h>
  53. #include <stdlib.h>     // for atoi()
  54.  
  55.  
  56. // Returns difference end_time - start_time in minutes
  57. int difference_of_two_times_in_minutes( char start_time[], char end_time[] )
  58. {
  59.     char str_h_end_time[3], str_m_end_time[3], str_h_start_time[3], str_m_start_time[3];    // time strings
  60.     int time2_hours, time2_minutes, time1_hours, time1_minutes;     // time integers
  61.     int difference_in_minutes;
  62.  
  63.     str_h_end_time[0] = end_time[0];
  64.     str_h_end_time[1] = end_time[1];
  65.     str_h_end_time[2] = '\0';               // finish the string
  66.     time2_hours = atoi(str_h_end_time);
  67.  
  68.     str_m_end_time[0] = end_time[2];
  69.     str_m_end_time[1] = end_time[3];
  70.     str_m_end_time[2] = '\0';               // finish the string
  71.     time2_minutes = atoi(str_m_end_time);
  72.  
  73.     str_h_start_time[0] = start_time[0];
  74.     str_h_start_time[1] = start_time[1];
  75.     str_h_start_time[2] = '\0';             // finish the string
  76.     time1_hours = atoi(str_h_start_time);
  77.  
  78.     str_m_start_time[0] = start_time[2];
  79.     str_m_start_time[1] = start_time[3];
  80.     str_m_start_time[2] = '\0';             // finish the string
  81.     time1_minutes = atoi(str_m_start_time);
  82.  
  83.     difference_in_minutes = (time2_hours - time1_hours)*60 + (time2_minutes - time1_minutes);
  84.     return(difference_in_minutes);
  85. }
  86.  
  87.  
  88. int main(void)
  89. {
  90.     char end_time[]   = "1030";
  91.     char start_time[] = "0830";
  92.     int difference_in_minutes, h, m;
  93.  
  94.     difference_in_minutes = difference_of_two_times_in_minutes(start_time, end_time);
  95.  
  96.     printf("\n The difference %s - %s in minutes is %d minutes. \n",
  97.             end_time, start_time, difference_in_minutes);
  98.  
  99.     h = difference_in_minutes / 60;
  100.     m = difference_in_minutes % 60;
  101.  
  102.     printf("\n %d minutes is %d hours and %d minutes. \n", difference_in_minutes, h, m);
  103.  
  104.     printf("\n %d minutes is ", difference_in_minutes);
  105.  
  106.     if( h < 10 )
  107.         printf("0%d:", h);
  108.     else
  109.         printf("%2d:", h);
  110.  
  111.     if( m < 10 )
  112.         printf("0%d h \n", m);
  113.     else
  114.         printf("%2d h \n", m);
  115.  
  116.  
  117.     return 0;
  118.  
  119. } // main()
Advertisement
RAW Paste Data Copied
Advertisement