gpt4 book ai didi

c++ - C++ 20 std::chrono::duration格式的缺点

转载 作者:行者123 更新时间:2023-12-02 10:21:53 24 4
gpt4 key购买 nike

我需要以易于理解的方式打印可能超过24小时的std::chrono::duration,例如"2 d. 09:10:11"

C++ 20的std::chrono::time_pointstd::chrono::duration formatting(基于Howard Hinnant的date library)包含大量有用的格式说明符,但std::chrono::duration中没有多少天说明符。最接近的一个是%j,但是它用零填充。也没有一天中小时数的说明符。最接近的是%H(但也包括整天的小时数)和%OH(但在断言小时数少于24时崩溃)。

我想念什么吗?

在C++ 20的std::format中添加更多说明符是否有意义?

到目前为止,我正在使用workaround function:

#include <chrono>
#include <cstring> // std::strncmp
#include <date.h> // use Howard Hinnant's date library until C++20 is supported by compilers
#include <string>

// Workaround for C++20 std::chrono::format's inability to produce number of days in std::chrono::duration.
// Temporary format specifier for this is `%J`. It must be in the beginning of the format string.
template<typename Rep, typename Period>
std::string duration_to_str(const char* format, const std::chrono::duration<Rep, Period>& duration)
{
if (std::strncmp(format, "%J", 2))
return date::format(format, duration);
static const auto one_day = std::chrono::hours(24);
const size_t days = std::chrono::duration_cast<std::chrono::hours>(duration).count() / 24;
return std::to_string(days) + date::format(format + 2, duration - days * one_day);
}

测试:

#include <iostream>

template<typename Rep, typename Period>
void test_dur_to_str(const std::chrono::duration<Rep, Period>& duration)
{
std::cout << duration_to_str("%T", duration) << ":\n"
<< duration_to_str("%j d. %T", duration) << '\n'
<< duration_to_str("%J d. %T", duration) << "\n\n";
}

int main()
{
test_dur_to_str(std::chrono::seconds(24 * 60 * 60 - 1));
test_dur_to_str(std::chrono::seconds(24 * 60 * 60));
test_dur_to_str(std::chrono::seconds(91297));
test_dur_to_str(std::chrono::seconds(2 * 24 * 60 * 60 - 1));
test_dur_to_str(std::chrono::seconds(2 * 24 * 60 * 60));
test_dur_to_str(std::chrono::seconds(2 * 24 * 60 * 60 + 1));
test_dur_to_str(std::chrono::seconds(13 * 24 * 60 * 60 + 5));
test_dur_to_str(std::chrono::seconds(365 * 24 * 60 * 60 + 5));
test_dur_to_str(std::chrono::seconds(366 * 24 * 60 * 60 + 5));
test_dur_to_str(std::chrono::seconds(367 * 24 * 60 * 60 + 5));
}

输出:
23:59:59:
000 d. 23:59:59
0 d. 23:59:59

24:00:00:
001 d. 24:00:00
1 d. 00:00:00

25:21:37:
001 d. 25:21:37
1 d. 01:21:37

47:59:59:
001 d. 47:59:59
1 d. 23:59:59

48:00:00:
002 d. 48:00:00
2 d. 00:00:00

48:00:01:
002 d. 48:00:01
2 d. 00:00:01

312:00:05:
013 d. 312:00:05
13 d. 00:00:05

8760:00:05:
365 d. 8760:00:05
365 d. 00:00:05

8784:00:05:
366 d. 8784:00:05
366 d. 00:00:05

8808:00:05:
367 d. 8808:00:05
367 d. 00:00:05

最佳答案

我想展示霍华德·辛南特(Howard Hinnant)在其评论中建议的变通方法函数simplified version,以供可能使用它的人使用:

#include <chrono>
#include <cstring> // std::strncmp
#include <date.h> // use Howard Hinnant's date library until C++20 is supported by compilers
#include <string>

template<typename Rep, typename Period>
std::string duration_to_str(const char* format, const std::chrono::duration<Rep, Period>& duration)
{
if (std::strncmp(format, "%J", 2))
return date::format(format, duration);
const auto days = std::chrono::duration_cast<date::days>(duration);
return std::to_string(days.count()) + date::format(format + 2, duration - days);
}

关于c++ - C++ 20 std::chrono::duration格式的缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59828474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com