Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,039 questions

52,004 answers

573 users

How to calculate the difference between two time periods in C

1 Answer

0 votes
#include <stdio.h> 

struct TIME
{
  int seconds;
  int minutes;
  int hours;
};

void TimeDifference(struct TIME t1, struct TIME t2, struct TIME *diff);

int main(void)
{   
    struct TIME startTime, endTime, diff;

    startTime.hours = 13; 
    startTime.minutes = 26;
    startTime.seconds = 17;
    
    endTime.hours = 15; 
    endTime.minutes = 12;
    endTime.seconds = 48;

    TimeDifference(startTime, endTime, &diff);

    printf("%d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
            
    return 0;
}

void TimeDifference(struct TIME startTM, struct TIME endTM, struct TIME *diff)
{
    if (endTM.seconds > startTM.seconds)
    {
        startTM.minutes--;
        startTM.seconds += 60;
    }

    if(endTM.minutes > startTM.minutes)
    {
        startTM.hours--;
        startTM.minutes += 60;
    }

    diff->seconds = startTM.seconds - endTM.seconds;
    diff->minutes = startTM.minutes - endTM.minutes;
    diff->hours = startTM.hours - endTM.hours;
}

  
/*
run:

-2:13:29

*/

 



answered Jun 3, 2017 by avibootz

Related questions

2 answers 385 views
1 answer 128 views
1 answer 121 views
1 answer 131 views
1 answer 134 views
...