传统的C++获取时间的方法需要分平台来定义。相信百度代码也不少。
我自己写了下,如下。
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 ""; }
答案是肯定的。
很简单的,代码如下:
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); }
本文原创,如需转载,请说明出处:
http://blog.csdn.net/q229827701/article/details/41015483
C++11新特性,利用std::chrono精简传统获取系统时间的方法
原文地址:http://blog.csdn.net/q229827701/article/details/41015483