C++11 – std::chrono – 使用std::chrono::duration_cast进行时间转换,hours/minutes/seconds/milliseconds/microseconds相互转换,以及自定义duration进行转换
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进行转换
原文链接:https://www.stubbornhuang.com/1139/
发布于:2021年02月05日 11:18:04
修改于:2023年06月26日 21:56:14
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论
52