Boost logo

Boost :

From: Philip Miller (pwmiller_at_[hidden])
Date: 2003-07-10 16:52:32


I have a very similar problem. I have attached my message that I posted on
6/26/2003. I patched the date_time library so the default to/from simple string
methods use an integral month rather than the string month.

Phil

Stefan Seefeld wrote:

> Jeff Garland wrote:
> > On Wed, 09 Jul 2003 09:33:44 -0400, Stefan Seefeld wrote
> >
> >>hi there,
> >>
> >>what is the suggested way to persist a time_duration into a string ?
> >>I tried 'to_iso_string' but there is no corresponding
> >>'duration_from_iso_string'. Shouldn't that exist (if only for
> >>symmetry) ?
> >
> >
> > Yes it should be there for symmetry. Give this a shot:
> > using boost::posix_time;
> > std::string tds; //.. your duration string
> > time_duration td =
> > boost::date_time::parse_undelimited_time_duration<time_duration>(tds);
> >
> > This doesn't look like it will handle fractional seconds, but maybe you don't
> > need that...
>
> yes, I do need that. I'm recording certain events to be able to play
> them back later on, so I store the 'current time' along.
>
> Right now I'm doing things:
>
> // recording...
>
> // at the start of the recording...
> origin = pt::microsec_clock::local_time();
> ...
> // and for each event...
> std::string tstamp =
> pt::to_simple_string(pt::microsec_clock::local_time() - origin);
>
> // playback...
> pt::time_duration time = pt::duration_from_string(tstamp);
> ...
>
> Regards,
> Stefan
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


attached mail follows:


Yes, that helps. Of course, it would be nice to have the time_from_string use its
internal month table to turn the month string into the correct integer. ;-)
If/when I have time, I may look into how to do that. I think it would also be
nice to be able to configure (at compile time and/or at run time) the format the
date_time library uses for its simple format. One choice might be an additonal
(default?) argument to to_simple_string. Please keep in mind that these are only
suggestions regarding relatively minor nitpicks in what I consider to be a great
library. If I am not yet annoying you with my pestering questions, could you
point me in the right direction on how I might go about solving the problem with
the operator<< and a default facet?

Thanks for your help.

Phil

P.S. Here is my attempt (approach number 1) at solving the problem. I modified
date_format_simple.hpp and date_names_put.hpp so that each chooses
month_as_integer as the hard-wired month_format to return. Any chance on getting
these changes into the next date_time library? :-)

Thus, I have changed simple_format::month_format to be

  //! Describe month format
 static month_format_spec month_format()
  {
// return month_as_short_string;
      return month_as_integer;
  }

and date_names_put::do_month_format to be

      //! Default month format
      virtual month_format_spec do_month_format() const
      {
// return month_as_short_string;
        return month_as_integer;
      }

Jeff Garland wrote:

> 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
> }
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


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