user-0 提问: 在c语言中怎么实现计算两个时间的差值，精确到时分秒计算

回复:
user: user-1
content: 仅供参考#include <afxdisp.h> #include <stdio.h> #include <stdlib.h> int main(int argc, TCHAR* argv[]) {     COleDateTime t;     COleDateTimeSpan ts;     CString s,fmt;     int nYear;     int nMonth;     int nDay;     int nHour;     int nMin;     int nSec;     int lDays;     int nHours;     int nMins;     int nSecs;     int i,N;     // initialize MFC and print and error on failure     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {         printf("Fatal Error: MFC initialization failed\n");         return 1;     }     if (argc<13) {         printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);         return 2;     }     if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";     else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";     else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";     else {         printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);         return 3;     }     nYear =atoi(argv[ 1]);     nMonth=atoi(argv[ 2]);     nDay  =atoi(argv[ 3]);     nHour =atoi(argv[ 4]);     nMin  =atoi(argv[ 5]);     nSec  =atoi(argv[ 6]);     lDays =atoi(argv[ 7]);     nHours=atoi(argv[ 8]);     nMins =atoi(argv[ 9]);     nSecs =atoi(argv[10]);     N     =atoi(argv[11]);     if (N<=0) {         printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);         return 4;     }     t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);     ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );     for (i=1;i<=N;i++) {         s=t.Format(fmt);         printf("%08d %s\n",i,s);         t=t+ts;     }     return 0; }

user: user-2
content: 这个直接度娘吧。就搜“c语言 时间差”，答案一大堆

user: user-3
content: 引用 5 楼 yuqing89320 的回复:struct tm tm_1 = 初始化; struct tm tm_2 = 初始化; printf( "%s", ctime( mktime( &tm_1 ) - mktime( &tm_2 ) ) );  改为 #include <stdio.h> #include <time.h>  int main( void ) {     struct tm tm_1 = {0};     struct tm tm_2 = {0};     struct tm tm_3 = {0};      tm_1.tm_sec = 1;  tm_1.tm_min = 1;     tm_1.tm_hour = 1; tm_1.tm_mday = 1;     tm_1.tm_mon = 0;  tm_1.tm_year = 2014 - 1900;          tm_2.tm_sec = 2;  tm_2.tm_min = 2;     tm_2.tm_hour = 2; tm_2.tm_mday = 2;     tm_2.tm_mon = 1;  tm_2.tm_year = 2015 - 1900;      tm_3.tm_sec = 0;  tm_3.tm_min = 0;     tm_3.tm_hour = 0; tm_3.tm_mday = 0;     tm_3.tm_mon = 0;  tm_3.tm_year = -1900;      time_t tim_1 = mktime( &tm_1 );     time_t tim_2 = mktime( &tm_2 );     time_t tim_3 = mktime( &tm_3 );     printf("time1: %s\n", ctime( &tim_1 ) );     printf("time2: %s\n", ctime( &tim_2 ) );     time_t tim = tim_2 - tim_1 + tim_3;     printf("time: %s\n", ctime( &tim ) );      return 0; }

user: user-1
content: difftime Finds the difference between two times.  double difftime( time_t timer1, time_t timer0 );  Routine Required Header Compatibility  difftime <time.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  difftime returns the elapsed time in seconds, from timer0 to timer1. The value returned is a double-precision floating-point number.  Parameters  timer1  Ending time  timer0  Beginning time  Remarks  The difftime function computes the difference between the two supplied time values timer0 and timer1.  Example   /* DIFFTIME.C: This program calculates the amount of time  * needed to do a floating-point multiply 10 million times.  */  #include <stdio.h> #include <stdlib.h> #include <time.h>  void main( void ) {    time_t   start, finish;    long loop;    double   result, elapsed_time;     printf( "Multiplying 2 floating point numbers 10 million times...\n" );        time( &start );    for( loop = 0; loop < 10000000; loop++ )       result = 3.63 * 5.27;     time( &finish );     elapsed_time = difftime( finish, start );    printf( "\nProgram takes %6.0f seconds.\n", elapsed_time ); }   Output  Multiplying 2 floats 10 million times...  Program takes      2 seconds.   Floating-Point Support Routines |  Time Management Routines  See Also   time

user: user-4
content: 去搜一下defftime,就是精确到秒的

user: user-5
content: linux下使用gettimeofday轻松搞定

user: user-3
content: struct tm tm_1 = 初始化; struct tm tm_2 = 初始化; printf( "%s", ctime( mktime( &tm_1 ) - mktime( &tm_2 ) ) );

user: user-0
content: 没有，什么意思。。。。

user: user-6
content: 听说过“闰秒”么？