当前本地时间高精度

 1 #include <chrono>
 2 void current_time()
 3 {
 4     // 当前时间点
 5     std::chrono::system_clock::time_point t_now =
 6         std::chrono::high_resolution_clock::now();
 7     // 转换精度到微秒级别
 8     std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds> t_n = 
 9         std::chrono::time_point_cast<std::chrono::microseconds>(t_now);
10 
11     time_t nt = std::chrono::system_clock::to_time_t(t_now);
12     struct tm btm;
13 #ifdef _WIN32
14     localtime_s(&btm, &nt);
15 #else
16     localtime_r(&nt, &btm);
17 #endif
18 
19     char mbstr[256] = { 0 };
20     strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %H:%M:%S", &btm);
21     printf("%s.%06" PRIu64 "\n", mbstr, t_n.time_since_epoch() % 1000000);
22 }

 

posted on 2018-05-23 13:47  独孤酷酷  阅读(234)  评论(0编辑  收藏  举报