如何将boost::ptime转换为字符串
How to convert a boost::ptime to string
我在将一个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
命名空间中提供以下ptime
到std::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
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数