命名空间
变体
操作

std::chrono::system_clock::to_time_t

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

t 转换为 std::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()
{
    // The old way
    std::time_t oldt = std::time({});
 
    std::this_thread::sleep_for(2700ms);
 
    // The new way
    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 转换为系统时钟时间点
(public static member function) [编辑]