- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 boost::posix_time::ptime
来测量我的模拟运行时间和其他东西。
假设
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;
现在
std::cout << to_simple_string( diff ) << std::endl;
以 hh:mm:ss.ssssss
格式返回时间,我也希望以 ss.sssssss
格式返回时间。
为此,我尝试过
boost::posix_time::time_duration::sec_type x = diff.total_seconds();
但这给了我 ss 格式的答案,seconds()
返回返回标准化秒数 (0..60)。
我的问题是如何获得 ss.ssssss 格式的模拟时间(以秒为单位)?
编辑
我能够做到:
std::cout << diff.total_seconds() << "." << diff.fractional_seconds() << std::endl;
有什么优雅的东西可以绘制 ss.sssssss 吗?
最佳答案
total_seconds()
返回一个 long
值,该值未标准化为 0..60s。
所以就这样做:
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
}
关于c++ - 以秒为单位获取 boost::posix_time::time_duration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9194226/
我想计算 boost::posix_time:ptime 中两个时间戳之间的时间差(以秒为单位)。但是,由于 timestamp 最多包含微秒,结果可能会出乎我的意料。 boost::posix_ti
我正在使用 boost::posix_time 来实现时钟。 date d(2000,Jan,20); ptime start(d); time_iterator titr(start,microse
我有以下代码从 posix_time 获取 UNIX 时间 boost::posix_time::ptime time1(boost::gregorian::date(9999,12,31)); bo
我想转换一个 time_duration到 DATE格式,即自1899年以来的天数、12、30。 DATE date_from_duration(time_duration td) { doub
我想通过网络传输 boost::posix_time::ptime 作为 boost::int64_t。根据A way to turn boost::posix_time::ptime into an
我正在使用 boost::posix_time::ptime 来测量我的模拟运行时间和其他东西。 假设 boost::posix_time::ptime start, stop; boost::pos
可以通过 total_microseconds 方法从 time_duration 中获取总微秒数,但我不知道如何根据该数字重新构建 time_duration。文档中似乎没有用于此目的的构造函数,我
考虑这段代码(使用 VS2015 编译,使用 boost 1.60: #include #include #include int main( int argc, char* argv[] )
我知道我可以使用 boost::gregorian::date_duration 作为以天为单位的时间间隔,使用 boost::posix_time::time_duration 作为时间间隔,但是有
您好,我正在使用 boost Posix 时间系统。我有课 class event{ private: boost::posix_time::ptime time; //some other stuu
我正在尝试为 boost::posix_time::duration 编写一个简单的生成器以在另一个生成器中使用。现在对我来说关键是我想打印的“+”或“-”。到目前为止我所拥有的是: struct G
我正在尝试将 boost::posix_time::time_duration 对象转换为 "%H%M"形式的 string。问题是对话是不工作就好像它不存在一样。 我用来将 Duration 转换为
下面这段代码: auto td = boost::posix_time::seconds(1); auto seconds = td.seconds(); // (*) std::cout << se
我是一名优秀的程序员,十分优秀!