如何在 C++ 中将 chrono::seconds 转换为 HH:MM:SS 格式的字符串?

why*_*hy2 4 c++ c++-chrono c++17

我有一个函数,它接受第二个作为参数并以 HH:MM:SS 格式返回一个字符串。如果没有 std::chrono,我可以这样实现:

string myclass::ElapsedTime(long secs) {
  uint32_t hh = secs / 3600;
  uint32_t mm = (secs % 3600) / 60;
  uint32_t ss = (secs % 3600) % 60;
  char timestring[9];
  sprintf(timestring, "%02d:%02d:%02d", hh,mm,ss);
  return string(timestring);
}
Run Code Online (Sandbox Code Playgroud)

使用std::chrono,我可以将参数转换为std::chrono::seconds sec {seconds};.

但是如何将其转换为具有格式的字符串?我在https://youtu.be/P32hvk8b13M 中看到了 Howard Hinnant 的精彩视频教程。不幸的是,没有这种情况的例子。

How*_*ant 8

使用Howard Hinnant 的 header-only date.h 库,它看起来像这样:

#include "date/date.h"
#include <string>

std::string
ElapsedTime(std::chrono::seconds secs)
{
    return date::format("%T", secs);
}
Run Code Online (Sandbox Code Playgroud)

如果你想自己写,那么它看起来更像是:

#include <chrono>
#include <string>

std::string
ElapsedTime(std::chrono::seconds secs)
{
    using namespace std;
    using namespace std::chrono;
    bool neg = secs < 0s;
    if (neg)
        secs = -secs;
    auto h = duration_cast<hours>(secs);
    secs -= h;
    auto m = duration_cast<minutes>(secs);
    secs -= m;
    std::string result;
    if (neg)
        result.push_back('-');
    if (h < 10h)
        result.push_back('0');
    result += to_string(h/1h);
    result += ':';
    if (m < 10min)
        result.push_back('0');
    result += to_string(m/1min);
    result += ':';
    if (secs < 10s)
        result.push_back('0');
    result += to_string(secs/1s);
    return result;
}
Run Code Online (Sandbox Code Playgroud)

在 C++20 中,您将能够说:

std::string
ElapsedTime(std::chrono::seconds secs)
{
    return std::format("{:%T}", secs);
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*low 7

一旦 C++20 实现落地,您将能够执行以下操作(未经测试的代码):

std::chrono::hh_mm_ss<std::chrono::seconds> tod{std::chrono::seconds(secs)};
std::cout << tod;
Run Code Online (Sandbox Code Playgroud)

请参阅time.hms.overview了解更多信息。