将毫秒转换为指定格式的持续时间

问题描述 投票:0回答:1

在给定的203443 毫秒]持续时间内(这是3分钟,23秒和443毫秒),例如这样的模式

This took about' m 'minutes and' s 'seconds.

将产生以下格式化输出:

This took about 3 minutes and 23 seconds.

从格式时间戳到当前日期时间不同。是否有任何C ++标准库(在C ++ 14下)或我可以遵循的解决方案。我是C ++的新手。

对于给定的203443毫秒的持续时间(这是3分钟,23秒和443毫秒),例如这大约花费了“ m”分钟和“ s”秒。会产生以下...

c++ c++14 duration chrono
1个回答
1
投票
#include <chrono>
#include <iostream>

int
main()
{
    using namespace std::chrono;
    auto d = 203443ms;
    auto m = duration_cast<minutes>(d);
    d -= m;
    auto s = duration_cast<seconds>(d);
    std::cout << "This took about " << m.count() << " minutes and "
                                    << s.count() << " seconds.\n";
}
© www.soinside.com 2019 - 2024. All rights reserved.