最終更新日時(UTC):
が更新

履歴 編集

function template
<chrono>

std::chrono::operator-(C++11)

namespace std {
namespace chrono {
  template <class Clock, class Duration1, class Rep2, class Period2>
  time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
    operator-(const time_point<Clock, Duration1>& lhs,
              const duration<Rep2, Period2>& rhs);      // (1) C++11

  // time_point - duration = time_point
  template <class Clock, class Duration1, class Rep2, class Period2>
  constexpr time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
    operator-(const time_point<Clock, Duration1>& lhs,
              const duration<Rep2, Period2>& rhs);      // (1) C++14

  // time_point - time_point - duration
  template <class Clock, class Duration1, class Duration2>
  typename common_type<Duration1, Duration2>::type
    operator-(const time_point<Clock, Duration1>& lhs,
              const time_point<Clock, Duration2>& rhs); // (2) C++11

  // time_point - time_point - duration
  template <class Clock, class Duration1, class Duration2>
  constexpr typename common_type<Duration1, Duration2>::type
    operator-(const time_point<Clock, Duration1>& lhs,
              const time_point<Clock, Duration2>& rhs); // (2) C++14
}}

概要

time_pointの減算を行う。

  • (1) : time_pointから時間間隔を減算する
  • (2) : time_point同士の差を求める

戻り値

  • (2)

return lhs + (-rhs);

  • (3)

return lhs.time_since_epoch() - rhs.time_since_epoch();

#include <iostream>
#include <chrono>

namespace chrono = std::chrono;

int main()
{
  using time_point = chrono::system_clock::time_point;

  time_point now = chrono::system_clock::now();

  // (1) 
  time_point tp1 = now - chrono::seconds{3};
  auto sec1 = chrono::duration_cast<chrono::seconds>(tp1.time_since_epoch());
  std::cout << sec1.count() << std::endl;

  // (2)
  auto sec2 = chrono::duration_cast<chrono::seconds>(now - tp1);
  std::cout << sec2.count() << std::endl;
}

出力例

1572329112
3

バージョン

言語

  • C++11

処理系

参照