Boost logo

Boost Users :

Subject: Re: [Boost-users] date_time formatting issues
From: Václav Haisman (v.haisman_at_[hidden])
Date: 2009-09-04 01:57:08


ariasgore_at_[hidden] wrote, On 1.9.2009 11:05:
> Hi,
> I have been trying to format a simple posix date time for string representation.
>
> The basic part is plain and simple:
> ----------------------------------------------
> #include <boost/date_time/posix_time/posix_time.hpp>
> namespace ps = boost::posix_time;
> ps::ptime t(ps::second_clock::local_time());
> std::string foo = ps::to_simple_string(t);
> ----------------------------------------------
>
> But how can I format this returned string? I think the facets are required
> but as mentioned here :
> http://www.boost.org/doc/libs/1_40_0/doc/html/date_time/date_time_io.html#date_time.time_facet
> I tried something with the boost::date_time::date_input_facet:
> e.g
> ----------------------------------------------
> ps::date_input_facet("%A");
This is a no-op. It creates and destroys instance of date_input_facet at the
same line. Beside that, it is also input date _input_ facet. It does not deal
with output.

> ps::ptime t(ps::second_clock::local_time());
> std::string foo = ps::to_simple_string(t);
> ----------------------------------------------
>
> by using it before the simple_string call. I am sure this is wrong but I
> wouldn't like to use streams in addition to that to simply rearange some
> parts of a date for that reason.
The point of the to_simple_string() function is that it is simple. It
provides quick and canonical (YYYY-mmm-DD HH:MM:SS.fffffffff) string
representation of a date. If you want something else, you will have to use
ostringstream, imbue it with locale with date_facet that produces your
desired formatting.

> thanks
> sam

--
VH

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