如何将boost :: ptime转换为字符串 [英] How to convert a boost::ptime to string

查看:628
本文介绍了如何将boost :: ptime转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将ptime对象从boost转换为要传递给函数的字符串时遇到了麻烦.我发现有多个类似的其他线程可以将增强时间对象输出到字符串(主要是输出到cout),但是我发现它们都没有起作用.

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对象插入字符串流,然后使用字符串流的字符串.正如其他线程上的一些答案所暗示的那样,我也曾尝试给timestfacet注入字符串流.但是,我无法创建一个time_facet对象.它给我一个错误,即类模板的参数列表丢失.令人困惑的是,在互联网上找不到我提到过time_facet的参数列表的地方,甚至boost的文档页面也显示time_facet的默认构造函数仅仅是time_facet().

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().

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

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
          ]

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

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

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.

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

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.Date_Time库提供以下

The Boost.Date_Time library provides the following ptime to std::string conversions within the boost::posix_time namespace:

  • 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是日期时间分隔符.
  • std::string to_simple_string(ptime) returns a string in the form of YYYY-mmm-DD HH:MM:SS.fffffffff format where mmm is the three character month name.
  • std::string to_iso_string(ptime) returns a string in the form of YYYYMMDDTHHMMSS,fffffffff where T is the date-time separator.
  • std::string to_iso_extended_string(ptime) returns a string in the form of YYYY-MM-DDTHH:MM:SS,fffffffff where T is the date-time separator.

此外,提供了流插入和提取 operators ,允许插入ptime或从流中提取.可以通过构造具有各种格式的构面来定制输入和输出格式.标记,然后在信息流中注入构面.

Additionally, stream insertion and extraction operators are provided, allowing ptime to 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.

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

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 pragma to disable or suppress the specific warning.

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

Here is a complete example demonstrating converting ptime to a string via its provided conversion functions and stream operators.

#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

这篇关于如何将boost :: ptime转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆