如何在c中找到两次之间的差异?

时间:2022-03-09 16:46:46

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??

我的第一次是12:10:20 PM,第二次是7:10:20同一天我怎么能找到他们的差异?

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

我的想法是一直转换为秒,并找到差异再次转换为时间

is it good Approch r anything else??

Approch还不错吗?

3 个解决方案

#1


7  

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

不一定是最好的方法,但如果你想使用系统上的可用内容,difftime()和mktime()可以提供帮助 -

#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 );

#2


12  

You want the difftime function.

你想要difftime功能。

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:

如果你没有difftime可用,我会建议你从你所处的任何格式转换到epoch的秒数,进行计算并转换回你需要的任何格式。以下功能组可以帮助您完成所有这些转换:

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

asctime,ctime,gmtime,localtime,mktime,asctime_r,ctime_r,gmtime_r,localtime_r - 转换故障时间或ASCII的日期和时间

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

timegm,timelocal - 用于gmtime和localtime的反转(可能并非在所有系统上都可用)

#3


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.

再进一步,您可以转换为通用时间格式,例如Unix时间,然后采取差异。

#1


7  

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

不一定是最好的方法,但如果你想使用系统上的可用内容,difftime()和mktime()可以提供帮助 -

#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 );

#2


12  

You want the difftime function.

你想要difftime功能。

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:

如果你没有difftime可用,我会建议你从你所处的任何格式转换到epoch的秒数,进行计算并转换回你需要的任何格式。以下功能组可以帮助您完成所有这些转换:

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

asctime,ctime,gmtime,localtime,mktime,asctime_r,ctime_r,gmtime_r,localtime_r - 转换故障时间或ASCII的日期和时间

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

timegm,timelocal - 用于gmtime和localtime的反转(可能并非在所有系统上都可用)

#3


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.

再进一步,您可以转换为通用时间格式,例如Unix时间,然后采取差异。