利用boost獲取時間并格式化的方法
更新時間:2017年03月16日 10:19:32 投稿:jingxian
下面小編就為大家?guī)硪黄胋oost獲取時間并格式化的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
利用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;
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;
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(); // 得到兩個時間間隔的秒數(shù); int sec = time_elapse.total_seconds();
以上這篇利用boost獲取時間并格式化的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Cocos2d-x 3.x入門教程(二):Node節(jié)點類
這篇文章主要介紹了Cocos2d-x 3.x入門教程(二):Node節(jié)點類,本文對Node節(jié)點類做了一個簡明講解及Node類提供的函數(shù)做了說明,需要的朋友可以參考下2014-11-11C語言數(shù)據(jù)結(jié)構(gòu)與算法之圖的遍歷(一)
這篇文章主要是介紹了利用深度優(yōu)先算法實現(xiàn)圖的遍歷,文中利用圖文詳細的介紹了實現(xiàn)步驟,對我們學習數(shù)據(jù)結(jié)構(gòu)與算法有一定的幫助,需要的朋友可以參考一下2021-12-12C++中typedef 及其與struct的結(jié)合使用
這篇文章主要介紹了C++中typedef 及其與struct的結(jié)合使用,需要的朋友可以參考下2014-02-02