C++11 标准库实现 time2Str, Str2time

来源:互联网 发布:淘宝外包美工多少钱 编辑:程序博客网 时间:2024/04/29 10:00
#include <iostream>#include <ctime>#include <iomanip>#include <string>#include <sstream>#include <stdio.h>/** * \brief C++11 标准库实现 time2Str, Str2time * */int main(){    //    // time2Str    //    std::time_t tt{std::time(nullptr)};    std::ostringstream oss;    oss << std::put_time(std::localtime(&tt), "%Y-%m-%d %H:%M:%S");    std::string strTime{oss.str()};    std::cout << "strTime:" << strTime << '\n';    //    // str2time    //    std::tm mytm = {};    std::istringstream iss{strTime};    iss >> std::get_time(&mytm, "%Y-%m-%d %H:%M:%S");    printf("struct tm: %04d-%02d-%02d %02d:%02d:%02d\n",           mytm.tm_year+1900, mytm.tm_mon+1, mytm.tm_mday,           mytm.tm_hour, mytm.tm_min, mytm.tm_sec);    std::time_t tt2{std::mktime(&mytm)};    std::cout << "tt2:" << tt2 << '\n';}
0 0
原创粉丝点击