Advertisement
brainuser5705

elasped time

Apr 25th, 2021
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None
  1. #include<stdio.h>
  2.  
  3. struct time{
  4.     int hours;
  5.     int minutes;
  6.     int seconds;
  7. };
  8.  
  9. struct time elapsed_time(struct time t1, struct time t2){
  10.  
  11.     int newHours = 0, newMinutes = 0, newSeconds = 0;
  12.  
  13.     if (t1.seconds > t2.seconds){
  14.         newSeconds += (60 - t1.seconds) + t2.seconds;
  15.         newMinutes--;
  16.     }else{
  17.         newSeconds += t2.seconds - t1.seconds;
  18.     }
  19.  
  20.     if (t1.minutes > t2.minutes){
  21.         newMinutes += (60 - t1.minutes) + t2.minutes;
  22.         newHours--;
  23.     }else{
  24.         newMinutes += t2.minutes- t1.minutes;
  25.     }
  26.  
  27.     if (t1.hours > t2.hours){
  28.         newHours += (23 - t1.hours) + t2.hours;
  29.         if (t2.hours == 0){ // special case for passing midnight
  30.             newHours++;
  31.         }
  32.     }else{
  33.         newHours += t2.hours- t1.hours;
  34.     }
  35.  
  36.     struct time newTime = {newHours, newMinutes, newSeconds};
  37.  
  38.     return newTime;
  39.  
  40. }
  41.  
  42. int main(void){
  43.  
  44.     struct time time1 = {2,14,31}, time2 = {0, 16, 31};
  45.     struct time newTime = elapsed_time(time1, time2);
  46.  
  47.     printf("The difference between the times are %.2i:%.2i.%.2i", newTime.hours, newTime.minutes, newTime.seconds);
  48.     return 0;
  49. }
Advertisement
RAW Paste Data Copied
Advertisement