【问题标题】:How to convert a boost::ptime to string如何将 boost::ptime 转换为字符串
【发布时间】:2014-04-09 22:21:24
【问题描述】:

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

看来最简单的方法是将 ptime 对象插入到字符串流中,然后使用字符串流的字符串。正如其他线程上的一些答案所暗示的那样,我还尝试为字符串流注入 time_facet。但是,我无法创建 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());

在字符串流中插入时间给了我一堆类似于

的编译错误
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 对象。

当我尝试使用 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() 相反?如果没有,任何其他帮助将不胜感激。谢谢。

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    Boost.Date_Time 库在 boost::posix_time 命名空间内提供以下 ptime to std::string conversions

    • 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 是日期时间分隔符。

    此外,还提供了流插入和提取operators,允许从流中插入或提取ptime。可以通过使用各种format flags 构造构面,然后用构面填充流来自定义输入和输出格式。

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


    这是一个complete example 演示通过其提供的转换函数和流运算符将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
    

    【讨论】:

    • 是的,我们的项目设置为将所有警告视为错误。虽然我仍然不确定为什么将时间插入流以及为什么创建 time_facets 在我的项目中不起作用(除非这些总是给出警告),但时间到字符串的转换函数似乎对我有用。非常感谢!
    • 有没有办法在时间的最后加上一个“Z”?我试过 "%Y-%m-%dT%H%M%SZ" 但我得到的只是 "YYYY-mm-ddTHH:MM:SS" 而最后没有 Z
    • 对于那些对为什么有原始new facet 而没有delete 感兴趣的人:stackoverflow.com/questions/17779660/…
    • ptime to stringstream to string 对我有用。 ptime 到字符串不起作用。
    • to_simple_string 位于哪个标头和namespace 中?
    【解决方案2】:

    我使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      相关资源
      最近更新 更多