Get the local time of the current system, accurate to milliseconds, get the current millisecond
1 #include <sys/timeb.h> 2 #include <chrono> 3 4 char* cur_time_c(char strDateTime[32]) 5 { 6 struct timeb tp_cur; 7 ftime(&tp_cur); 8 9 struct tm btm;10 11 #ifdef WIN3212 localtime_s(&btm, &tp_cur.time);13 #else14 localtime_r(&tp_cur.time, &btm);15 #endif16 17 sprintf(strDateTime, "%04d-%02d-%02d %02d:%02d:%02d.%03d", 18 btm.tm_year + 1900, btm.tm_mon + 1, btm.tm_mday,19 btm.tm_hour, btm.tm_min, btm.tm_sec, tp_cur.millitm);20 21 return strDateTime;22 }23 24 25 char* cur_time_cpp11(char strDateTime[32])26 {27 static const std::chrono::hours = EIGHT_HOURS(8);28 29 auto nowLocalTimeCount 30 = std::chrono::system_clock::now().time_since_epoch()31 + EIGHT_HOURS;32 33 std::chrono::hours now_h34 = std::chrono::duration_cast<std::chrono::hours>(nowLocalTimeCount);35 std::chrono::minutes now_m36 = std::chrono::duration_cast<std::chrono::minutes>(nowLocalTimeCount);37 std::chrono::seconds now_s38 = std::chrono::duration_cast<std::chrono::seconds>(nowLocalTimeCount);39 std::chrono::milliseconds now_ms40 = std::chrono::duration_cast<std::chrono::milliseconds>(nowLocalTimeCount);41 42 sprintf(strDateTime, "%02d:%02d:%02d.%03d", 43 now_h % 24, now_m % 60, now_s % 60, now_ms % 1000);44 45 return strDateTime;46 }