Boost logo

Boost Users :

Subject: [Boost-users] Date Time I/O formatting question
From: Polder, Matthew J (matthew.j.polder_at_[hidden])
Date: 2009-07-13 13:19:11


I've found the Date Time library incredibly useful for dealing with timestamps, but I can't seem to get the output formatted the way I want. I've followed the example for the Date Time IO Tutorial (http://www.boost.org/doc/libs/1_39_0/doc/html/date_time/date_time_io.html#date_time.io_tutorial) but the output always comes out the same way. I've tried the following code with Visual Studio Express 2008 and g++ 4.2 on Solaris 10. I'm using Boost 1.39.0. Any help appreciated.

matthew

#include <fstream>
#include <functional>
#include <iostream>
#include <sstream>
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/local_time/local_time.hpp"

using namespace std;

int main(int argc, char** argv)
{
      stringstream ss;

      ifstream in( argv[1] );

      string inputFormat = argv[2];
      string outputFormat = argv[3]; //I want my own formatting.

      boost::local_time::local_time_facet* output_facet =
            new boost::local_time::local_time_facet();

      boost::local_time::local_time_input_facet* input_facet =
            new boost::local_time::local_time_input_facet();

      ss.imbue(locale(locale::classic(), output_facet));
      ss.imbue(locale(ss.getloc(), input_facet));

      output_facet->format( outputFormat.c_str() );
      input_facet->format( inputFormat.c_str() );

      while ( !in.eof() )
      {
            string s;
            getline( in, s );
            ss<<s;
            boost::posix_time::ptime p;
            ss>>p; //This works fine.

            string s1 = boost::posix_time::to_simple_string(p);

            cout<<s1<<"\n";

            ss.str("");
            ss<<p; //I don't get my format.
            string s2 = ss.str();

            cout<<s2<<"\n";

            std::ws(in);
            ss.str("");
      }
}

----Input File----
05:01:2007_23:08:02
06:01:2007_23:09:04
07:01:2007_16:32:58
07:01:2007_18:51:42
07:01:2007_19:51:27

----Output----
2007-Jan-05 23:08:02
2007-Jan-05 23:08:02
2007-Jan-06 23:09:04
2007-Jan-06 23:09:04
2007-Jan-07 16:32:58
2007-Jan-07 16:32:58
2007-Jan-07 18:51:42
2007-Jan-07 18:51:42
2007-Jan-07 19:51:27
2007-Jan-07 19:51:27



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