Get the local time of the current system, accurate to milliseconds, get the current millisecond

Source: Internet
Author: User

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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.