在c语言中怎么实现计算两个时间的差值,精确到时分秒计算

xgsdatmlkp 2014-07-06 09:45:10
求大神。新手,最好有代码。。。。。
...全文
2951 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-07-07
  • 打赏
  • 举报
回复
仅供参考
#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;
}
starytx 2014-07-07
  • 打赏
  • 举报
回复
这个直接度娘吧。就搜“c语言 时间差”,答案一大堆
yuqing89320 2014-07-07
  • 打赏
  • 举报
回复
引用 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;
}

赵4老师 2014-07-07
  • 打赏
  • 举报
回复
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
难题 2014-07-07
  • 打赏
  • 举报
回复
去搜一下defftime,就是精确到秒的
lucky-lucky 2014-07-07
  • 打赏
  • 举报
回复
linux下使用gettimeofday轻松搞定
yuqing89320 2014-07-07
  • 打赏
  • 举报
回复
struct tm tm_1 = 初始化; struct tm tm_2 = 初始化; printf( "%s", ctime( mktime( &tm_1 ) - mktime( &tm_2 ) ) );
xgsdatmlkp 2014-07-06
  • 打赏
  • 举报
回复
没有,什么意思。。。。
taodm 2014-07-06
  • 打赏
  • 举报
回复
听说过“闰秒”么?

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧