C++ 以秒为单位获取 boost::posix_time::time_duration
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9194226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get boost::posix_time::time_duration in seconds
提问by Eagle
I am using boost::posix_time::ptime
to measure my simulation run-time and for something else.
我boost::posix_time::ptime
用来测量我的模拟运行时间和其他东西。
assuimg
假设
boost::posix_time::ptime start, stop;
boost::posix_time::time_duration diff;
start = boost::posix_time::microsec_clock::local_time();
sleep(5);
stop = boost::posix_time::microsec_clock::local_time();
diff = stop - stop;
now
现在
std::cout << to_simple_string( diff ) << std::endl;
return the time in hh:mm:ss.ssssss
format and i would like to have the time as well in ss.sssssss
.
以hh:mm:ss.ssssss
格式返回时间,我也希望在ss.sssssss
.
for doing this, i tried
为此,我试过
boost::posix_time::time_duration::sec_type x = diff.total_seconds();
but that gave me the answer in format of ss and seconds()
returns Returns normalized number of seconds (0..60).
但这给了我 ss 格式的答案并seconds()
返回返回标准化秒数(0..60)。
My question how could i get my simulation time in seconds of the format ss.ssssss?
我的问题是如何获得格式为 ss.ssssss 的模拟时间(以秒为单位)?
EDIT
编辑
i was able to do:
我能够做到:
std::cout << diff.total_seconds() << "." << diff.fractional_seconds() << std::endl;
is there something elegant that could plot ss.sssssss?
有什么优雅的东西可以绘制 ss.sssssss 吗?
回答by nabulke
total_seconds()
returns a long
value which is not normalized to 0..60s.
total_seconds()
返回一个long
未标准化为 0..60s 的值。
So just do this:
所以只需这样做:
namespace bpt = boost::posix_time;
int main(int , char** )
{
bpt::ptime start, stop;
start = bpt::microsec_clock::local_time();
sleep(62);
stop = bpt::microsec_clock::local_time();
bpt::time_duration dur = stop - start;
long milliseconds = dur.total_milliseconds();
std::cout << milliseconds << std::endl; // 62000
// format output with boost::format
boost::format output("%.2f");
output % (milliseconds/1000.0);
std::cout << output << std::endl; // 62.00
}
回答by Piotr99
The most straight-forward way I see is something like this output, the rest of the time computations along the lines of nabulke's post:
我看到的最直接的方式是这样的输出,其余的时间计算沿着 nabulke 的帖子:
#include <iomanip>
double dseconds = dur.total_milliseconds() / 1000. ;
std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(3);
std::cout << dseconds << std::endl;
You want to express time in terms of a floating point number, so it's probably best to actually use one and apply the standard stream formatting manipulators.
您想用浮点数表示时间,因此最好实际使用浮点数并应用标准流格式操作符。
回答by jschober
// whatever time you have (here 1second)
boost::posix_time::ptime pt = boost::posix_time::from_time_t( 1 );
// subtract 0 == cast to duration
boost::posix_time::time_duration dur = pt - boost::posix_time::from_time_t(0);
// result in ms
uint64_t ms = dur.total_milliseconds();
// result in usec
uint64_t us = dur.total_microseconds();
// result in sec
uint64_t s = dur.total_seconds();
std::cout << "s = " << s << ", ms = " << ms << ", us = " << us << std::endl;
s = 1, ms = 1000, us = 1000000
s = 1, ms = 1000, us = 1000000