C++ C++;增强库本地\u时间\u周期
使用C++ C++;增强库本地\u时间\u周期,c++,boost,C++,Boost,使用#包括“boost/date\u time/posix\u time/posix\u time.hpp”可以获取当前本地时间。我创建了获取本地时间的函数: boost::posix_time::ptime getTime(){ return boost::posix_time::second_clock::local_time(); } 此函数用于返回boost::posix_time::ptime类型的本地时间。 返回示例:Date2017-11-05 07:58:36 我想要
#包括“boost/date\u time/posix\u time/posix\u time.hpp”
可以获取当前本地时间。我创建了获取本地时间的函数:
boost::posix_time::ptime getTime(){
return boost::posix_time::second_clock::local_time();
}
此函数用于返回boost::posix_time::ptime
类型的本地时间。
返回示例:Date2017-11-05 07:58:36
我想要一些开始日期和结束日期。然后使用boost::posix_time::time_period
计算开始日期和结束日期的时间段
int main(){
boost::posix_time::ptime startDate = getTime();
boost::posix_time::ptime endDate;
string duration;
cout << "start Date" << startDate << endl;
for(int i = 0; i <= 900000000; i++) {
if(i == 900000000){
endDate = getTime();
}
}
cout << "End Date " << endDate << endl;
duration = to_simple_string(boost::posix_time::time_period(startDate,endDate).length());
cout << "Duration: " << duration << endl;
}
intmain(){
boost::posix_time::ptime startDate=getTime();
boost::posix_time::ptime endDate;
字符串持续时间;
cout如果你减去两个ptime
对象,你将得到一个time\u duration
对象。这个对象有一个total\u seconds()
方法,它将给出总秒数
例如:
请参阅:
// startTime and endTime are ptime instances
time_duration d = endTime - startTime;
long secs = d.total_seconds();