如何将boost :: ptime转换为字符串

use*_*209 14 c++ boost

我将ptime对象从boost转换为要传递给函数的字符串时遇到问题.我已经找到了多个类似的其他线程,关于将一个提升时间对象输出到一个字符串(主要是cout),但我发现它们都没有工作.

看起来最简单的方法是将ptime对象插入到字符串流中,然后使用stringstream的字符串.我还尝试用time_facet填充stringstream,正如其他线程上的一些答案所示.但是,我无法创建time_facet对象.它给了我一个错误,即缺少类模板的参数列表.令人困惑的是,互联网上没有任何地方提到time_facet的参数列表,甚至boost的文档页面都显示time_facet的默认构造函数只是time_facet().

以下是我尝试过的简单版本:

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

boost::posix_time::ptime time = boost::posix_time::time_from_string("1981-08-20 08:05:00"); 
std::stringstream sstream;
sstream << time;
_updateStatement->setString(1, (sql::SQLString)sstream.str());
Run Code Online (Sandbox Code Playgroud)

将时间插入到字符串流中会给我带来一堆编译错误

error C2220: warning treated as error - no 'object' file generated C:\code\trunk\Development\External\boost\include\boost/date_time/time_facet.hpp(247) :while compiling class template member function 'boost::date_time::time_facet<time_type,CharT>::time_facet(size_t)'
          with
          [
              time_type=boost::posix_time::ptime,
              CharT=char
          ]
Run Code Online (Sandbox Code Playgroud)

尽管我没有使用任何time_facet对象.

当我尝试使用time_facet对象执行此操作时,我添加了

sstream.imbue(std::locale(sstream.getloc(), new boost::date_time::time_facet("%Y-%m-%d %H:%M:%S")));
Run Code Online (Sandbox Code Playgroud)

在将时间插入字符串流之前.这个错误是它需要一个参数列表,如本文顶部所述.

在boost中是否有一个与boost :: posix_time :: time_from_string()相反的函数?如果没有,任何其他帮助将不胜感激.谢谢.

Tan*_*ury 23

该的Boost.Date_Time库提供以下ptime,以std::string转换的内部boost::posix_time命名空间:

  • std::string to_simple_string(ptime)YYYY-mmm-DD HH:MM:SS.fffffffffformat格式返回一个字符串,其中mmm是三个字符的月份名称.
  • std::string to_iso_string(ptime)返回形式的串YYYYMMDDTHHMMSS,fffffffff,其中T是日期时间分隔符.
  • std::string to_iso_extended_string(ptime)返回形式的串YYYY-MM-DDTHH:MM:SS,fffffffff,其中T是日期时间分隔符.

另外,提供了流插入和提取操作符,允许ptime从流中插入或提取.可以通过构造具有各种格式标志的构面来定制输入和输出格式,然后使用构面填充流.

基于编译错误(C2220),编译器设置为将所有警告视为错误.在某些情况下,Boost库将使用警告进行编译.考虑评估实际警告的严重性,并从那里适当地处理它.例如,如果警告是微不足道的,则可以使用警告编译指示来禁用或禁止特定警告.


这是一个完整的示例,演示ptime通过其提供的转换函数和流运算符转换为字符串.

#include <iostream>
#include <locale>
#include <string>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

int main()
{
  const boost::posix_time::ptime time = 
      boost::posix_time::time_from_string("1981-08-20 08:05:00");

  // ptime to string.
  const std::string str_time = to_simple_string(time);
  std::cout << str_time << std::endl;

  // ptime to stringstream to string.
  std::stringstream stream;
  stream << time;
  std::cout << stream.str() << std::endl;
  stream.str("");

  // Use a facet to display time in a custom format (only hour and minutes).
  boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
  facet->format("%H:%M");
  stream.imbue(std::locale(std::locale::classic(), facet));
  stream << time;
  std::cout << stream.str() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

其中产生以下输出:

1981-Aug-20 08:05:00    
1981-Aug-20 08:05:00    
08:05
Run Code Online (Sandbox Code Playgroud)

  • 对于那些对为什么有原始“新方面”而没有“删除”感兴趣的人:/sf/ask/1244576231/ (2认同)

小智 6

我使用1.55版的用法

#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main()
{
  boost::gregorian::date dayte(boost::gregorian::day_clock::local_day());
  boost::posix_time::ptime midnight(dayte);
  boost::posix_time::ptime 
     now(boost::posix_time::microsec_clock::local_time());
  boost::posix_time::time_duration td = now - midnight;

  std::stringstream sstream;

  std::cout << dayte << std::endl;
  std::cout << dayte.year() << "/" << dayte.month().as_number()
   << "/" << dayte.day() << std::endl;
  std::cout << now << std::endl;
  std::cout << td << std::endl;
  std::cout << td.hours() << "/" << td.minutes() << "/"
     << td.seconds() << "/" << td.fractional_seconds() << std::endl;

  sstream << dayte << std::endl;
  sstream << dayte.year() << "/" << dayte.month().as_number()
     << "/" << dayte.day() << std::endl;
  sstream << now << std::endl;
  sstream << td << std::endl;
  sstream << td.hours() << "/" << td.minutes() << "/" << td.seconds()
    << "/" << td.fractional_seconds() << std::endl;

  std::cout << sstream.str();
}
Run Code Online (Sandbox Code Playgroud)

结果:

2015-Oct-27
2015/10/27
2015-Oct-27 14:25:18.614684
14:25:18.614684
14/25/18/614684
2015-Oct-27
2015/10/27
2015-Oct-27 14:25:18.614684
14:25:18.614684
Run Code Online (Sandbox Code Playgroud)