c++ - 将 minutes::rep 转换为 hours::rep

标签 c++ linux date c++11 c++-chrono

我应该如何继续将 std::chrono::minutes::rep 类型值转换为小时表示。

#include <iostream>
#include <chrono>

using namespace std;
using namespace std::chrono;

int main() {
    minutes::rep time = 4;
    std::cout << time;  // outputs as minutes, need in hours

    // duration cast doesn't seems to work here because it
    // needs minutes instead of minutes::rep probably

    return 0;
}

最佳答案

minutes::rep time = 4;
std::cout << time;  // outputs as minutes, need in hours

因为 time 只是一个(实现定义的)整数类型,它与 minuteshours 无关,并且对基础时间表示一无所知。

您想停留在duration 领域:

minutes time{4};
auto as_hours = std::duration_cast<hours>(time);
std::cout << as_hours.count(); // prints 0

或者,可能:

auto as_hours_dbl = std::duration_cast<duration<double, hours::period>>(time);
std::cout << as_hours_dbl.count(); // prints 0.0666667

关于c++ - 将 minutes::rep 转换为 hours::rep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39898935/

相关文章:

c++ - 查找数组中所有总和为给定值的三元组。数组中有重复项

c++ - 快速生成变量嵌套for循环的数字组合

c - 套接字服务器内存使用率上升,变得无响应

c++ - 有没有办法使用 typedef 中的参数名称

c - 来自一个父进程的多个子进程

linux - Linux 中大小介于 2 之间的文件数

Windows 和 Linux 中的 PHP 日期函数

mysql date_format 需要作为字符串返回

javascript - 如何使用 Javascript 获取日期差

c++ - 从一系列图像创建 CvCapture