C++11 new features, using Std::chrono to streamline the traditional method of acquiring system time

Source: Internet
Author: User
Tags sprintf

The traditional method of acquiring system time

The traditional method of acquiring time in C + + needs to be defined by platform. Believe that Baidu code is also a lot.

I wrote it myself, as follows.

Const std::string Getcurrentsystemtime () {if (Platform_android | | Platform_ios) {struct Timeval s_now;struct tm* p_tm;gettimeofday (&s_now,null);p _tm = localtime ((const time_t*) &S_NOW.TV_SEC); char date[60] = {0};sprintf (date, "%d-%02d-%02d      %02d:%02d:%02d", (int) p_tm->tm_year + 1900, (int) P_tm->tm_mon + 1, (int) p_tm->tm_mday, (int) p_tm->tm_hour, (int) p_tm->tm_min, (int) p_tm->tm_sec); return std::string (date);} if (platform_w32) {struct tm* p_tm;time_t timep;time (&TIMEP);p _tm = localtime (&TIMEP); char date[60] = {0}; sprintf (date, "%d-%02d-%02d      %02d:%02d:%02d", (int) p_tm->tm_year + 1900, (int) P_tm->tm_mon + 1, (int) p_tm- >tm_mday, (int) p_tm->tm_hour, (int) p_tm->tm_min, (int) p_tm->tm_sec); Log ("%s", date); return std::string ( date);} Return "";}


Second, C++11 STD standard library cross-platform approach obviously, we notice that the code similarity is very high in different platforms, so can we use the new features inside the c++11 to make them merge?

The answer is yes.

Very simple, the code is as follows:

Const std::string getcurrentsystemtime () {Auto TT = std::chrono::system_clock::to_time_t (std::chrono::system_clock:: Now ()); struct tm* ptm = localtime (&TT); char date[60] = {0};sprintf (date, "%d-%02d-%02d      %02d:%02d:%02d", (int) Ptm->tm_year + 1900, (int) Ptm->tm_mon + 1, (int) ptm->tm_mday, (int) ptm->tm_hour, (int) ptm->tm_min, (int) PTM->TM_SEC); return std::string (date);}

It's short and simple with wood.

This article original, if need to reprint, please explain the source:

http://blog.csdn.net/q229827701/article/details/41015483

C++11 new features, using Std::chrono to streamline the traditional method of acquiring system time

Related Article

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.