2009-07-13 144 views
11

下面的代碼時用來打印在日誌的時間:捕捉毫秒

#define PRINTTIME() struct tm * tmptime; 
time_t  tmpGetTime; 
time(&tmpGetTime); 
tmptime = localtime(&tmpGetTime); 
cout << tmptime->tm_mday << "/" <<tmptime->tm_mon+1 << "/" << 1900+tmptime->tm_year << " " << tmptime->tm_hour << ":" << tmptime->tm_min << ":" << tmptime->tm_sec<<">>"; 

有什麼辦法來毫秒添加到這個?

+4

可能重複[如何獲得當前TI以毫秒爲單位mestamp自1970年以來就是這個樣子的Java得到(http://stackoverflow.com/questions/19555121/how-to-get-current-timestamp-in-milliseconds-since-1970-just-the-way-java-得到) – 2015-09-30 09:06:47

回答

17

要獲得毫秒精度,您必須使用特定於您的操作系統的系統調用。

在Linux中,你可以使用

#include <sys/time.h> 

timeval tv; 
gettimeofday(&tv, 0); 
// then convert struct tv to your needed ms precision 

timeval具有微秒級精度。

在Windows中,你可以使用:

#include <Windows.h> 

SYSTEMTIME st; 
GetSystemTime(&st); 
// then convert st to your precision needs 

當然你也可以使用Boost來爲你做的:)

4

您需要一個具有更高分辨率的計時器才能捕獲毫秒。試試這個:

int cloc = clock(); 
//do something that takes a few milliseconds 
cout << (clock() - cloc) << endl; 

這當然依賴於你的操作系統。

+0

坎我認爲時鐘()返回其代表一個時鐘單元clock_t表示。而且在Linux上沒有實現毫秒精度AFAIK – neuro 2009-07-13 16:37:38

+0

@neuro在我的Linux程序,我收到雙值時鐘() – user3085931 2015-03-17 21:26:13

2

高分辨率計時器通常在Linux風格的平臺和Windows上的QueryPerformanceCounter上進行。

您應該知道,對單個操作的持續時間進行計時(即使使用高分辨率計時器)也不會產生準確的結果。遊戲中有太多隨機因素。爲了獲得可靠的時間信息,您應該運行任務以循環定時並計算平均任務時間。對於這種類型的時序,clock()函數應該足夠了。

2

如果你不希望使用任何特定的操作系統的代碼,你可以使用爲大多數標準操作系統提供ACE_OS::gettimeofday功能的ACE軟件包。 例如:

ACE_Time_Value startTime = ACE_OS::gettimeofday(); 

do_something(); 

ACE_Time_Value endTime = ACE_OS::gettimeofday(); 

cout << "Elapsed time: " << (endTime.sec() - startTime.sec()) << " seconds and " << double(endTime.usec() - startTime.usec())/1000 << " milliseconds." << endl; 

無論您的操作系統的這段代碼就可以了(只要ACE支持該操作系統)。

14

// C++ 11風格:

cout << "Time in Milliseconds =" << 
chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count() 
<< std::endl; 

cout << "Time in MicroSeconds=" << 
chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now().time_since_epoch()).count() 
<< std::endl; 
1

在Ubuntu的16.04這個工作對我來說...

const std::string currentDateTime() { 
    char   fmt[64], buf[64]; 
    struct timeval tv; 
    struct tm  *tm; 

    gettimeofday(&tv, NULL); 
    tm = localtime(&tv.tv_sec); 
    strftime(fmt, sizeof fmt, "%Y-%m-%d %H:%M:%S.%%06u", tm); 
    snprintf(buf, sizeof buf, fmt, tv.tv_usec); 
    return buf; 
} 

然後,...

std::cout << currentDateTime(); 

我得到...

2016-12-29 11:09:55.331008 
1

使用C++ 11或C++ 14老問題的新答案,這free, open-source library

#include "tz.h" 
#include <iostream> 

int 
main() 
{ 
    using namespace date; 
    using namespace std; 
    using namespace std::chrono; 
    auto now = make_zoned(current_zone(), floor<milliseconds>(system_clock::now())); 
    cout << format("%e/%m/%Y %T", now) << '\n'; 
} 

對我來說這只是輸出:

16/01/2017 15:34:32.167 

這是我現在的本地日期和時間以毫秒爲準。通過消除floor<milliseconds>()您將自動獲得您的system_clock有任何精度。

如果你想要的結果作爲一個UTC時間戳,而不是一個本地時間戳,它是更容易:

auto now = floor<milliseconds>(system_clock::now()); 
    cout << format("%e/%m/%Y %T", now) << '\n'; 

如果你想有一個UTC時間戳,你不挑剔的精度或格式,你可以:

cout << system_clock::now() << '\n'; 

這只是我的輸出:

2017-01-16 20:42:11.267245