名前空間
変種
操作

std::timespec

From cppreference.com
< cpp‎ | chrono‎ | c
 
 
 
 
ヘッダ <ctime> で定義
struct timespec;
(C++17以降)

秒とナノ秒に分解された時間を保持する構造体。

目次

[編集] データメンバー

メンバ 説明
秒 (整数部)。値は 0 以上。
(public メンバーオブジェクト)
long tv_nsec
ナノ秒。値の範囲は [0999999999]
(public メンバーオブジェクト)

tv_sectv_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

[編集] 関連項目

与えられた時間基準に基づいて、カレンダー時間を秒とナノ秒で返す
(関数) [編集]
カレンダー時間型
(クラス) [編集]
English 日本語 中文(简体) 中文(繁體)