timespec_get
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <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_get の引数 base として使用するのに適した値に展開されます。追加のタイムベースを表す TIME_ で始まる他のマクロ定数が処理系によって提供される場合があります。
base が TIME_UTC の場合、
- ts->tv_sec は処理系定義のエポックからの秒数に設定されます。端数は切り捨てられます。
- ts->tv_nsec はシステムクロックの分解能に丸められたナノ秒に設定されます。
引数
| ts | - | struct timespec 型のオブジェクトへのポインタ
|
| base | - | TIME_UTC またはタイムベースを表すその他の非ゼロな整数値
|
戻り値
成功した場合は base の値、そうでなければゼロ。
ノート
POSIX の関数 clock_gettime(CLOCK_REALTIME, ts) もエポックからの時間を timespec に取得するために使用することができます。
例
Run this code
#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
参考文献
- C11 standard (ISO/IEC 9899:2011):
- 7.27.2.5 The timespec_get function (p: 390)
関連項目
(C11) |
秒とナノ秒で表した時間 (構造体) |
| エポックからの経過時間で表したシステムの現在時刻を返します (関数) | |
timespec_get の C++リファレンス
| |