当我两次用GetLocalTime获得了当前时间后,要怎样才能算出两个时间的间隔?
我这样做的目的是:既要知道当前的时间,又要计算两个时间的间隔。如果有替换方法也可以。

解决方案 »

  1.   

    直接相减,CTime重载了“-”运算符
      

  2.   

    例如
    CTime t1( 1999, 3, 19, 22, 15, 0 ); // 10:15PM March 19, 1999
    CTime t2( 1999, 3, 20, 22, 15, 0 ); // 10:15PM March 20, 1999
    CTimeSpan ts = t2 - t1;  // Subtract 2 CTimes
    ASSERT( ts.GetTotalSeconds() == 86400L );
    ASSERT( ( t1 + ts ) == t2 );  // Add a CTimeSpan to a CTime.
    ASSERT( ( t2 - ts ) == t1 );  // Subtract a CTimeSpan from a Ctime.
      

  3.   

    请参见下面的代码
    for( ; it3 != tempMessage1.end(),it4 != tempMessage2.end() ;it3++,it4++)
    {
    DWORD dwStart = GetTickCount();
    for(;;)
    {
       if(SMSSend.lock())
       {
    if(g_SmsSend)
    {
                            g_SmsSend = FALSE;
      
                            m_SendShotrMessage->SetstrTel( (*it3).c_str()  );       m_SendShotrMessage->SetstrMessage((*it4).c_str();       m_SendShotrMessage->resume( ); 
    ::PostThreadMessage(m_SendShotrMessage->getId(),WM_SENDCMGS,0,0);
    m_diverhasdo = true; 
    sendcouut ++;          break;
    }
    }
    else
    {
    break;
    }
    SMSSend.unlock();
    Sleep(100);

    if ( sendcouut == 1  || sendcouut == 0)
    {
    if( GetTickCount() - dwStart >= 6000) 
    {
                     m_diverhasdo = false;
       MessageBox(NULL,"请检验短信发送设备是否接通!确定设备接通后请重新启动AlarmService服务。","AlarmService提示信息", 0 | 0X00200000L | MB_TOPMOST | MB_ICONEXCLAMATION );
      goto loop;
    }
    }

    }
    代码的大致含义是:
    在你要计算的时间的开始部分,用DWORD dwStart = GetTickCount();记录下当时的时间。然后运行你的代码,在你想要计算时间间隔的地方,用if( GetTickCount() - dwStart >= 6000) 再次得到时间,并判断条件!
    祝你成功!!
      

  4.   

    或用
             COleDateTime  ts1(Eyear, Emonth, Eday, Ehour, Eminute, Esecond);
    COleDateTime  ts2(Byear, Bmonth, Bday, Bhour, Bminute, Bsecond);
    COleDateTime  ts3 = ts1 - ts2; second = ts3.GetSecond();
      

  5.   

    union {
    FILETIME ft;
    LONGLONG val;
    }t1,t2;SYSTEMTIME st1,st2;
    GetLocalTime(&st1);
    ...
    GetLocalTime(st2);SystemTimeToFileTime(&st1, &t1.ft);
    SystemTimeToFileTime(&st2, &t2.ft);LONGLONG secs=t1.val-t2.val;
      

  6.   

    在开始时:
    unsigned t =timeGetTime();结束时
           t=(timeGetTime()-t);这样,两次的时间差就保存在t中了
      

  7.   

    int time1=GetTickCount();//begin time
    //..............................
    //other code
    //..............................
    time1=GetTickCount()-time1;//end time
    //time1就是时间差
      

  8.   

    unsigned t =timeGetTime();
    t=(timeGetTime()-t);
      

  9.   


    DWORD g_dwLastPauseTime = 0; if((GetTickCount() - g_dwLastPauseTime)<800)
    {
    return;
    }
    else
    {
    g_dwLastPauseTime = GetTickCount();
    }
      

  10.   

    在开始时:
    unsigned t =timeGetTime();结束时
           t=(timeGetTime()-t);这样,两次的时间差就保存在t中了
      

  11.   

    COleDateTime dtBegin = COleDateTime::GetCurrentTime();
    //some code performing
    COleDateTime dtEnd = COleDateTime::GetCurrentTime();
    COleDateTimeSpan dtResult = dtEnd - dtBegin;
      

  12.   

    CTime t1( 2003, 3, 19, 22, 15, 0 ); // 10:15PM March 19, 2003
    CTime t2( 2003, 3, 20, 22, 15, 0 ); // 10:15PM March 20, 2003
    CTimeSpan ts = t2 - t1;  ts=1
      

  13.   

    我给你的方法不一定要在MFC中
      

  14.   

    我的方法不行吗?改一下最后一句
    LONGLONG secs=t2.val-t1.val;