Boost logo

Boost Users :

From: Bart (bartmann.nsd_at_[hidden])
Date: 2005-09-20 16:34:58


On Tue, 20 Sep 2005 11:41:33 -0500, Michal Lijowski wrote:

> Is the only way to initialize date and ptime
> using strings like
> date d(from_simple_string(s));
>
> NOT stringstreams
>
> stringstream ss("2004-Jan-1");
> date;
> ss >> d;
>
> Michal
>

Using boost 1.33 you can use either method. If you use the stringstream
method, you do not have to link with the library. The
to_*_string & from_*_string functions require linking.

In the following example, the date will first be initialized to
not_a_date_time, and then set to 2004-Jan-01.
  date d; // d == not_a_date_time
  std::stringstream ss("2004-Jan-01");
  ss >> d; // d == 2004-Jan-01

Also note, the default format for streaming in dates it "%Y-%b-%d". The %d
flag requires two digits, so "2004-Jan-1" will not parse correctly and
leave it set to not_a_date_time.

It is possible to "turn-on" exceptions to catch these kinds of errors.
See: http://tinyurl.com/8qzu4 for details.

Bart


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