Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Main Repositories
otb
Commits
a8c0ea37
Commit
a8c0ea37
authored
Nov 15, 2021
by
Cédric Traizet
Browse files
ENH: add exception handling in ReadFormattedDate
parent
a0c6c0d4
Pipeline
#9113
passed with stages
in 84 minutes and 4 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Modules/Core/Metadata/src/otbDateTime.cxx
View file @
a8c0ea37
...
...
@@ -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
;
}
...
...
Modules/Core/Metadata/test/otbDateTest.cxx
View file @
a8c0ea37
...
...
@@ -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
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment