Commit a8c0ea37 authored by Cédric Traizet's avatar Cédric Traizet
Browse files

ENH: add exception handling in ReadFormattedDate

parent a0c6c0d4
Pipeline #9113 passed with stages
in 84 minutes and 4 seconds
......@@ -20,6 +20,7 @@
#include "otbDateTime.h"
#include "date.h"
#include "otbMissingMetadataException.h"
namespace otb
{
......@@ -29,8 +30,21 @@ namespace MetaData
TimePoint ReadFormattedDate(const std::string & dateStr, const std::string & format)
{
std::istringstream is(dateStr);
is.exceptions(std::istringstream::failbit | std::istringstream::badbit);
TimePoint tp;
tp.Read(is, format.c_str());
try
{
tp.Read(is, format.c_str());
}
catch (const std::istringstream::failure &)
{
otbGenericExceptionMacro(otb::MissingMetadataException,
<< "Cannot parse the input date "
<< dateStr
<< " with input format "
<< format);
}
return tp;
}
......
......@@ -31,6 +31,8 @@
#endif
#include "otbDateTime.h"
#include "otbMissingMetadataException.h"
BOOST_AUTO_TEST_CASE(Duration)
{
......@@ -61,6 +63,22 @@ BOOST_AUTO_TEST_CASE(TimePoint)
BOOST_TEST(date1 - date2 == otb::MetaData::Duration::Seconds(86400));
BOOST_TEST(date1 + otb::MetaData::Duration::Seconds(0.1) ==
otb::MetaData::ReadFormattedDate("2021-06-22T00:01:04.52578987"));
// Test Parsing with a different format
otb::MetaData::ReadFormattedDate("2021-06-22 00:01:04.42578987", "%Y-%m-%d %H:%M:%S");
// Test some invalid cases
BOOST_REQUIRE_THROW(otb::MetaData::ReadFormattedDate("2021-06-21 00:01:04.42578987"),
otb::MissingMetadataException);
BOOST_REQUIRE_THROW(otb::MetaData::ReadFormattedDate("2021-06-21"),
otb::MissingMetadataException);
BOOST_REQUIRE_THROW(otb::MetaData::ReadFormattedDate("2021-6-21 00:1:4.42578987"),
otb::MissingMetadataException);
}
BOOST_AUTO_TEST_CASE(TimePointPrecision)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment