C++11 - std::chrono - 使用std::chrono::duration_cast进行时间转换

这些C++代码示例展示了如何使用chrono库将不同时间单位(小时、分钟、秒、毫秒、微秒)相互转换,包括自定义duration类型的转换。通过duration_cast模板函数,可以轻松实现时间精度的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(); 
}

如果有兴趣可以访问我的个站:https://www.stubbornhuang.com,更多知识等着您去发现!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HW140701

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值