timeb.h和time.h
這兩個是C++的標準頭文件,在Microsoft Visual Studio/VC98/Include中,表示了C++對時間的支持能力。
time.h中申明瞭tm結構體,來對時間年月日、時分秒的支持,值得注意的是,它不支持毫秒級的計時方式。
可以用函數time(NULL)函數獲取當前時間戳。
timeb.h是對time.h的補充,申明瞭timeb結構體,用來支持毫秒級的計時方式。
可以用ftime(timeb&)獲取當前時間戳。
兩個頭文件的都定義了typedef long time_t;
time_t和tm可以相互轉換,然而C++並沒有提供從time_t到tm的轉換,可以調用ACE的轉換函數ACE_OS::localtime_r (const time_t *t, struct tm *res)來實現。
timeb.time實際上就是time_t;timeb.millitm表示毫秒。
timeb.h獲取毫秒的代碼
#include <iostream>
#include <sys/timeb.h>
using namespace std;
int main()
{
timeb t;
ftime(&t);//獲取毫秒
cout << t.time << " " << t.millitm << endl;//秒級時間戳
cout << t.time * 1000 + t.millitm << endl;//某個時間戳下走過的毫秒數,可用來求時間複雜度
cout << t.time * 1000 + t.millitm << endl;//13位毫秒的時間戳
}
timeb是一個結構體,下面是源碼
struct timeb
{
time_t time;
unsigned short millitm;
short timezone;
short dstflag;
};