
Hello, How can I get a localized date string with the boost date / time library? To illustrate: date d(2005,06,25); string sometext=to_simple_string(d); This returns: 2005-Jun-25 However, I want to be able to define my own formats.I can get the localization example to work and format my stuff on stdout, but nowhere it tells anything about how to fill in a string using localized settings. Thank you, Albert

Not sure, if I understand you correctly. Have you tried date_facets to imbue stringstreams? There you can define the format of dates. Something, like this: date d(2005,06,25); date_facet date_output; stringstream ss; ss.imbue( locale(ss.getloc(), &date_output) ); date_output.format("%Y-%B-%d"); ss << d; string strDate = ss.str(); Is it what you are asking for? Christian

On Mon, 20 Feb 2006 18:17:24 -0500, Christian Henning wrote
Not sure, if I understand you correctly. Have you tried date_facets to imbue stringstreams? There you can define the format of dates.
Something, like this:
date d(2005,06,25);
date_facet date_output;
Just as a note: this code is likely to crash when the stringstream is destroyed and date_output is deleted. You'll want to dynamically allocate these facets as shown in the date-time docs.
stringstream ss; ss.imbue( locale(ss.getloc(), &date_output) );
date_output.format("%Y-%B-%d");
ss << d;
string strDate = ss.str();
Is it what you are asking for?
The formats, the strings used for things like month and day names -- basically anything can be replacecd using the facets as Christian indicates. There is an I/O tutorial in the documents that shows how to do this. Good places to read: http://www.boost.org/doc/html/date_time/date_time_io.html#date_time.io_tutor... http://www.boost.org/doc/html/date_time/date_time_io.html HTH, Jeff

Something, like this:
date d(2005,06,25);
date_facet date_output;
Just as a note: this code is likely to crash when the stringstream is destroyed and date_output is deleted. You'll want to dynamically allocate these facets as shown in the date-time docs.
I'm probably not the first one to ask. But why is that behavior? Why is there a need for creating the facet in the heap instead on the stack? I'm usually a little picky when it comes to dynamically allocating memory. Thanks, Christian

I'm probably not the first one to ask. But why is that behavior?
It is the standard way to implement facets. The locale system keep a reference count for each facet and deletes them when the last locale uses it is destroyed. As with other facets it is possible to override this behaviour by passing 1 as the last parameter "ref" to the constructor. It should works fine for temporary stringstreams but I wouldn't use it for cout or in the global locale. Almost all locale operations allocate heap memory so one more allocation on facet creation makes a very small difference. There is one theoretical memory leak problem if an exception occurs between the allocation and the time when the facet is added to a locale. Note that the examples in the date_time library: // create date_facet and date_input_facet using all defaults date_facet* date_output = new date_facet(); ss.imbue(locale(ss.getloc(), date_output)); // replace names in the output facet date_output->long_month_names(long_months); date_output->long_weekday_names(long_days); isn't "standard" since the facets are meant to be immutable once they are added to a locale. The streams are allowed to cache the values returned by the facets so changes are not guaranteed to be effective until the next imbue call. However, the date_time streaming is implemented without caches so it works.

On Wed, 22 Feb 2006 21:04:30 +0000 (UTC), Martin Adrian wrote
Note that the examples in the date_time library:
// create date_facet and date_input_facet using all defaults date_facet* date_output = new date_facet(); ss.imbue(locale(ss.getloc(), date_output));
// replace names in the output facet date_output->long_month_names(long_months); date_output->long_weekday_names(long_days);
isn't "standard" since the facets are meant to be immutable once they are added to a locale. The streams are allowed to cache the values returned by the facets so changes are not guaranteed to be effective until the next imbue call. However, the date_time streaming is implemented without caches so it works.
Hmm, thx for pointing this out...I hadn't realized this. I'll have to take that into account in the next version of the TR2 date-time proposal :-) Thx, Jeff
participants (4)
-
Albert Wessel
-
Christian Henning
-
Jeff Garland
-
Martin Adrian