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

Add time duration to C++ timepoint

我有一个以毫秒为单位的开始时间点,如下所示:

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?

如果你想在 startTimePoint 上增加五个小时,这很简单:

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

Live example.

顺便说一下,您正在尝试将 steady_clock::now() 转换为 system_clock::time_point,即 shouldn't even compile。将 steady_clock::now() 更改为 system_clock::now() 就可以了。

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

本质上不可能将 time_point 转换为持续时间或直接返回。 很多例子都用time_t作为中间,这是一个很好的方法。

我用的是time_point'zero'作为辅助的方法

#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;
}

这里我用了几分钟的时间,你可以从用户那里得到你想要的任何东西。 所以下面是使用 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;
}