Namespaces
Variants

std::chrono::system_clock:: to_time_t

From cppreference.net
static std:: time_t to_time_t ( const time_point & t ) noexcept ;
(C++11 起)

t 转换为 std::time_t 类型。

如果 std::time_t 的精度较低,则值是舍入还是截断由实现定义。

目录

参数

t - 待转换的系统时钟时间点

返回值

一个表示 t std::time_t 值。

示例

以两种方式获取当前时间作为 std::time_t

#include <chrono>
#include <ctime>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;
int main()
{
    // 传统方式
    std::time_t oldt = std::time({});
    std::this_thread::sleep_for(2700ms);
    // 现代方式
    auto const now = std::chrono::system_clock::now();
    std::time_t newt = std::chrono::system_clock::to_time_t(now);
    std::cout << "newt - oldt == " << newt - oldt << " s\n";
}

可能的输出:

newt - oldt == 3 s

参见

[static]
std::time_t 转换为系统时钟时间点
(公开静态成员函数)