Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;增强库本地\u时间\u周期_C++_Boost - Fatal编程技术网

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();