std::timespec
From cppreference.com
| ヘッダ <ctime> で定義 |
||
| struct timespec; |
(C++17以降) | |
秒とナノ秒に分解された時間を保持する構造体。
目次 |
[編集] データメンバー
| メンバ | 説明 |
| std::time_t tv_sec |
秒 (整数部)。値は 0 以上。 (public メンバーオブジェクト) |
| long tv_nsec |
ナノ秒。値の範囲は [0, 999999999](public メンバーオブジェクト) |
tv_sec と tv_nsec の宣言順は未規定です。実装は timespec に他のデータメンバーを追加する場合があります。
[編集] 備考
tv_nsec の型は、一部のプラットフォームでは long long ですが、これは現在 C++ では非準拠ですが、C23 以降の C では許可されています。
[編集] 例
このコードを実行
#include <ctime> #include <iostream> int main() { std::timespec ts; std::timespec_get(&ts, TIME_UTC); char buff[0x80]; std::strftime(buff, sizeof buff, "%D %T", std::gmtime(&ts.tv_sec)); // auto [sec, nsec] = ts; // UB: structured bindings should not be used because the // declaration order and data member list are unspecified std::cout << "Current time: " << buff << " (UTC)\n" << "Raw timespec.tv_sec: " << ts.tv_sec << '\n' << "Raw timespec.tv_nsec: " << ts.tv_nsec << '\n'; }
実行結果の例
Current time: 04/06/23 12:03:31 (UTC) Raw timespec.tv_sec: 1680782611 Raw timespec.tv_nsec: 678437213
[編集] 関連項目
| (C++17) |
与えられた時間基準に基づいて、カレンダー時間を秒とナノ秒で返す (関数) |
| カレンダー時間型 (クラス) | |
| C ドキュメント (timespec)
| |