C++ 打印 chrono::duration 的天数、小时数、分钟数等

标签 c++ windows chrono

我有以下代码:

// #includes for <chrono>, <iostream>, etc.

using namespace std;
using namespace chrono;

int main(int argc, char* argv[]) {
    auto sysStart = system_clock::now();

    LogInit();  // Opens a log file for reading/writing
    // Last shutdown time log entry is marked by a preceding null byte
    logFile.ignore(numeric_limits<streamsize>::max(), '\0');

    if (logFile.fail() || logFile.bad()) {
        // Calls GetLastError() and logs the error code and message
        LogError("main");
    }

    // Parse the timestamp at the start of the shutdown log entry
    tm end = { 0 };
    logFile >> get_time(&end, "[%d-%m-%Y %T");

    if (logFile.fail() || logFile.bad()) {
        // Same as above. Param is name of function within which error occurred
        LogError("main");
    }

    // Finally, we have the last shutdown time as a chrono::time_point
    auto sysEnd = system_clock::from_time_t(mktime(&end));

    // Calculate the time for which the system was inactive
    auto sysInactive = sysStart - sysEnd;
    auto hrs = duration_cast<hours>(sysInactive).count();
    auto mins = duration_cast<minutes>(sysInactive).count() - hrs * 60;
    auto secs = duration_cast<seconds>(sysInactive).count() - (hrs * 3600) - mins * 60;
    auto ms = duration_cast<milliseconds>(sysInactive).count() - (hrs * 3600000)
        - (mins * 60000) - secs * 1000;

    return 0;
}
它可以工作,但它很丑陋,而且对我来说太冗长了。任何更简单的方法来做到这一点只使用 STL 函数? MTIA :-)
编辑:我意识到完整的代码毕竟不是那么复杂,所以根据@idclev463035818 的评论,我已经添加了缺失的内容。

最佳答案

当您无法使用 fmtlib 时或等待 C++20 <format> ,您至少要延迟调用 cout()尽可能多的时间。另外,让 <chrono>为您处理计算。这两种措施都提高了片段的简洁性:

const auto hrs = duration_cast<hours>(sysInactive);
const auto mins = duration_cast<minutes>(sysInactive - hrs);
const auto secs = duration_cast<seconds>(sysInactive - hrs - mins);
const auto ms = duration_cast<milliseconds>(sysInactive - hrs - secs);
和输出:
cout << "System inactive for " << hrs.count() <<
    ":" << mins.count() <<
    ":" << secs.count() <<
    "." << ms.count() << endl;
请注意,您还可以定义实用程序模板,
template <class Rep, std::intmax_t num, std::intmax_t denom>
auto chronoBurst(std::chrono::duration<Rep, std::ratio<num, denom>> d)
{
    const auto hrs = duration_cast<hours>(d);
    const auto mins = duration_cast<minutes>(d - hrs);
    const auto secs = duration_cast<seconds>(d - hrs - mins);
    const auto ms = duration_cast<milliseconds>(d - hrs - secs);

    return std::make_tuple(hrs, mins, secs, ms);
}
结合结构化绑定(bind)有一个很好的用例:
const auto [hrs, mins, secs, ms] = chronoBurst(sysInactive);

关于C++ 打印 chrono::duration 的天数、小时数、分钟数等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64169258/

相关文章:

c++ - 访问实异常(exception)部的函数指针

c++ - 如何用 C++ 编写多个 GetAsyncKeyState

c# - 以用户身份运行我的程序

c++ - 使用C++将时间戳转换为格式化的日期时间

c++ - 如何在C++ 20中使用格式打印毫秒

c++ - 部分模板特化可能不适用于函数,但重载不是同一件事吗?

c++ - std::rethrow_exception(nullptr) 未定义行为或 bad_exception?

c# - 在依赖 Oracle 的 Windows 启动时自动启动 Windows 服务

windows - 为什么 WmiPrvSE.exe 持有我进程的作业对象的句柄?

c++ - 为什么 C++20 中的 time_of_day 和 hh_mm_ss 是两种不同的类型?