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