Boost logo

Boost Users :

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


Commander Pirx wrote:

------------------------------------------

Would be interesting whats your format string looks like. Not all format flags work with all date_time classes. %z (Time zone) for instance works only with local_date_time.

------------------------------------------

It seems that no matter what formatting I try to use, it doesn't work. Example:

TimeConverter sampleFile.txt %d:%m:%Y_%H:%M:%S %m:%d:%Y_%H:%M:%S

sampleFile.txt:
05:01:2007_23:08:02
06:01:2007_23:09:04

expected output: (month and day positions switched)
2007-Jan-05 23:08:02
01:05:2007_23:08:02
2007-Jan-06 23:09:04
01:06:2007_23:09:04

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

Code is below

#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];

     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;

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

          cout<<s1<<"\n";

          ss.str("");
          ss<<p;
          string s2 = ss.str();

          cout<<s2<<"\n";

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



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