boostorg / boostorg/date_time

Inconsistent '%f' behaviour between boost::posix_time::time_facet and boost::posix_time::time_input_facet

Open
#102 8 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
70
Forks
99
PR merge metrics
No merged PRs in 30d

Description

I'm trying to convert a boost::posix_time::ptime to a specific string format (extended ISO) and then back to a boost::posix_time::ptime.

Surprisingly, it looks like for boost::posix_time::time_facet %f means 000000 to 999999 (with no decimal separator). But for boost::posix_time::time_input_facet, it means .000000 to .999999 (with decimal separator).

int main( int argc, char** argv )
{
auto now = boost::posix_time::second_clock::local_time();

std::stringstream outStr;
{
boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
facet->format("%Y-%m-%dT%H:%M:%S.%f");
outStr.imbue(std::locale(std::locale::classic(), facet));
outStr << now;
}
std::cout << outStr.str() << std::endl;

{
static const std::string format = "%Y-%m-%dT%H:%M:%S.%f";
const std::locale loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet(format));
std::istringstream is(outStr.str());
is.imbue(loc);
boost::posix_time::ptime converted;
is >> converted;
std::cout << converted << std::endl;
}

{
static const std::string format = "%Y-%m-%dT%H:%M:%S%f";
const std::locale loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet(format));
std::istringstream is(outStr.str());
is.imbue(loc);
boost::posix_time::ptime converted;
is >> converted;
std::cout << converted << std::endl;
}

return 0;
}

This outputs:

2019-04-30T12:23:29.000000
not-a-date-time
2019-Apr-30 12:23:29

While I would expect:

2019-04-30T12:23:29.000000
2019-Apr-30 12:23:29
not-a-date-time

Note that using "%Y-%m-%dT%H:%M:%S%F" works just fine (but has different behaviour when time is not a decimal value)

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.