std::timespec_get
来自cppreference.com
工具库
日期和时间工具
| 时间点 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 时长 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 时钟 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 当天时刻 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 日历 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 时区 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
chrono I/O |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++20)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C 风格日期和时间 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
在标头
<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 <cstdio> #include <ctime> int main() { std::timespec ts; std::timespec_get(&ts, TIME_UTC); char buff[100]; std::strftime(buff, sizeof buff, "%D %T", std::gmtime(&ts.tv_sec)); std::printf("Current time: %s.%09ld UTC\n", buff, ts.tv_nsec); }
可能的输出:
Current time: 06/24/16 20:07:42.949494132 UTC
参阅
|
(C++17)
|
以秒和纳秒表示的时间 (结构体) |
| 返回自纪元起计的系统当前时间 (函数) |