4

my first time is 12:10:20 PM and second time is 7:10:20 Am of the same day how can i find diff b/w them??

My idea is convert all the time to seconds and find the difference again convert to time

is it good Approch r anything else??

Cute
  • 13,643
  • 36
  • 96
  • 112
  • 1
    Can you post code that shows how you are storing the two time values 12:10:20 PM and 7:10:20 AM? For example, are they of the same day? Different days? It's not clear if they are values that can be readily converted using common functions in . If they are convertable, you can calculate the difference in epoch time, and then convert that back to a more readable time. – Alex Reynolds May 15 '09 at 05:57

3 Answers3

12

You want the difftime function.

Edit

If you don't have difftime available I would suggest just converting from whatever format you're in to seconds from the epoch, do your calculations and covert back to whatever format you need. The following group of functions can help you with all those conversions:

asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, localtime_r - transform date and time to broken-down time or ASCII

timegm, timelocal - inverses for gmtime and localtime ( may not be available on all systems )

Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
  • Would you happen to know how widely supported C99 is? I know it's still not completely supported gcc according to spec yet, but I would think that 10 years later it would be mostly supported in most systems ( wishful thinking on my part? ) – Robert S. Barnes May 15 '09 at 08:47
  • `difftime()` and `mktime()` were part of C89. The `*_r()` functions are more recent but are POSIX standard. The `timegm()` and `timelocal()` functions are much more restricted (BSD, Linux?). – Jonathan Leffler Jun 08 '13 at 01:14
7

Not necessarily the best way, but if you wish to use what's available on the system, difftime() and mktime() can help -

#include <time.h>

tm Time1 = { 0 };  // Make sure everything is initialized to start with.
/* 12:10:20 */
Time1.tm_hour = 12;
Time1.tm_min = 10;
Time1.tm_sec = 20;

/* Give the function a sane date to work with (01/01/2000, here). */
Time1.tm_mday = 1;
Time1.tm_mon = 0;
Time1.tm_year = 100;

tm Time2 = Time1;  // Base Time2 on Time1, to get the same date...
/* 07:10:20 */
Time2.tm_hour = 7;
Time2.tm_min = 10;
Time2.tm_sec = 20;

/* Convert to time_t. */
time_t TimeT1 = mktime( &Time1 );
time_t TimeT2 = mktime( &Time2 );

/* Use difftime() to find the difference, in seconds. */
double Diff = difftime( TimeT1, TimeT2 );
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Hexagon
  • 6,845
  • 2
  • 24
  • 16
  • @jons34yp: you converted an answer which was bilingual in C and C++ and therefore sensible as an answer to a question tagged as C and C++ into one that was no longer a valid answer in C. If you must edit the C++11 version of the code into the answer (not necessarily a bad thing to do, but you could always answer the question yourself rather than editing another's answer), do it leaving the bilingual code untouched (copy, and edit, not just edit). – Jonathan Leffler Jun 07 '13 at 15:29
  • @JonathanLeffler I presumed an user tags a question [c] in addition to [c++] only because a C solution can always be used in C++. –  Jun 07 '13 at 15:33
0

Your approach sounds sensible.

Taking it one stage further, you could conver to a universal time format, such as Unix time, and then take the difference.

Steve Melnikoff
  • 2,620
  • 1
  • 22
  • 24