命名空间
变体
操作

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;
(since C++11)

将 t 转换为 std::time_t 类型。

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

内容

[edit] 参数

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

[edit] 返回值

表示 t 的 std::time_t 值。

[edit] 示例

以两种方式获取当前时间作为 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

[edit] 参见

[静态]
将 std::time_t 转换为系统时钟时间点
(public static member function) [编辑]