利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题。
1. 输出YYYYMMDD
?
|
1
2
3
4
5
6
7
|
#include <boost/date_time/gregorian/gregorian.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::gregorian::to_iso_string(\\
boost::gregorian::day_clock::local_day());
std::cout << strTime.c_str() << std::endl;
|
2. 输出YYYYMMDD-HH:MM:SS
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
std::string strTime = boost::posix_time::to_iso_string(\\
boost::posix_time::second_clock::local_time());
// 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了
int pos = strTime.find('T');
strTime.replace(pos,1,std::string("-"));
strTime.replace(pos + 3,0,std::string(":"));
strTime.replace(pos + 6,0,std::string(":"));
std::cout << strTime.c_str() << std::endl;
|
3.计算时间间隔。boost里计算时间间隔的功能很多很强大,我列举的仅仅是我目前用到的。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#define BOOST_DATE_TIME_SOURCE
boost::posix_time::ptime time_now,time_now1;
boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
// 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;
time_now = boost::posix_time::microsec_clock::universal_time();
// sleep 100毫秒;
boost::this_thread::sleep(boost::posix_time::millisec(100));
time_now1 = boost::posix_time::microsec_clock::universal_time();
time_elapse = time_now1 - time_now;
// 类似GetTickCount,只是这边得到的是2个时间的ticket值的差,以微秒为单位;
int ticks = time_elapse.ticks();
// 得到两个时间间隔的秒数;
int sec = time_elapse.total_seconds();
|
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 50
-
2025-05-25 38
-
2025-05-27 45
-
2025-06-04 78
-
2025-06-04 84
热门评论

