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

How to convert a boost::ptime to string

本文关键字:转换 字符串 ptime boost      更新时间:2023-10-16

我在将一个ptime对象从boost转换为要传递给函数的字符串时遇到问题。我发现了多个类似的其他线程将boost时间对象输出到字符串(主要是输出到cout),但我在它们上发现的都不起作用。

似乎最简单的方法是将ptime对象插入字符串流中,然后使用字符串流的字符串。我还尝试在字符串流中注入time_facet,正如其他线程中的一些答案所建议的那样。但是,我无法创建time_facet对象。它给我的错误是,类模板的参数列表丢失了。令人困惑的是,我在互联网上找不到任何关于time_facet的参数列表,甚至boost的文档页面也显示,time_facet的默认构造函数只是time_face()。

下面是我尝试过的一个简单版本:

#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());

在字符串流中插入时间给了我一堆类似的编译错误

error C2220: warning treated as error - no 'object' file generated C:codetrunkDevelopmentExternalboostincludeboost/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
          ]

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

当我尝试使用time_facet对象时,我会添加

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

在将时间插入字符串流之前。错误在于它想要一个如本文顶部所述的参数列表。

boost中是否存在与boost::posix_time::time_from_string()相反的函数?如果没有,我们将不胜感激。非常感谢。

Boost.Date_Time库在boost::posix_time命名空间中提供以下ptimestd::string转换:

  • std::string to_simple_string(ptime)返回YYYY-mmm-DD HH:MM:SS.fffffffff格式的字符串,其中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。可以通过构造具有各种格式标志的facet,然后将facet注入流来定制输入和输出格式。

根据编译错误(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;
}

它产生以下输出:

1981-Aug-20 08:05:00    
1981-Aug-20 08:05:00    
08:05

我使用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();
}

结果:

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