c++ 11 使用 chrono 获取当前时间, 他是从 1970.1.1 开始到现在的时间.
不多bb, 直接上代码
#include <iostream>
#include <chrono>
// #include <ratio>
int main()
{
std::cout << "time from cpp11" << std::endl;
{
// hours 小时 h
using namespace std::chrono;
// time_point_cast<>() 会向下取整
time_point<system_clock, hours> now_time_point = time_point_cast<hours>(system_clock::now());
std::cout << "hours now time_point: " << now_time_point.time_since_epoch().count() << std::endl;
// hours now time_point: 474032
// 会根据输入的 time_point 自动计算 time_t 比如这边就是直接把 小时数*3600 了 // 474032 * 3600 = 1706515200
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now_time_point);
std::cout << "hours now time_t: " << now_time_t << std::endl;
// hours now time_t: 1706515200
int time_int = static_cast<int>(now_time_t);
std::cout << "hours now int: " << time_int << std::endl;
// hours now int: 1706515200
}
{
// minutes 分钟 m
using namespace std::chrono;
time_point<system_clock, minutes> now_time_point = time_point_cast<minutes>(system_clock::now());
std::cout << "minutes now time_point: " << now_time_point.time_since_epoch().count() << std::endl;
// minutes now time_point: 28441939
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now_time_point);
std::cout << &