将持续时间添加到C ++时间点

时间:2015-09-27 19:04:14

标签: c++ c++11 chrono

我有一个起始时间点,以毫秒为单位:

using namespace std::chrono;
typedef time_point<system_clock, milliseconds> MyTimePoint;

MyTimePoint startTimePoint = time_point_cast<MyTimePoint::duration>(system_clock::time_point(steady_clock::now()));

现在我要在startTimePoint上添加或减去一定的小时数。

int numHours = -5//or 5 etc (Can be a plus or minus number)

如何将这段时间添加到原始的startTimePoint?

3 个答案:

答案 0 :(得分:14)

如果您想在startTimePoint添加五个小时,那就非常简单了:

startTimePoint += hours(5); // from the alias std::chrono::hours

Live example

顺便说一句,您正在尝试将steady_clock::now()转换为system_clock::time_pointshouldn't even compile。将steady_clock::now()更改为system_clock::now(),您应该好好去。

答案 1 :(得分:1)

将time_point转换为持续时间,或将duration转换为time_point,而无需中间。

天生就不可能将time_point转换为持续时间或直接转换回持续时间。 许多示例都使用time_t作为中间方法,这是一种很好的方法。

我使用了将time_point'零'用作帮助器的方法。

#include <iostream>
#include <chrono>
#include <thread>

using namespace std;

int main(int argc, char *argv[])
{
    using namespace std::chrono;
    system_clock::time_point zero;      // initialised to zero in constructor
    system_clock::time_point tp_now;    // now as time_point
    duration<int, ratio<1>> dur_now;    // now as duration
    system_clock::time_point tp_future; // calculated future as time_point

    // The objective is to sleep_until the system time is at the next 5 minutes
    // boundary (e.g. time is 09:35)

    tp_now = system_clock::now();       // What time is it now?

    cout << "tp_now  = " << tp_now.time_since_epoch().count() << endl;

    // It is not possible to assign a time_point directly to a duration.
    // but the difference between two time_points can be cast to duration
    dur_now = duration_cast<seconds>(tp_now-zero); // subtract nothing from time_point

    cout << "dur_now = " << dur_now.count() << endl;

    // Instead of using seconds granularity, I want to use 5 minutes
    // so I define a suitable type: 5 minutes in seconds
    typedef duration<int,ratio<5*60>> dur5min;

    // When assigning the time_point (ok: duration) is truncated to the nearest 5min
    dur5min min5 = duration_cast<dur5min>(tp_now-zero); // (Yes, I do it from time_point again)

    cout << "min5 ('now' in 5min units) = " << min5.count() << endl;

    // The next 5 min time point is
    min5 += dur5min{1};

    cout << "min5 += dur5min{1}         = " << min5.count() << endl;

    // It is not possible to assign a duration directly to a time_point.
    // but I can add a duration to a time_point directly
    tp_future = zero + min5;

    cout << "tp_future = " << tp_future.time_since_epoch().count() << endl;

    // to be used in e.g. sleep_until

    // std::this_thread::sleep_until(tp_future);

    return 0;
}

答案 2 :(得分:1)

在这里,我已经花了几分钟的时间,您可以从用户那里得到想要的任何东西。 因此,以下是使用chrono的简单程序

#include <iostream>
#include <chrono>
using namespace std;
int main() {
    using clock = std::chrono::system_clock;
    clock::time_point nowp = clock::now();
    cout<<"Enter the time that you want to add in minutes"<<endl;
    int time_min;
    cin>>time_min;
    cin.ignore();
    clock::time_point end = nowp + std::chrono::minutes(time_min);
    time_t nowt =  clock::to_time_t ( nowp );
    time_t endt =  clock::to_time_t ( end);
    std::cout  << " " << ctime(&nowt) << "\n";
    std::cout << ctime(&endt) << std::endl;
    return 0;
}