名稱空間
變體
操作

std::timespec

來自 cppreference.com
< cpp‎ | chrono‎ | c
 
 
 
 
定義於標頭檔案 <ctime>
struct timespec;
(C++17 起)

一個結構體,用於儲存分解為秒和納秒的時間間隔。

目錄

[編輯] 資料成員

成員 描述
整秒數,值 >= 0
(公有成員物件)
long tv_nsec
納秒數,值在範圍 [0999999999]
(公有成員物件)

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

[編輯] 參閱

根據給定時間基準返回以秒和納秒錶示的日曆時間
(函式) [編輯]
日曆時間型別
(類) [編輯]
C 文件 關於 timespec