利用boost获取时间并格式化
利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题。
1. 输出YYYYMMDD
- #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;
- #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
- #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;
- #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里计算时间间隔的功能很多很强大,我列举的仅仅是我目前用到的。
- #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();
- #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();
- // 得到两个时间间隔的秒
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
2014-04-25 qt QSettings
2014-04-25 Qt *.pro工程文件 详解
2014-04-25 QT中显示动画
2014-04-25 qt creator中常用快捷键