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 << "minutes now time_t: " << now_time_t << std::endl;
// minutes now time_t: 1706516340
int time_int = static_cast<int>(now_time_t);
std::cout << "minutes now int: " << time_int << std::endl;
// minutes now int: 1706516340
}
{
// 直接用 system_clock::now() 取到的是 纳秒 从 1970年1月1日 开始算到当前为止的纳秒 有点nb
std::chrono::system_clock::time_point now_time_point = std::chrono::system_clock::now();
std::cout << "seconds now time_point: " << now_time_point.time_since_epoch().count() << std::endl;
// std::cout << "seconds now time_point: " << now_time_point.time_since_epoch().max() << std::endl;
// seconds now time_point: 1706513126665858590
// long 可以存下 纳秒
long time_long = static_cast<long>(now_time_point.time_since_epoch().count());
std::cout << "seconds now long: " << time_long << std::endl;
// seconds now long: 1706513126665858590
// 会转化为 秒
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now_time_point);
std::cout << "seconds now time_t: " << now_time_t << std::endl;
// seconds now time_t: 1706513126
int time_int = static_cast<int>(now_time_t);
std::cout << "seconds now int: " << time_int << std::endl;
// seconds now int: 1706513126
// 不能使用 int 直接去接 time_point.time_since_epoch().count() 的值, 因为会截取从后往前数 32 bit的值
time_int = static_cast<int>(now_time_point.time_since_epoch().count());
std::cout << "seconds now int: " << time_int << std::endl;
// seconds now int: -1351242210
std::cout << "int max: " << INT32_MAX << std::endl;
// int max: 2147483647
// 或者麻烦点
using namespace std::chrono;
time_point<system_clock, seconds> now_time_point_s = time_point_cast<seconds>(system_clock::now());
std::cout << "seconds now time_point: " << now_time_point_s.time_since_epoch().count() << std::endl;
}
{
// 毫秒时间获取 milliseconds ms
// std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> now_time_point
// = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
// time_point_cast<单位>(数值) 作用就是计算 当前 '数值' 有多少个 '单位'
using namespace std::chrono;
time_point<system_clock, milliseconds> now_time_point = time_point_cast<milliseconds>(system_clock::now());
std::cout << "milliseconds now time_point: " << now_time_point.time_since_epoch().count() << std::endl;
// milliseconds now time_point: 1706513126665
long time_long = static_cast<long>(now_time_point.time_since_epoch().count());
std::cout << "milliseconds now long: " << time_long << std::endl;
// milliseconds now long: 1706513126665
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now_time_point);
std::cout << "milliseconds now time_t: " << now_time_t << std::endl;
// milliseconds now time_t: 1706513606
// int 就能存下 秒, 别的都白费
int time_int = static_cast<int>(now_time_t);
std::cout << "milliseconds now int: " << time_int << std::endl;
// milliseconds now int: 1706514080
}
{
// 微秒 microseconds us
using namespace std::chrono;
time_point<system_clock, microseconds> now_time_point = time_point_cast<microseconds>(system_clock::now());
std::cout << "microseconds now time_point: " << now_time_point.time_since_epoch().count() << std::endl;
// microseconds now time_point: 1706516388030998
long time_long = static_cast<long>(now_time_point.time_since_epoch().count());
std::cout << "microseconds now long: " << time_long << std::endl;
// microseconds now long: 1706516388030998
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now_time_point);
std::cout << "microseconds now time_t: " << now_time_t << std::endl;
// microseconds now time_t: 1706516388
}
{
// 纳秒 nanoseconds us
using namespace std::chrono;
// 来来来 脱裤子放个屁
time_point<system_clock, nanoseconds> now_time_point = time_point_cast<nanoseconds>(system_clock::now());
std::cout << "nanoseconds now time_point: " << now_time_point.time_since_epoch().count() << std::endl;
// nanoseconds now time_point: 1706516388031017227
long time_long = static_cast<long>(now_time_point.time_since_epoch().count());
std::cout << "nanoseconds now long: " << time_long << std::endl;
// nanoseconds now long: 1706516388031017227
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now_time_point);
std::cout << "nanoseconds now time_t: " << now_time_t << std::endl;
// nanoseconds now time_t: 1706516388
}
return 0;
}
日志输出
/************* log *************
time from cpp11
hours now time_point: 474032
hours now time_t: 1706515200
hours now int: 1706515200
minutes now time_point: 28441939
minutes now time_t: 1706516340
minutes now int: 1706516340
seconds now time_point: 1706516388030928867
seconds now long: 1706516388030928867
seconds now time_t: 1706516388
seconds now int: 1706516388
seconds now int: 133650403
int max: 2147483647
seconds now time_point: 1706516388
milliseconds now time_point: 1706516388030
milliseconds now long: 1706516388030
milliseconds now time_t: 1706516388
milliseconds now int: 1706516388
microseconds now time_point: 1706516388030998
microseconds now long: 1706516388030998
microseconds now time_t: 1706516388
nanoseconds now time_point: 1706516388031017227
nanoseconds now long: 1706516388031017227
nanoseconds now time_t: 1706516388
*/