解析错误输入时的c++ boost时间验证
c++ boost time validation when parsing a wrong input
我正在学习C++
并制作一个简单的应用程序,并且我有以下代码将std::string
解析为ptime
:
ptimePointer CsvHelper::string_to_ptime(const std::string& s, const int mode,
timePointer pt)
{
const std::locale formats[] = {
std::locale(std::locale::classic(),new boost::posix_time::time_input_facet("%d/%m/%Y H:%M:%S")),
std::locale(std::locale::classic(),new boost::posix_time::time_input_facet("%H:%M"))
};
std::istringstream is(s);
is.imbue(formats[mode]);
is >> *pt;
}
return pt;
问题是,如果我传递一个字母"a"
,它将被创建一个具有特殊值"not-a-date-time"的ptime
。
好的,所以我检查了文档,并了解了如果我用默认构造函数创建ptime
,我会得到一个具有相同类型的ptime
。因此,在我的代码中,我用以下代码验证了输入:
boost::posix_time::ptime ptime;
if (*this->myDate != ptime)
{
...
} else
{
treat the problem;
}
我觉得这不是一个明确的解决方案,有什么更好的方法来验证这个日期?
ptime有一个is_not_a_date_time()函数。你可以输入
if(myDate.is_not_a_date_time())
do_something();
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- Boost Spirit,获取迭代器内部语义动作
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 使用Boost Interprocess创建托管共享内存需要很长时间
- Boost::posix_time::ptime舍入到给定的分钟数
- boost xml parsingl将xml的路径作为变量发送