Current date and time as string

后端 未结 6 1875
梦毁少年i
梦毁少年i 2020-12-04 11:09

I wrote a function to get a current date and time in format: DD-MM-YYYY HH:MM:SS. It works but let\'s say, its pretty ugly. How can I do exactly the sam

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 11:38

    I wanted to use the C++11 answer, but I could not because GCC 4.9 does not support std::put_time.

    std::put_time implementation status in GCC?

    I ended up using some C++11 to slightly improve the non-C++11 answer. For those that can't use GCC 5, but would still like some C++11 in their date/time format:

     std::array buffer;
     buffer.fill(0);
     time_t rawtime;
     time(&rawtime);
     const auto timeinfo = localtime(&rawtime);
     strftime(buffer.data(), sizeof(buffer), "%d-%m-%Y %H-%M-%S", timeinfo);
     std::string timeStr(buffer.data());
    

提交回复
热议问题