Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2003-06-26 14:56:18


On Thu, 26 Jun 2003 14:35:26 -0400, Philip Miller wrote
> I am trying out the boost 1.30.0 date_time library and am quite pleased
> with it. I just came across a "feature" on which I would appreciate
> some help. There is an asymmetry when converting a ptime to/from a
> simple string. The to_simple_string(ptime) method will output
> 2002-Jan-01 10:00:01.123456789 but the time_from_string( const
> std::string& ) methhod will choke on "Jan". Is this a bug or a
> feature? I would like a symmetric pair of functions such that the
> following code works and t1 == t0.
>
> t1 = time_from_string( to_simple_string( t0 ) )
>
> I appreciate any feedback.

Well, I guess it is a feature :-(

Apparently there is no existing set of to_string/from functions that
offer this functionality directly. So in the short run your
options are:

1) write your own version of to_string which is compatible
2) Use operator<< with an appropriate date formmatting facet

Number 1 would be something like:
//WARNING -- never compiled or tested!!
std::string another_to_string(const boost::posix_time::ptime& t) {
   using boost::gregorian;
   date d = t.get_date();
   std::string s =
date_time::date_formatter<date,date_time::iso_extended_format>::date_to_string
(t.get_date());
   s = s + ' ' + boost::posix_time::to_iso_string(t.time_of_day());
   return s;
}
 
HTH,

Jeff
  }


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk