1. 程式人生 > >當前本地時間高精度

當前本地時間高精度

oid end include win point lock OS bsp RR

 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 }

當前本地時間高精度