std::chrono::duration
提供: cppreference.com
| ヘッダ <chrono> で定義
|
||
| template< class Rep, |
(C++11およびそれ以降) | |
クラステンプレート std::chrono::duration は時の間隔を表します。
これは刻み数と刻み幅から構成されます。 刻み数は Rep 型の値で、刻み幅はある刻みから次の刻みまでの秒数を表すコンパイル時有理数の定数です。
duration に格納されるデータは Rep 型の刻み数だけです。 Rep が浮動小数点の場合は、 duration は小数点以下の刻み数を表現できます。 Period は duration の型の一部に含まれ、異なる duration 間で変換するときにのみ使用されます。
目次 |
[編集] メンバ型
| メンバ型 | 定義 |
rep
|
Rep、刻み数を表す算術型
|
period
|
Period (C++17以前)typename Period::type (C++17およびそれ以降)、刻み幅を表す std::ratio (すなわち刻み1つあたりの秒数)
|
[編集] メンバ関数
| 新しい時間を構築します (パブリックメンバ関数) | |
| 内容を代入します (パブリックメンバ関数) | |
| 刻み数を返します (パブリックメンバ関数) | |
| [static] |
長さゼロの時間を返します (パブリック静的メンバ関数) |
| [static] |
時間の最小値を返します (パブリック静的メンバ関数) |
| [static] |
時間の最大値を返します (パブリック静的メンバ関数) |
| 単項 + および単項 - を実装します (パブリックメンバ関数) | |
| 刻み数をインクリメントまたはデクリメントします (パブリックメンバ関数) | |
| 2つの時間の間の複合代入を実装します (パブリックメンバ関数) |
[編集] 非メンバ関数
| std::common_type 特性の特殊化 (クラステンプレートの特殊化) | |
| 引数として時間を取る算術演算を実装します (関数テンプレート) | |
| 2つの時間を比較します (関数テンプレート) | |
| 時間を異なる刻み幅を持つ別の時間に変換します (関数テンプレート) | |
| (C++17) |
時間を別の時間に切り捨て変換します (関数テンプレート) |
| (C++17) |
時間を別の時間に切り上げ変換します (関数テンプレート) |
| (C++17) |
時間を別の時間の最も近い値に丸めて変換します (関数テンプレート) |
| (C++17) |
時間の絶対値を取得します (関数テンプレート) |
| (C++20) |
duration に対してストリーム出力を行います (関数テンプレート) |
| (C++20) |
指定された書式に従って duration をストリームに出力します (関数テンプレート) |
| (C++20) |
指定された書式に従って duration をストリームからパースします (関数テンプレート) |
[編集] ヘルパー型
| 型 | 定義 |
| std::chrono::nanoseconds | duration</*signed integer type of at least 64 bits*/, std::nano> |
| std::chrono::microseconds | duration</*signed integer type of at least 55 bits*/, std::micro> |
| std::chrono::milliseconds | duration</*signed integer type of at least 45 bits*/, std::milli> |
| std::chrono::seconds | duration</*signed integer type of at least 35 bits*/> |
| std::chrono::minutes | duration</*signed integer type of at least 29 bits*/, std::ratio<60>> |
| std::chrono::hours | duration</*signed integer type of at least 23 bits*/, std::ratio<3600>> |
| std::chrono::days (C++20およびそれ以降) | duration</*signed integer type of at least 25 bits*/, std::ratio<86400>> |
| std::chrono::weeks (C++20およびそれ以降) | duration</*signed integer type of at least 22 bits*/, std::ratio<604800>> |
| std::chrono::months (C++20およびそれ以降) | duration</*signed integer type of at least 20 bits*/, std::ratio<2629746>> |
| std::chrono::years (C++20およびそれ以降) | duration</*signed integer type of at least 17 bits*/, std::ratio<31556952>> |
ノート: 定義済みの時間型 nanoseconds, microseconds, milliseconds, seconds, minutes, hours は、いずれも少なくとも±292年の範囲をカバーします。
|
定義済みの時間型 |
(C++20およびそれ以降) |
[編集] ヘルパークラス
| 時間が異なる刻み幅を持つ時間に変換可能であることを表します (クラステンプレート) | |
| ゼロ、最小、最大の刻み数を持つ指定された型の値を構築します (クラステンプレート) |
[編集] リテラル
| 名前空間
std::literals::chrono_literals で定義 | |
| (C++14) |
時間を表す std::chrono::duration リテラル (関数) |
| (C++14) |
分を表す std::chrono::duration リテラル (関数) |
| (C++14) |
秒を表す std::chrono::duration リテラル (関数) |
| (C++14) |
ミリ秒を表す std::chrono::duration リテラル (関数) |
| (C++14) |
マイクロ秒を表す std::chrono::duration リテラル (関数) |
| (C++14) |
ナノ秒を表す std::chrono::duration リテラル (関数) |
| (C++20およびそれ以降) |
[編集] 例
この例は、どのようにカスタム時間型を定義し、その型の間で変換を行うかを示します
Run this code
#include <iostream> #include <chrono> constexpr auto year = 31556952ll; // seconds in average Gregorian year int main() { using shakes = std::chrono::duration<int, std::ratio<1, 100000000>>; using jiffies = std::chrono::duration<int, std::centi>; using microfortnights = std::chrono::duration<float, std::ratio<14*24*60*60, 1000000>>; using nanocenturies = std::chrono::duration<float, std::ratio<100*year, 1000000000>>; std::chrono::seconds sec(1); std::cout << "1 second is:\n"; // integer scale conversion with no precision loss: no cast std::cout << std::chrono::microseconds(sec).count() << " microseconds\n" << shakes(sec).count() << " shakes\n" << jiffies(sec).count() << " jiffies\n"; // integer scale conversion with precision loss: requires a cast std::cout << std::chrono::duration_cast<std::chrono::minutes>(sec).count() << " minutes\n"; // floating-point scale conversion: no cast std::cout << microfortnights(sec).count() << " microfortnights\n" << nanocenturies(sec).count() << " nanocenturies\n"; }
出力:
1 second is: 1000000 microseconds 100000000 shakes 100 jiffies 0 minutes 0.82672 microfortnights 0.316887 nanocenturies

