【问题标题】:Add time duration to C++ timepoint将持续时间添加到 C++ 时间点
【发布时间】:2015-12-25 00:43:46
【问题描述】:

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

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??

【问题讨论】:

    标签: c++ c++11 chrono


    【解决方案1】:

    如果你想给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(),你应该很高兴。

    【讨论】:

    • 或者,将system_clock::time_point 更改为steady_clock::time_point
    • 我想补充一点,如果您的 time_point 的时间分辨率比您要添加的单位更粗略,则它不适用于隐式转换,并且必须进行显式转换。 f.e.如果你想将 nanoseconds(1) 添加到具有 10ns 分辨率的 stable_clock::now() ,那么上面的代码不能直接工作。这是我今天缺少的一条信息……
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      将 time_point 转换为 duration 或将 duration 转换为 time_point 而无需中间。

      本质上不可能将 time_point 转换为 duration 或直接返回。 很多例子使用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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-31
        • 2021-05-28
        • 2021-06-21
        • 2016-02-23
        相关资源
        最近更新 更多