2010-03-22 101 views
10

有人可以幫助我在Windows XP上使用Visual Studio C++ 2008的gettimeofday()函數嗎?這裏是我在網上找到的代碼:如何使用gettimeofday()或類似於Visual Studio C++ 2008的東西?

#include <time.h> 
#include <windows.h> 

#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) 
    #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 
#else 
    #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL 
#endif 

struct timezone 
{ 
    int tz_minuteswest; /* minutes W of Greenwich */ 
    int tz_dsttime;  /* type of dst correction */ 
}; 

int gettimeofday(struct timeval *tv, struct timezone *tz) 
{ 
    FILETIME ft; 
    unsigned __int64 tmpres = 0; 
    static int tzflag; 

    if (NULL != tv) 
    { 
    GetSystemTimeAsFileTime(&ft); 

    tmpres |= ft.dwHighDateTime; 
    tmpres <<= 32; 
    tmpres |= ft.dwLowDateTime; 

    /*converting file time to unix epoch*/ 
    tmpres -= DELTA_EPOCH_IN_MICROSECS; 
    tmpres /= 10; /*convert into microseconds*/ 
    tv->tv_sec = (long)(tmpres/1000000UL); 
    tv->tv_usec = (long)(tmpres % 1000000UL); 
    } 

    if (NULL != tz) 
    { 
    if (!tzflag) 
    { 
     _tzset(); 
     tzflag++; 
    } 
    tz->tz_minuteswest = _timezone/60; 
    tz->tz_dsttime = _daylight; 
    } 

    return 0; 
} 

... 
// call gettimeofday() 
gettimeofday(&tv, &tz); 
tm = localtime(&tv.tv_sec); 

去年當我用VC++ 6測試這個代碼時,它工作正常。但現在,當我使用VC++ 2008時,出現異常處理錯誤。那麼有沒有關於如何使用gettimeofday或類似的東西的想法?

感謝您的回覆和任何幫助將是非常讚賞:

+0

你能發表你得到的例外嗎? – Jason 2010-03-22 17:33:22

+0

請解釋您遇到的實際錯誤 - 「異常處理錯誤」不是很具體。 – 2010-03-22 17:33:25

+0

感謝您的回覆!我已經發布了一個關於這個錯誤的問題,它是:http://stackoverflow.com/questions/2490449/how-to-solve-unhandled-exception-error-when-using-visual-c-2008 再次感謝您的回覆 – make 2010-03-22 17:59:11

回答

2

有幾種不同的類型來表示時間。下面是一些代碼,我最近使用過的:

time_t now; 
tm* local; 
time(&now); 
local=localtime(&now); 

然後我又建立從local件一個字符串,但你可以做你想要在這一點上有什麼。

Kate

+0

謝謝很多爲您的答覆。是!我可以使用time()函數,但它不如gettimeofday()函數那樣精確,這使我成爲一個問題,因爲我試圖將我在Unix上獲得的結果與我在Windows上獲得的結果進行比較。再次感謝! – make 2010-03-22 19:04:10

18

在UNIX中,時區結構的使用已過時。我不知道你爲什麼使用它。請參閱http://linux.about.com/od/commands/l/blcmdl2_gettime.htm但是,如果您希望使用此結構來了解當地時間的GMT(UTC)差異,那麼將是:tz_minuteswest是GMT(UTC)的真實時差,tz_dsttime是一個標誌,指示是否現在是夏時制正在使用。

你有了一些變化示例工作在Visual C細++ 2008速成:

#include "stdafx.h" 
#include <time.h> 
#include <windows.h> 

const __int64 DELTA_EPOCH_IN_MICROSECS= 11644473600000000; 

/* IN UNIX the use of the timezone struct is obsolete; 
I don't know why you use it. See http://linux.about.com/od/commands/l/blcmdl2_gettime.htm 
But if you want to use this structure to know about GMT(UTC) diffrence from your local time 
it will be next: tz_minuteswest is the real diffrence in minutes from GMT(UTC) and a tz_dsttime is a flag 
indicates whether daylight is now in use 
*/ 
struct timezone2 
{ 
    __int32 tz_minuteswest; /* minutes W of Greenwich */ 
    bool tz_dsttime;  /* type of dst correction */ 
}; 

struct timeval2 { 
__int32 tv_sec;   /* seconds */ 
__int32 tv_usec;  /* microseconds */ 
}; 

int gettimeofday(struct timeval2 *tv/*in*/, struct timezone2 *tz/*in*/) 
{ 
    FILETIME ft; 
    __int64 tmpres = 0; 
    TIME_ZONE_INFORMATION tz_winapi; 
    int rez=0; 

    ZeroMemory(&ft,sizeof(ft)); 
    ZeroMemory(&tz_winapi,sizeof(tz_winapi)); 

    GetSystemTimeAsFileTime(&ft); 

    tmpres = ft.dwHighDateTime; 
    tmpres <<= 32; 
    tmpres |= ft.dwLowDateTime; 

    /*converting file time to unix epoch*/ 
    tmpres /= 10; /*convert into microseconds*/ 
    tmpres -= DELTA_EPOCH_IN_MICROSECS; 
    tv->tv_sec = (__int32)(tmpres*0.000001); 
    tv->tv_usec =(tmpres%1000000); 


    //_tzset(),don't work properly, so we use GetTimeZoneInformation 
    rez=GetTimeZoneInformation(&tz_winapi); 
    tz->tz_dsttime=(rez==2)?true:false; 
    tz->tz_minuteswest = tz_winapi.Bias + ((rez==2)?tz_winapi.DaylightBias:0); 

    return 0; 
} 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
struct timeval2 tv; 
struct timezone2 tz; 
struct tm *tm1; 
time_t time1; 

ZeroMemory(&tv,sizeof(tv)); 
ZeroMemory(&tz,sizeof(tz)); 

gettimeofday(&tv, &tz); // call gettimeofday() 
time1=tv.tv_sec; 
tm1 = localtime(&time1); 



FILE *f; 
f=fopen("rez.txt","w"); 

fprintf(f,"%04d.%02d.%02d %02d:%02d:%02d\n",1900+tm1->tm_year,1+tm1->tm_mon,tm1->tm_mday,tm1->tm_hour,tm1->tm_min,tm1->tm_sec); 
fprintf(f,"Diffrence between GMT(UTC) and local time=%d %s\n",tz.tz_minuteswest,"minutes"); 
fprintf(f,"Is Daylight now=%s\n",tz.tz_dsttime?"Yes":"No"); 

fclose(f); 
return 0; 
} 
+0

這幫助了我很多,但我需要更精確的時間。我使用了'GetSystemTimePreciseAsFileTime'來代替'GetSystemTimeAsFileTime',並且它工作得很完美。乾杯。 – 2016-06-24 18:31:11

3

以秒爲簡單的秒錶

FWIW:這是一個類似的,凱特的,但我只是想提及它,如果有人正在尋找最簡單的秒錶在C + +(計秒)。我知道,這不是什麼大不了的事。它只有1秒的分辨率,所以如果你想發送微秒,繼續與其他例子。

double seconds=0; 
time_t timer1, timer2; 
time(&timer1); /* get current time */ 
... 
time(&timer2); /* get current time later */ 
seconds = difftime(timer2,timer1); 
相關問題