【问题标题】:Get current time in milliseconds using C++ and Boost使用 C++ 和 Boost 获取当前时间(以毫秒为单位)
【发布时间】:2011-10-07 17:48:40
【问题描述】:

在我的线程中(使用 boost::thread)我需要以毫秒或更短的时间检索当前时间并转换为毫秒:

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

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

而且似乎可以工作,但是在我需要一个很长的毫秒值之后……

我该怎么做?

【问题讨论】:

  • 你的意思是纪元时间戳吗?
  • "但是之后我需要有一个 long 值的现在的毫秒数。" ....什么?

标签: c++ boost time timestamp milliseconds


【解决方案1】:

您可以使用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,因为时钟的分辨率更高。希望能帮上一点忙。

【讨论】:

【解决方案2】:
// 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
}

【讨论】:

    【解决方案3】:

    试试这个:前面提到的 import headers.. 只给出秒和毫秒。如果你需要解释代码阅读this link

    #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);
    
    }
    

    【讨论】:

    • 这是一个基于windows的解决方案。最好提供一个跨平台的解决方案
    • 这与boost无关。
    【解决方案4】:

    如果你的意思是毫秒自纪元你可以这样做

    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();
    

    但是,你的目标不是特别清楚。

    查看 Boost Date Time 的 DateTime 文档中的示例

    【讨论】:

    • +1 另外,请注意这里的日期类型为 boost::gregorian::date
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 2017-02-20
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多