利用boost计算运行时间间隔及获取系统时间并格式化

1 篇文章 0 订阅

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

1. 输出YYYYMMDD

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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();   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中使用Boost获取文件的创建时间可以通过以下步骤实现: 第一步,包含所需的boost库头文件。需要包含<boost/filesystem.hpp>和<boost/date_time/posix_time/posix_time.hpp>。 第二步,使用boost::filesystem::path类来表示文件的路径和名称。可以使用构造函数来将文件名传递给path类。例如,boost::filesystem::path filePath("example.txt"); 第三步,使用boost::filesystem::last_write_time函数获取文件的最后修改时间,该函数返回一个表示时间的特殊类型:boost::filesystem::file_time_type。可以将该返回值存储在一个变量中。例如,boost::filesystem::file_time_type fileCreationTime = boost::filesystem::last_write_time(filePath); 第四步,为了使用方便,可以将返回的file_time_type转换为boost::posix_time::ptime类型,以便更容易地处理和显示时间。可以使用boost::posix_time::from_time_t函数进行转换。例如,boost::posix_time::ptime creationTime = boost::posix_time::from_time_t(boost::filesystem::last_write_time(filePath)); 第五步,通过使用string相关函数将ptime转换为所需的格式,例如yyyy/mm/dd hh:mm:ss。例如,std::string strCreationTime = boost::posix_time::to_simple_string(creationTime); 最后,可以打印或使用strCreationTime字符串中的文件创建时间。 这是使用Boost获取文件创建时间的一种方法。通过以上步骤,可以方便地获取和处理文件的创建时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值