zoukankan      html  css  js  c++  java
  • 利用boost获取时间并格式化

     

    利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题。

    1. 输出YYYYMMDD

    [cpp] view plaincopy
     
    1. #include <boost/date_time/gregorian/gregorian.hpp>   
    2. #define BOOST_DATE_TIME_SOURCE   
    3.   
    4. std::string strTime = boost::gregorian::to_iso_string(  
    5. boost::gregorian::day_clock::local_day());  
    6.   
    7. std::cout << strTime.c_str() << std::endl;  
    [cpp] view plaincopy
     
    1. #include <boost/date_time/gregorian/gregorian.hpp>  
    2. #define BOOST_DATE_TIME_SOURCE  
    3.   
    4. std::string strTime = boost::gregorian::to_iso_string(  
    5. boost::gregorian::day_clock::local_day());  
    6.   
    7. std::cout << strTime.c_str() << std::endl;  

    2. 输出YYYYMMDD-HH:MM:SS
    [cpp] view plaincopy
     
    1. #include <boost/date_time/posix_time/posix_time.hpp>   
    2. #define BOOST_DATE_TIME_SOURCE   
    3.   
    4. std::string strTime = boost::posix_time::to_iso_string(  
    5. boost::posix_time::second_clock::local_time());  
    6.   
    7. // 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了   
    8.   
    9. int pos = strTime.find('T');  
    10. strTime.replace(pos,1,std::string("-"));  
    11. strTime.replace(pos + 3,0,std::string(":"));  
    12. strTime.replace(pos + 6,0,std::string(":"));  
    13.   
    14. std::cout << strTime.c_str() << std::endl;  
    [cpp] view plaincopy
     
    1. #include <boost/date_time/posix_time/posix_time.hpp>  
    2. #define BOOST_DATE_TIME_SOURCE  
    3.   
    4. std::string strTime = boost::posix_time::to_iso_string(  
    5. boost::posix_time::second_clock::local_time());  
    6.   
    7. // 这时候strTime里存放时间的格式是YYYYMMDDTHHMMSS,日期和时间用大写字母T隔开了  
    8.   
    9. int pos = strTime.find('T');  
    10. strTime.replace(pos,1,std::string("-"));  
    11. strTime.replace(pos + 3,0,std::string(":"));  
    12. strTime.replace(pos + 6,0,std::string(":"));  
    13.   
    14. std::cout << strTime.c_str() << std::endl;  
    3. 计算时间间隔。boost里计算时间间隔的功能很多很强大,我列举的仅仅是我目前用到的。
    [cpp] view plaincopy
     
    1. #include <boost/date_time/posix_time/posix_time.hpp>   
    2. #include <boost/thread.hpp>   
    3. #define BOOST_DATE_TIME_SOURCE   
    4.   
    5. boost::posix_time::ptime time_now,time_now1;  
    6. boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;  
    7.   
    8. // 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;   
    9. time_now = boost::posix_time::microsec_clock::universal_time();  
    10.   
    11. // sleep 100毫秒;   
    12. boost::this_thread::sleep(boost::posix_time::millisec(100));  
    13.   
    14. time_now1 = boost::posix_time::microsec_clock::universal_time();  
    15.   
    16. time_elapse = time_now1 - time_now;  
    17.   
    18. // 类似GetTickCount,只是这边得到的是2个时间的ticket值的差,以微秒为单位;   
    19. int ticks = time_elapse.ticks();  
    20.   
    21. // 得到两个时间间隔的秒数;   
    22. int sec = time_elapse.total_seconds();  
    [cpp] view plaincopy
     
    1. #include <boost/date_time/posix_time/posix_time.hpp>  
    2. #include <boost/thread.hpp>  
    3. #define BOOST_DATE_TIME_SOURCE  
    4.   
    5. boost::posix_time::ptime time_now,time_now1;  
    6. boost::posix_time::millisec_posix_time_system_config::time_duration_type time_elapse;  
    7.   
    8. // 这里为微秒为单位;这里可以将microsec_clock替换成second_clock以秒为单位;  
    9. time_now = boost::posix_time::microsec_clock::universal_time();  
    10.   
    11. // sleep 100毫秒;  
    12. boost::this_thread::sleep(boost::posix_time::millisec(100));  
    13.   
    14. time_now1 = boost::posix_time::microsec_clock::universal_time();  
    15.   
    16. time_elapse = time_now1 - time_now;  
    17.   
    18. // 类似GetTickCount,只是这边得到的是2个时间的ticket值的差,以微秒为单位;  
    19. int ticks = time_elapse.ticks();  
    20.   
    21. // 得到两个时间间隔的秒
  • 相关阅读:
    qemuimg convert 转换vmdk 等虚拟机文件到dd
    golang 占位符%d %t %v
    QT插件vs报错。 The system cannot find the path specified. 1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppCommon.targets(170,5): error MSB6006: “cmd.exe”已退出,代码为 3
    兆芯,ZXC4580
    jenkins 构建任务 —— 微服务依赖其它微服务
    jenkins构建项目后主动杀死进程再重启进程。
    升级jenkins到最新版本,并安装git插件
    golang基础 自定义类型和类型别名(type)
    SpinalWorkshop实验笔记(三)
    SpinalWorkshop实验笔记(一)
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/4455414.html
Copyright © 2011-2022 走看看