Increase the ease of human reading time

Does anyone know how to get a simple date format from raising the current time to the local system?

boost::posix_time::ptime now = boost::posix_time::second_clock::universal_time();
boost::posix_time::time_facet *facet = new boost::posix_time::time_facet("%d-%m-%Y %H:%M:%S");

I have seen examples that say use cout.imbue, but I just want a simple string.

+5
source share
2 answers

you can try this code:

void FormatDateTime(
    std::string const&              format,
    boost::posix_time::ptime const& date_time,
    std::string&                    result)
  {
    boost::posix_time::time_facet * facet =
      new boost::posix_time::time_facet(format.c_str());
    std::ostringstream stream;
    stream.imbue(std::locale(stream.getloc(), facet));
    stream << date_time;
    result = stream.str();
  }

Set the format "%d-%m-%Y %H:%M:%S"or any other face you want.

For local time, use boost::posix_time::second_clock::local_time()( date_time) as the second argument .

+6
source

I know this too late, but for users like me:

#include <boost/format.hpp>

const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();

const boost::wformat f = boost::wformat(L"%02d.%02d.%s  %02d:%02d")
                % now.date().year_month_day().dayas_number()
                % now.date().year_month_day().month.as_number()
                % now.date().year_month_day().year
                % now.time_of_day().hours()
                % now.time_of_day().minutes();

const std::wstring result = f.str();  // "21.06.2013  14:38"
+3
source

All Articles