名稱空間
變體
操作

timespec_get

來自 cppreference.com
< c‎ | 時間
定義於標頭檔案 <time.h>
int timespec_get( struct timespec *ts, int base );
(1) (C11 起)
#define TIME_UTC /* implementation-defined */
(2) (C11 起)
1) 修改由 ts 指向的 timespec 物件,使其儲存時間基準 base 中的當前日曆時間。
2) 展開為一個適合用作 timespec_getbase 引數的值

實現可以提供其他以 TIME_ 開頭的宏常量,以指示額外的時間基準

如果 baseTIME_UTC,則

  • ts->tv_sec 被設定為自實現定義紀元以來的秒數,截斷為整數值
  • ts->tv_nsec 成員被設定為納秒的整數值,四捨五入到系統時鐘的解析度

目錄

[編輯] 引數

ts - 指向 struct timespec 型別物件的指標
base - TIME_UTC 或指示時間基準的另一個非零整數值

[編輯] 返回值

如果成功,返回 base 的值,否則返回零。

[編輯] 注意

POSIX 函式 clock_gettime(CLOCK_REALTIME, ts) 也可用於填充一個包含自紀元以來時間的 timespec

[編輯] 示例

#include <stdio.h>
#include <time.h>
 
int main(void)
{
    struct timespec ts;
    timespec_get(&ts, TIME_UTC);
    char buff[100];
    strftime(buff, sizeof buff, "%D %T", gmtime(&ts.tv_sec));
    printf("Current time: %s.%09ld UTC\n", buff, ts.tv_nsec);
}

可能的輸出

Current time: 02/18/15 14:34:03.048508855 UTC

[編輯] 參考

  • C23 標準 (ISO/IEC 9899:2024)
  • 7.27.2.5 timespec_get 函式 (p: TBD)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.27.2.5 timespec_get 函式 (p: 286)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.27.2.5 timespec_get 函式 (p: 390)

[編輯] 參見

秒和納秒時間
(struct)[編輯]
根據給定時間基準返回日曆時間的解析度
(function) [編輯]
返回系統當前日曆時間,自紀元起的時間
(function) [編輯]
C++ 文件 用於 timespec_get