1 小时转换为分钟/秒/毫秒/微秒

#include <iostream>
#include <string>
#include <chrono>


int main()
{
    std::chrono::hours hour_time = std::chrono::hours(1);

    std::chrono::minutes minutes_time = std::chrono::duration_cast<std::chrono::minutes>(hour_time);

    std::chrono::seconds seconds_time = std::chrono::duration_cast<std::chrono::seconds>(hour_time);

    std::chrono::milliseconds milliseconds_time = std::chrono::duration_cast<std::chrono::milliseconds>(hour_time);

    std::chrono::microseconds microseconds_time = std::chrono::duration_cast<std::chrono::microseconds>(hour_time);

    std::cout << "1小时可转换为 \n"
        << minutes_time.count() << "分钟 \n"
        << seconds_time.count() << "秒 \n"
        << milliseconds_time.count() << "毫秒 \n"
        << microseconds_time.count() << "微秒" << std::endl;

    getchar();
    return 0;
}

2 分钟转换为小时/秒/毫秒/微秒

#include <iostream>
#include <string>
#include <chrono>


int main()
{
    std::chrono::minutes minutes_time = std::chrono::minutes(66);

    std::chrono::hours hours_time = std::chrono::duration_cast<std::chrono::hours>(minutes_time);

    std::chrono::seconds seconds_time = std::chrono::duration_cast<std::chrono::seconds>(minutes_time);

    std::chrono::milliseconds milliseconds_time = std::chrono::duration_cast<std::chrono::milliseconds>(minutes_time);

    std::chrono::microseconds microseconds_time = std::chrono::duration_cast<std::chrono::microseconds>(minutes_time);

    std::cout << "分钟可转换为 \n"
        << hours_time.count() << "小时 \n"
        << seconds_time.count() << "秒 \n"
        << milliseconds_time.count() << "毫秒 \n"
        << microseconds_time.count() << "微秒" << std::endl;

    getchar();
    return 0;
}

3 秒转换为小时/分钟/毫秒/微秒

#include <iostream>
#include <string>
#include <chrono>


int main()
{
    std::chrono::seconds seconds_time = std::chrono::seconds(36000);

    std::chrono::hours hours_time = std::chrono::duration_cast<std::chrono::hours>(seconds_time);

    std::chrono::minutes minutes_time = std::chrono::duration_cast<std::chrono::minutes>(seconds_time);

    std::chrono::milliseconds milliseconds_time = std::chrono::duration_cast<std::chrono::milliseconds>(seconds_time);

    std::chrono::microseconds microseconds_time = std::chrono::duration_cast<std::chrono::microseconds>(seconds_time);

    std::cout << "秒钟可转换为 \n"
        << hours_time.count() << "小时 \n"
        << minutes_time.count() << "分钟 \n"
        << milliseconds_time.count() << "毫秒 \n"
        << microseconds_time.count() << "微秒" << std::endl;

    getchar();
    return 0;
}

4 毫秒转换为小时/分钟/秒/微秒

#include <iostream>
#include <string>
#include <chrono>


int main()
{
    std::chrono::milliseconds milliseconds_time = std::chrono::milliseconds(3600000);

    std::chrono::hours hours_time = std::chrono::duration_cast<std::chrono::hours>(milliseconds_time);

    std::chrono::minutes minutes_time = std::chrono::duration_cast<std::chrono::minutes>(milliseconds_time);

    std::chrono::seconds seconds_time = std::chrono::duration_cast<std::chrono::seconds>(milliseconds_time);

    std::chrono::microseconds microseconds_time = std::chrono::duration_cast<std::chrono::microseconds>(milliseconds_time);

    std::cout << "毫秒可转换为 \n"
        << hours_time.count() << "小时 \n"
        << minutes_time.count() << "分钟 \n"
        << seconds_time.count() << "秒 \n"
        << microseconds_time.count() << "微秒" << std::endl;

    getchar();
    return 0;
}

5 微秒转换为小时/分钟/秒/毫秒

#include <iostream>
#include <string>
#include <chrono>


int main()
{
    std::chrono::microseconds microseconds_time = std::chrono::microseconds(3600000000);

    std::chrono::hours hours_time = std::chrono::duration_cast<std::chrono::hours>(microseconds_time);

    std::chrono::minutes minutes_time = std::chrono::duration_cast<std::chrono::minutes>(microseconds_time);

    std::chrono::seconds seconds_time = std::chrono::duration_cast<std::chrono::seconds>(microseconds_time);

    std::chrono::milliseconds milliseconds_time = std::chrono::duration_cast<std::chrono::milliseconds>(microseconds_time);

    std::cout << "微秒可转换为 \n"
        << hours_time.count() << "小时 \n"
        << minutes_time.count() << "分钟 \n"
        << seconds_time.count() << "秒 \n"
        << milliseconds_time.count() << "毫秒" << std::endl;

    getchar();
    return 0;
}

6 自定义duration进行转换

#include <iostream> 
#include <chrono> 

typedef std::chrono::duration<float, std::ratio<3, 1> > three_seconds; 
typedef std::chrono::duration<float, std::ratio<1, 10> > one_tenth_seconds; 

int main() 
{ 
    three_seconds s = std::chrono::duration_cast<three_seconds>(one_tenth_seconds(3)); 
    std::cout << "3 [1/10 seconds] equal to " << s.count() << " [3 seconds]\n"; 
    std::cin.get(); 
}

当前分类随机文章推荐

全站随机文章推荐

关于本站站长 StubbornHuang
C++11 – std::chrono – 使用std::chrono::duration_cast进行时间转换,hours/minutes/seconds/milliseconds/microseconds相互转换,以及自定义duration进行转换-StubbornHuang Blog纵使晴明无雨色,入云深处亦沾衣。