0

I have found several questions already on Stack Overflow, using various stringstreams and some weird methods that aren't really documented properly. (Apparently there doesn't seem to be a standard way of doing this either? )

Is there really no easy (5 lines or less) way to format a boost::chrono::system_time::now() into a custom Date/Time std::string?

Jeroen
  • 15,257
  • 12
  • 59
  • 102
  • Have you tried C++11 [`std::chrono`](http://en.cppreference.com/w/cpp/chrono)? On the other hand, it doesn't have standard functions to format datetime (I refer to C++ style, you have to use C posix functions). I'm waiting for [`std::chrono_io`](http://home.roadrunner.com/~hinnant/bloomington/chrono_io.html). – Manu343726 Oct 06 '13 at 12:05
  • 1
    Also Boost provides its `boost::chrono_io` along with the [`boost::chrono`](http://www.boost.org/doc/libs/1_54_0/doc/html/chrono.html) library – Manu343726 Oct 06 '13 at 12:07
  • @Manu343726 There doesn't seem to be any documentation nor actual code using this in existent. (Cannot even find proper documentation embedded in the boost headers) – Jeroen Oct 06 '13 at 12:36

1 Answers1

0

Yes there is, you can use ctime to convert a time point into calendar notation.

std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::string ts = std::ctime(&t);

Will return Sun Oct 06 13:55:05 2013

Duncan Smith
  • 530
  • 2
  • 10
  • But how to custom format that time? Like what if I wanted it to be something like "YYYY-MM-DD HH-MM-SS"? – Jeroen Oct 06 '13 at 12:58
  • Use it with the boost datetime format library, see here: http://stackoverflow.com/questions/5018188/how-to-format-a-datetime-to-string-using-boost – Duncan Smith Oct 06 '13 at 13:03