Boost logo

Boost Users :

From: LABELLE, Maxime (mlabelle_at_[hidden])
Date: 2006-06-07 03:38:47


I'm experiencing a problem using custom formatting of fractional seconds.

const std::string date_string = "2006 Jun 01 999000 23.59.59";
const std::string format_string = "%Y %b %d %f %H.%M.%S";

When using a time_facet together with the "%f" flag, I get a correct output
containing a 6-digit number in place of the fractional part.

However, using a time_input_facet together with the "%f" flag, I cannot get
a correct ptime from an input using the same format as the above example.

The problem is twofold :

1) First, the input facet expects a leading '.'
   otherwise, it parses a special value and fails.
   [see time_facet.hpp(1035-1049)]
   This is not the case in an output facet.

2) Second, even if I include a leading dot, the space
   after the fractional seconds is not consumed, and
   the parsing of the remainder fails.
   This means that I need to add an extra space to the
   input format string.

Here is a sample program:

---------
#include <cassert>
#include <boost/posix_time/posix_time.hpp>
Using namespace boost;
...
        posix_time::ptime time;

        const std::string input_format = "%Y %b %d %f %H.%M.%S"; //
formats are
        const std::string output_format = "%Y %b %d .%f %H.%M.%S"; // not
symmetric

        const std::string input_date_string = "2006 Jun 01 .999000
23.59.59";
        std::string output_date_string;

        {
                posix_time::time_input_facet* input_facet =
                        new
posix_time::time_input_facet(input_format.c_str());

                std::istringstream in(input_date_string);
                in.imbue(std::locale(std::locale::classic(), input_facet));
                in >> time;
        }

        {
                posix_time::time_facet* output_facet =
                        new posix_time::time_facet(output_format.c_str());

                std::ostringstream out;
                out.imbue(std::locale(std::locale::classic(),
output_facet));
                out << time;

                output_date_string = out.str();
        }

        assert( output_date_string == input_date_string );
---------

Am I missing something ?
Thanks for your help.
Maxime LABELLE.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net