名前空間
変種
操作

std::chrono::time_point

From cppreference.com
< cpp‎ | chrono
 
 
 
 
ヘッダー <chrono> で定義
template<

    class Clock,
    class Duration = typename Clock::duration

> class time_point;
(C++11以降)

クラステンプレート std::chrono::time_point は時刻における時点を表します。これは、Clock のエポックの開始からの時間間隔を示す Duration 型の値を格納しているかのように実装されています。

ClockClock の要件を満たすか、std::chrono::local_t である(C++20以降)必要があります。

(C++23まで)

目次

[編集] メンバー型

メンバ型 定義
clock Clock、この時点が計測されるクロック
duration Duration、エポックからの時間を計測するために使用される std::chrono::duration
rep Rep、期間のティック数を表す算術型
period Period、期間のティック周期を表す std::ratio

[編集] メンバー関数

新しい時刻の時点を構築する
(public メンバー関数) [編集]
そのクロックの開始からの期間として時刻の時点を返す
(public メンバー関数) [編集]
指定された期間によって時刻の時点を変更する
(public メンバー関数) [編集]
期間をインクリメントまたはデクリメントする
(public メンバー関数) [編集]
[static]
最小の期間に対応する時刻の時点を返す
(public static メンバー関数) [編集]
[static]
最大の期間に対応する時刻の時点を返す
(public static メンバー関数) [編集]

[編集] 非メンバー関数

タイムポイントを含む加算および減算演算を実行
(関数テンプレート) [編集]
(C++11)(C++11)(C++20で削除)(C++11)(C++11)(C++11)(C++11)(C++20)
2つの時間点を比較する
(関数テンプレート) [edit]
時間点を、同じクロック上の異なる期間を持つ別の時間点に変換する
(関数テンプレート) [edit]
time_point を別の time_point に変換し、切り捨てる
(関数テンプレート) [edit]
time_point を別の time_point に変換し、切り上げる
(関数テンプレート) [edit]
time_point を別の time_point に変換し、最も近い値に丸め、等距離の場合は偶数に丸める
(関数テンプレート) [edit]

[編集] ヘルパークラス

std::common_type 特性を特殊化する
(クラス テンプレートの特殊化) [edit]
std::chrono::time_point のハッシュサポート
(クラステンプレートの特殊化)

[編集]

#include <algorithm>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
 
void slow_motion()
{
    static int a[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    // Generate Γ(13) == 12! permutations:
    while (std::ranges::next_permutation(a).found) {}
}
 
int main()
{
    using namespace std::literals; // enables literal suffixes, e.g. 24h, 1ms, 1s.
 
    const std::chrono::time_point<std::chrono::system_clock> now =
        std::chrono::system_clock::now();
 
    const std::time_t t_c = std::chrono::system_clock::to_time_t(now - 24h);
    std::cout << "24 hours ago, the time was "
              << std::put_time(std::localtime(&t_c), "%F %T.\n") << std::flush;
 
    const std::chrono::time_point<std::chrono::steady_clock> start =
        std::chrono::steady_clock::now();
 
    std::cout << "Different clocks are not comparable: \n"
                 "  System time: " << now.time_since_epoch() << "\n"
                 "  Steady time: " << start.time_since_epoch() << '\n';
 
    slow_motion();
 
    const auto end = std::chrono::steady_clock::now();
    std::cout
        << "Slow calculations took "
        << std::chrono::duration_cast<std::chrono::microseconds>(end - start) << " ≈ "
        << (end - start) / 1ms << "ms ≈ " // almost equivalent form of the above, but
        << (end - start) / 1s << "s.\n";  // using milliseconds and seconds accordingly
}

実行結果の例

24 hours ago, the time was 2021-02-15 18:28:52.
Different clocks are not comparable:
  System time: 1666497022681282572ns
  Steady time: 413668317434475ns
Slow calculations took 2090448µs ≈ 2090ms ≈ 2s.

[編集] 関連項目

(C++11)
時間の間隔
(クラステンプレート) [編集]
特定のyearmonthdayを表す
(クラス) [編集]
English 日本語 中文(简体) 中文(繁體)