Opened 10 years ago

#7715 new Bugs

Fractional seconds are parsed as timezone

Reported by: knight666@… Owned by: az_sw_dude
Milestone: To Be Determined Component: date_time
Version: Boost 1.52.0 Severity: Problem
Keywords: date_time, timezone, fractional seconds, facet, flags Cc:

Description

We're trying to use Boost's excellent date time functionality, but we've hit a snag. The formatting we require is "%Y-%m-%dT%H:%M:%S.%F%ZP", which results in for example "2012-11-19T23:44:23.122344UTC+1". We use this to store dates and time in our database as a string.

The problem seems to be that the %F and %ZP flags don't work well together. The following code generates an exception:

std::string serialized = "2012-11-19T23:44:23.122344UTC+1";
std::stringstream ss(serialized);

std::string format = "%Y-%m-%dT%H:%M:%S.%F%ZP";

boost::local_time::local_time_facet* facet_output = new boost::local_time::local_time_facet();
facet_output->format(format.c_str());
ss.imbue(std::locale(std::locale::classic(), facet_output));

boost::local_time::local_time_input_facet* facet_input = new boost::local_time::local_time_input_facet();
facet_input->format(format.c_str());
ss.imbue(std::locale(ss.getloc(), facet_input));

boost::local_time::local_date_time result(boost::local_time::not_a_date_time);

ss >> result; // exception

What happens when you trace the stack to the exception is that the tokenizer doesn't see the "122344" bit as fractional seconds and instead thinks it's the timezone. This results in an invalid timezone and thus an exception.

We can circumvent the problem by removing the %F flag, but that is an unacceptable workaround, because we need the precision of the fractional seconds. Removing the %F flag solves the problem.

The problem was found in 1.48.0, but has not yet been fixed in 1.52.0.

Change History (0)

Note: See TracTickets for help on using tickets.