• <legend id='0p29a'><style id='0p29a'><dir id='0p29a'><q id='0p29a'></q></dir></style></legend>

      • <bdo id='0p29a'></bdo><ul id='0p29a'></ul>
      <i id='0p29a'><tr id='0p29a'><dt id='0p29a'><q id='0p29a'><span id='0p29a'><b id='0p29a'><form id='0p29a'><ins id='0p29a'></ins><ul id='0p29a'></ul><sub id='0p29a'></sub></form><legend id='0p29a'></legend><bdo id='0p29a'><pre id='0p29a'><center id='0p29a'></center></pre></bdo></b><th id='0p29a'></th></span></q></dt></tr></i><div id='0p29a'><tfoot id='0p29a'></tfoot><dl id='0p29a'><fieldset id='0p29a'></fieldset></dl></div>

        <small id='0p29a'></small><noframes id='0p29a'>

      1. <tfoot id='0p29a'></tfoot>

        使用 C++ 和 Boost 以毫秒为单位获取当前时间

        时间:2023-06-30

        <small id='i4xyb'></small><noframes id='i4xyb'>

        <legend id='i4xyb'><style id='i4xyb'><dir id='i4xyb'><q id='i4xyb'></q></dir></style></legend>
          <i id='i4xyb'><tr id='i4xyb'><dt id='i4xyb'><q id='i4xyb'><span id='i4xyb'><b id='i4xyb'><form id='i4xyb'><ins id='i4xyb'></ins><ul id='i4xyb'></ul><sub id='i4xyb'></sub></form><legend id='i4xyb'></legend><bdo id='i4xyb'><pre id='i4xyb'><center id='i4xyb'></center></pre></bdo></b><th id='i4xyb'></th></span></q></dt></tr></i><div id='i4xyb'><tfoot id='i4xyb'></tfoot><dl id='i4xyb'><fieldset id='i4xyb'></fieldset></dl></div>

          1. <tfoot id='i4xyb'></tfoot>
              <tbody id='i4xyb'></tbody>

                • <bdo id='i4xyb'></bdo><ul id='i4xyb'></ul>
                  本文介绍了使用 C++ 和 Boost 以毫秒为单位获取当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  In my thread (using boost::thread) I need to retrieve the current time in ms or less and to convert into ms:

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

                  Actually, reading here I've found this:

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

                  并且似乎有效,但是在我需要现在的毫秒值之后...

                  And seems to work, but after I need to have a long value of the milliseconds of the now...

                  我该怎么做?

                  推荐答案

                  您可以使用 boost::posix_time::time_duration 获取时间范围.例如像这样

                  You can use boost::posix_time::time_duration to get the time range. E.g like this

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

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

                  And to get a higher resolution you can change the clock you are using. For example to the boost::posix_time::microsec_clock, though this can be OS dependent. On Windows, for example, boost::posix_time::microsecond_clock has milisecond resolution, not microsecond.

                  一个有点依赖硬件的例子.

                  An example which is a little dependent on the hardware.

                  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,因为时钟的分辨率更高.我希望能有所帮助.

                  On my win7 machine. The first out is either 0 or 1000. Second resolution. The second one is nearly always 500, because of the higher resolution of the clock. I hope that help a little.

                  这篇关于使用 C++ 和 Boost 以毫秒为单位获取当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:std::chrono::high_resolution_clock 的用途是什么? 下一篇:C `clock()` 函数只返回一个零

                  相关文章

                • <i id='tdMPz'><tr id='tdMPz'><dt id='tdMPz'><q id='tdMPz'><span id='tdMPz'><b id='tdMPz'><form id='tdMPz'><ins id='tdMPz'></ins><ul id='tdMPz'></ul><sub id='tdMPz'></sub></form><legend id='tdMPz'></legend><bdo id='tdMPz'><pre id='tdMPz'><center id='tdMPz'></center></pre></bdo></b><th id='tdMPz'></th></span></q></dt></tr></i><div id='tdMPz'><tfoot id='tdMPz'></tfoot><dl id='tdMPz'><fieldset id='tdMPz'></fieldset></dl></div>
                    • <bdo id='tdMPz'></bdo><ul id='tdMPz'></ul>
                    <legend id='tdMPz'><style id='tdMPz'><dir id='tdMPz'><q id='tdMPz'></q></dir></style></legend>
                  1. <small id='tdMPz'></small><noframes id='tdMPz'>

                    1. <tfoot id='tdMPz'></tfoot>