如何计算系统时间间隔。

来源:互联网 发布:广东盈通网络联系电话 编辑:程序博客网 时间:2024/05/16 00:58

从系统中随便取出两个时间,要计算其时间间隔,方法有许多种,总结如下,要是有更好的方法,请大家发表评论啊。:)

1、当然是最基本的,用程序自己写,代码如下:

/*calculate time*/
#include "time.h"
#include "stdio.h"
#include "conio.h"
main()
{
  time_t start,end;
  int i;
  start=time(NULL);
  for(i=0;i<30000;i++)
    printf("/1/1/1/1/1/1/1/1/1/1/n");
  end=time(NULL);
  printf("/1: The different is %6.3f/n",difftime(end,start));
  getch();
}
struct Time
{
int year;
int month;
int day;
int hour;
int second;
};
int timeInterval(Time a,Time b);
int allday(Time a);


int _tmain(int argc, _TCHAR* argv[])
{
Time a,b;
a.year=2000;
a.month=3;
a.day=1;
a.hour=21;
a.second=18;
b.year=2000;
b.month=2;
b.day=28;
b.hour=20;
b.second=17;

int x=timeInterval(a,b);
printf("%d/n",x);
return 0;
}

int timeInterval(Time a,Time b)
{
    int x,y;
int sum;
x=allday(a);
    y=allday(b);
sum=(x-y)*24+a.hour-b.hour;
if(a.second<b.second)
{
sum=sum-1;
}else {
sum=sum;
}
return sum;
}

int allday(Time a)
{
int run[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int notrun[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int x;
x=(a.year)*365+a.year/4+a.year/400-a.year/100;
if((((a.year%4)==0)&&((a.year%4)!=100))||(a.year%400==0))
{
for(int i=0;i<a.month;i++)
{
if(i>0)
{
x=x+notrun[i-1];
}
}
}
else
{
for(int i=0;i<a.month;i++)
{
if(i>0)
{
x=x+notrun[i-1];
}
}
}
x=x+a.day;
return x;
}
这个是在网上copy下来的,没有经过测试,仅供参考。

这个方法最普通,也是最麻烦的一种,你要考虑很多的条件。

2、用MFC里面的函数。

#include <afx.h>

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPreInst, LPSTR pchCmdLine, int iCmdShow )
{
 CTime t1( 2005, 12, 31, 15, 32, 0 ); //
 CTime t2( 2006, 01, 01, 9, 48, 0 ); //
 CTimeSpan ts = t2 - t1;             // Subtract 2 CTimes

 int n_ssss = ts.GetTotalHours();
 return 0;

}

三句话就搞定啦。不过你要确认VC里面你用了MFC类库阿,如果没有,方法如下:

Project -> 设定 -〉 Microsoft Foundation Class 中选择使用共有DLL MFC。 哦了~

3、使用GetTickCount()函数,取的是从开机到现在的时刻。很简单,不多说了。

#include <stdio.h>
#include <windows.h>
int main()
{
 DWORD t;

 t = GetTickCount();

 printf("Windows 起動から %d日 %d時間 %d分 %d秒 経過",
  t/86400000,
  (t/3600000)%24,
  (t/60000)%60,
  (t/1000)%60
 );
 return 0;
}

4、使用QueryPerformanceCounter()函数。这个函数精确到毫秒啊~

#include <windows.h>
#include <stdio.h>
int main()
{
 int i;
 LARGE_INTEGER nFreq, nBefore, nAfter;
 DWORD dwTime;
 memset(&nFreq,   0x00, sizeof nFreq);
 memset(&nBefore, 0x00, sizeof nBefore);
 memset(&nAfter,  0x00, sizeof nAfter);
 dwTime = 0;

 QueryPerformanceFrequency(&nFreq);
 QueryPerformanceCounter(&nBefore); 
 for(i=0;i<5;i++){
 Sleep(3000);
 QueryPerformanceCounter(&nAfter);

 dwTime =(DWORD)((nAfter.QuadPart - nBefore.QuadPart) *1000/ nFreq.QuadPart);
 }
 printf("%d 毫秒/n", dwTime);
 
 return 0;
}

5、利用time(long a)函数。

这个函数取得的是从1970年到现在经过的时间,返回为秒数保存在变量a中。

计算时间间隔,取俩次时间,相减就可以了。这个函数是有期限的啊,当秒数超过了long型的范围,就不能用了。

6、如果你计算时间间隔是为了定期运行某些程序的话,可以用定时器Settimer()。

#include <windows.h>

class foo_class {
  static int counter;
public:
  static void  __stdcall timer_proc(HWND,unsigned int, unsigned int, unsigned long) {
    if (counter++ < 20)
      MessageBox(0,"Hello","MessageBox",0);
    else
      PostQuitMessage(0);
  }
};

int foo_class::counter=0;

WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int) {

int iTimerID = SetTimer(0, 0, 300, foo_class::timer_proc);
  MSG m;
  while (GetMessage(&m,0,0,0)) {
    TranslateMessage(&m);
    DispatchMessage(&m);

   }
  return 1;
}
这是一个定时器,每隔1秒弹出一个提示框。关于定时器的文章,在网上有很多,我就不想细说了。