When using time_input_facet boost datetime posix_time fails to parse single digit day of month. Unbeknownst to me at the time of initial posting, https://github.com/boostorg/date_time/issues/106 describes this issue, as pointed out in the accepted answer.
#include <boost/date_time/posix_time/posix_time.hpp>
#include <vector>
void date_from_string(const std::string& format, const std::string& date_str, boost::posix_time::ptime& date) {
auto* facet = new boost::posix_time::time_input_facet;
facet->format(format.c_str());
std::stringstream ss(date_str);
ss.imbue(std::locale(std::locale::classic(), facet));
ss >> date;
// delete facet; > "Your program has UB. delete facet; leads to double-free."
}
int main() {
std::cout << "Boost Version: " << BOOST_VERSION << std::endl; // 107500
// Boost Version: 107500
for (auto& i : std::vector<std::pair<std::string,std::string>>{
{"%a, %d %b %Y %H:%M:%S %ZP","Thu, 17 Oct 2021 15:00:00 GMT"},
// 2021-Oct-17 15:00:00 from str: Thu, 17 Oct 2021 15:00:00 GMT using: %a, %d %b %Y %H:%M:%S %ZP
{"%a, %d %b %Y %H:%M:%S %ZP","Thu, 7 Oct 2021 15:00:00 GMT"},
// not-a-date-time from str: Thu, 7 Oct 2021 15:00:00 GMT using: %a, %d %b %Y %H:%M:%S %ZP
{"%d", "7"},
// not-a-date-time from str: 7 using: %d
{"%d", "07"}
// 1400-Jan-07 00:00:00 from str: 07 using: %d
}) {
boost::posix_time::ptime m_date;
date_from_string(i.first, i.second,m_date);
std::cout << boost::posix_time::to_simple_string(m_date) << " from str: " << i.second << " using: " << i.first << std::endl;
}
}
%d Day of the month as decimal 01 to 31. When used to parse input, the leading zero is optional. (emphasis mine)
https://www.boost.org/doc/libs/1_75_0/doc/html/date_time/date_time_io.html (format flags)