时间处理函数

// 虽然std::localtime 函数返回一个tm结构的指针,但是从Windows 的实现来看,它是对每一个线程都创建一个单独的对象,所以这个函数应该是线程安全的。

 1 std::string get_current_datetime(const char *fmt = "%Y-%m-%d %H:%M:%S")
 2 {
 3     auto now = std::chrono::system_clock::now();
 4     time_t now_c = std::chrono::system_clock::to_time_t(now);
 5     char mbstr[256] = { 0 };
 6     if (std::strftime(mbstr, sizeof(mbstr), fmt, std::localtime(&now_c)))
 7         return std::string { mbstr };
 8 
 9     return std::string;
10 }

 

 

 

 

 

 

 

=========================================================================================================

posted on 2017-09-14 10:25  独孤酷酷  阅读(173)  评论(0编辑  收藏  举报