std::chrono::system_clock::to_time_t
来自cppreference.com
| |
(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
参阅
[静态] |
转换 std::time_t 到系统时钟时间点 (公开静态成员函数) |