我的第一次是12:10:20 PM,第二次是7:10:20同一天我怎么能找到他们的差异?
我的想法是将所有时间转换为秒,并找到差异再次转换为时间
Approch还不错吗?
Rob*_*nes 12
你想要这个difftime功能.
编辑
如果你没有difftime可用我会建议你从你所处的任何格式转换到纪元的秒数,做你的计算并转换回你需要的任何格式.以下功能组可以帮助您完成所有这些转换:
asctime,ctime,gmtime,localtime,mktime,asctime_r,ctime_r,gmtime_r,localtime_r - 转换故障时间或ASCII的日期和时间
timegm,timelocal - 用于gmtime和localtime的反转(可能并非在所有系统上都可用)
不一定是最好的方法,但如果你想使用系统上的可用内容,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 );
Run Code Online (Sandbox Code Playgroud)