boostorg / boostorg/date_time

boost::posix_time::time_input_facet accepting wrong date format

Open
#184 1 comment 3 reactions 1 assignee Claimed by @JeffGarland View on GitHub
Dominant language
C++
Stars
70
Forks
99
PR merge metrics
No merged PRs in 30d

Description

Hello,

(boost version: 1.68.0)

While using the boost::posix_time::time_input_facet to read a custom format string, the read completes with a totally invalid string:

E.g.:
```cpp
boost::posix_time::ptime fromISO8601ToPtime(const std::string& iISO8601Str) noexcept try {
std::stringstream ss;
ss.exceptions(std::ios_base::failbit);
ss.imbue(std::locale(std::locale::classic(), new boost::posix_time::time_facet("%Y-%m-%dT%H:%M:%SZ")));
ss.imbue(std::locale(ss.getloc(), new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ")));
boost::posix_time::ptime aTimeToReturn;
ss << iISO8601Str;
ss >> aTimeToReturn;
return aTimeToReturn;
}
catch (std::exception& e) {
// default constructor creates a non-valid ptime
return boost::posix_time::ptime();
}
catch (...) {
// default constructor creates a non-valid ptime
return boost::posix_time::ptime();
}
```

If I pass for instance the following **invalid** inputs to this method it will create a valid ptime:

"2020-12-30T25:52:28Z" -> invalid due to the hours (25 should not be valid), leads to ptime: 2020-Dec-31 01:52:28
"2020-12-30T16:62:28Z" -> minutes should be < 60, leads to ptime: 2020-Dec-30 17:02:28
"2020-12-30T16:59:75Z" -> seconds should be < 60, leads to ptime: 2020-Dec-30 17:00:15
"202001-04T23:52:28Z" -> does not conform with the facet in method, leads to ptime: 2020-Jan-04 23:52:28
"4323" -> no idea why this translates to a valid input, leads to ptime: 4323-Jan-01 00:00:00

Interestingly enough the time parameters are being "wrapped" while the same behavior is not verified for the date parameters: dates like "2020-1**3**-30T25:52:28Z" are deemed wrong and triggers the exception throwing. So here there is at least an inconsistency.

Could you please help me understand why and if there is an alternative to perform validation for such kind of custom input deserialization?

Best regards,
Rodrigo

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.