std::timespec_get
來自 cppreference.com
| 定義於標頭檔案 <ctime> |
||
| int timespec_get( std::timespec* ts, int base ); |
(1) | (C++17 起) |
| #define TIME_UTC /* implementation-defined */ |
(2) | (C++17 起) |
2) 展開為一個適合用作
std::timespec_get 的 base 引數的值。實現可以提供其他以 TIME_ 開頭的宏常量以指示額外的時間基準。
如果 base 是 TIME_UTC,則
- ts->tv_sec 被設定為自實現定義紀元以來的秒數,截斷為整數值,
- ts->tv_nsec 成員被設定為納秒的整數值,四捨五入到系統時鐘的解析度。
目錄 |
[編輯] 引數
| ts | - | 指向 std::timespec 型別物件的指標 |
| base | - | TIME_UTC 或另一個非零整數值,指示時間基準 |
[編輯] 返回值
如果成功則返回 base 的值,否則返回零。
[編輯] 注意
POSIX 函式 clock_gettime(CLOCK_REALTIME, ts) 也可以用來填充一個包含自紀元以來時間的 std::timespec。
[編輯] 示例
執行此程式碼
#include <ctime> #include <iostream> int main() { std::timespec ts; std::timespec_get(&ts, TIME_UTC); char buf[100]; std::strftime(buf, sizeof buf, "%D %T", std::gmtime(&ts.tv_sec)); std::cout << "Current time: " << buf << '.' << ts.tv_nsec << " UTC\n"; }
可能的輸出
Current time: 06/24/16 20:07:42.949494132 UTC
[編輯] 參閱
| (C++17) |
秒和納秒時間 (結構體) |
| 返回系統當前時間,作為自紀元以來的時間 (函式) | |
| C 文件 關於 timespec_get
| |