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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22975077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:17:28  来源:igfitidea点击:

How to convert a boost::ptime to string

c++boost

提问by user3517209

I'm having trouble converting a ptime object from boost into a string to be passed in to a function. I have found multiple similar other threads in regards to outputing a boost time object to a string (mostly to cout) but none of what I've found on them are working.

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

It appears the easiest way is inserting the ptime object into a stringstream and then using the stringstream's string. I have also attempted to imbue the stringstream with a time_facet, as some of the answers on other threads suggest. However, I am unable to create a time_facet object. It gives me the error that the argument list for the class template is missing. What is confusing is the nowhere on the internet have I found any mention of an argument list for time_facet, and even boost's documentation page shows that the default constructor for a time_facet is merely time_facet().

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

Below is a simple version of what I have tried:

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

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

The insertion of time into the stringstream gives me a bunch of compilation errors in the vein of

将时间插入字符串流给了我一堆编译错误

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
          ]

despite the fact that I haven't used any time_facet objects.

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

When I DO try to do this with a time_facet object, I add in

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

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

before inserting the time into the stringstream. The errors for that are that it wants an argument list as mentioned at the top of this post.

在将时间插入字符串流之前。错误在于它需要本文顶部提到的参数列表。

Is there perhaps a function in boost that is the reverse of boost::posix_time::time_from_string()? If not, any other help would be appreciated. Thank you.

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

回答by Tanner Sansbury

The Boost.Date_Time library provides the following ptimeto std::stringconversionswithin the boost::posix_timenamespace:

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

  • std::string to_simple_string(ptime)returns a string in the form of YYYY-mmm-DD HH:MM:SS.fffffffffformat where mmmis the three character month name.
  • std::string to_iso_string(ptime)returns a string in the form of YYYYMMDDTHHMMSS,fffffffffwhere Tis the date-time separator.
  • std::string to_iso_extended_string(ptime)returns a string in the form of YYYY-MM-DDTHH:MM:SS,fffffffffwhere Tis the date-time separator.
  • std::string to_simple_string(ptime)YYYY-mmm-DD HH:MM:SS.fffffffff格式形式返回一个字符串,其中mmm是三个字符的月份名称。
  • std::string to_iso_string(ptime)YYYYMMDDTHHMMSS,fffffffffwhereT是日期时间分隔符的形式返回一个字符串。
  • std::string to_iso_extended_string(ptime)YYYY-MM-DDTHH:MM:SS,fffffffffwhereT是日期时间分隔符的形式返回一个字符串。

Additionally, stream insertion and extraction operatorsare provided, allowing ptimeto be inserted or extracted from a stream. The input and output formats can be customized by constructing facets with various format flags, and then imbuing the stream with the facet.

此外,还提供了流插入和提取操作符,允许ptime从流中插入或提取。可以通过构造具有各种格式标志的构面,然后将构面注入流来自定义输入和输出格式。

Based on the compile error (C2220), the compiler is set to treat all warnings as errors. In some cases, the Boost libraries will compile with warnings. Consider assessing the severity of the actual warning, and handling it appropriately from there. For example, if the warning is trivial, it may be acceptable to use a warning pragmato disable or suppress the specific warning.

根据编译错误 ( C2220),编译器设置为将所有警告视为错误。在某些情况下,Boost 库会在编译时出现警告。考虑评估实际警告的严重性,并从那里适当地处理它。例如,如果警告是微不足道的,那么使用警告编译指示来禁用或抑制特定警告可能是可以接受的。



Here is a complete exampledemonstrating converting ptimeto a string via its provided conversion functions and stream operators.

这是一个完整的示例,演示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;
}

Which produces the following output:

产生以下输出:

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

回答by sfanjoy

My usage using release 1.55

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

Results:

结果:

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