public static member function
<chrono>

std::chrono::system_clock::to_time_t

static time_t to_time_t (const time_point& tp) noexcept;
转换为 time_t
tp 转换为其等效的 time_t 类型。

参数

tp
一个 time_point 值。
time_point是一个成员类型,定义为time_point<system_clock>.

返回值

tptime_t 等效值。
time_t 是在 <ctime> 头文件中定义的类型。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// system_clock::to_time_t
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

int main ()
{
  using namespace std::chrono;

  duration<int,std::ratio<60*60*24> > one_day (1);

  system_clock::time_point today = system_clock::now();
  system_clock::time_point tomorrow = today + one_day;

  time_t tt;

  tt = system_clock::to_time_t ( today );
  std::cout << "today is: " << ctime(&tt);

  tt = system_clock::to_time_t ( tomorrow );
  std::cout << "tomorrow will be: " << ctime(&tt);

  return 0;
}

可能的输出
today is: Wed May 30 12:38:06 2012
tomorrow will be: Thu May 31 12:38:06 2012


另见