使用C++和Boost获取当前时间的毫秒数

55

在我的线程中(使用boost::thread),我需要获取当前时间的毫秒级或更短的时间并将其转换为毫秒:

实际上,在这里阅读后,我发现了这个:

tick = boost::posix_time::second_clock::local_time();
now  = boost::posix_time::second_clock::local_time();

看起来似乎可以工作,但之后我需要得到当前时间的毫秒级长整数值...

我该怎么做呢?


1
你的意思是一个纪元时间戳吗? - Nim
9
但是之后我需要获取当前时间的毫秒数,并以长整型形式返回。 - Mooing Duck
4个回答

71

您可以使用boost::posix_time::time_duration获取时间范围。例如:

boost::posix_time::time_duration diff = tick - now;
diff.total_milliseconds();

要获得更高的分辨率,您可以更改所使用的时钟。例如,使用 boost::posix_time::microsec_clock,尽管这可能取决于操作系统。例如,在 Windows 上,boost::posix_time::microsecond_clock 具有毫秒分辨率而不是微秒。

这是一个有点依赖于硬件的示例。

int main(int argc, char* argv[])
{
    boost::posix_time::ptime t1 = boost::posix_time::second_clock::local_time();
    boost::this_thread::sleep(boost::posix_time::millisec(500));
    boost::posix_time::ptime t2 = boost::posix_time::second_clock::local_time();
    boost::posix_time::time_duration diff = t2 - t1;
    std::cout << diff.total_milliseconds() << std::endl;

    boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time();
    boost::this_thread::sleep(boost::posix_time::millisec(500));
    boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time();
    boost::posix_time::time_duration msdiff = mst2 - mst1;
    std::cout << msdiff.total_milliseconds() << std::endl;
    return 0;
}

在我的win7机器上。第一次输出要么是0,要么是1000。第二个分辨率。 第二个输出通常是500,因为时钟的精度更高。希望这有点帮助。


3
谢谢,但是如果我需要将当前时间转换为毫秒,应该怎么做? - ghiboz
1
我认为你可以使用一些“using”语句。 - Lightness Races in Orbit
2
@ghiboz: boost::chrono::high_resolution_clock - Mooing Duck

17

如果你的意思是自1970年1月1日0时0分0秒(UTC/GMT)到现在的毫秒数,那么你可以这样做:

ptime time_t_epoch(date(1970,1,1)); 
ptime now = microsec_clock::local_time();
time_duration diff = now - time_t_epoch;
x = diff.total_milliseconds();

然而,并不是非常清楚你需要什么。

请看DateTime文档中的示例,网址为Boost Date Time


7
+1 还要注意,这里的日期类型是 boost::gregorian::date。 - ntg

9
// Get current date/time in milliseconds.
#include "boost/date_time/posix_time/posix_time.hpp"
namespace pt = boost::posix_time;

int main()
{
     pt::ptime current_date_microseconds = pt::microsec_clock::local_time();

    long milliseconds = current_date_microseconds.time_of_day().total_milliseconds();

    pt::time_duration current_time_milliseconds = pt::milliseconds(milliseconds);

    pt::ptime current_date_milliseconds(current_date_microseconds.date(), 
                                        current_time_milliseconds);

    std::cout << "Microseconds: " << current_date_microseconds 
              << " Milliseconds: " << current_date_milliseconds << std::endl;

    // Microseconds: 2013-Jul-12 13:37:51.699548 Milliseconds: 2013-Jul-12 13:37:51.699000
}

-9
尝试这个:按照提到的方式导入头文件...只提供秒和毫秒。如果您需要解释代码,请阅读此链接
#include <windows.h>

#include <stdio.h>

void main()
{

    SYSTEMTIME st;
    SYSTEMTIME lt;

    GetSystemTime(&st);
   // GetLocalTime(&lt);

     printf("The system time is: %02d:%03d\n", st.wSecond, st.wMilliseconds);
   //  printf("The local time is: %02d:%03d\n", lt.wSecond, lt.wMilliseconds);

}

6
这是一个基于 Windows 的解决方案。最好提供跨平台的解决方案。 - Moataz Elmasry
7
这与增强无关。 - Qix - MONICA WAS MISTREATED

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接