提升现在简单的人类可读日期时间

Blu*_*ron 5 c++ boost

任何人都知道如何从当前时间的提升到本地系统获得简单的日期格式?

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");
Run Code Online (Sandbox Code Playgroud)

我见过使用cout.imbue的例子,但我只想要一个简单的字符串.

Art*_*hur 6

你可以试试这段代码:

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();
  }
Run Code Online (Sandbox Code Playgroud)

将格式设置为"%d-%m-%Y %H:%M:%S"您想要的或任何方面.

对于本地时间,使用boost::posix_time::second_clock::local_time()第二个参数(date_time).