std::chrono::system_clock::to_time_t

来自cppreference.com
< cpp‎ | chrono‎ | system clock
 
 
 
 
static std::time_t to_time_t( const time_point& t ) noexcept;
(C++11 起)

转换 tstd::time_t 类型。

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

目录

[编辑] 参数

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

[编辑] 返回值

表示 tstd::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

[编辑] 参阅

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