gcc 4.4.4: std::time_put facet instantiation, and locale imbue-ing
Sam Varshavchik
mrsam@courier-mta.com
Sun Jun 6 21:11:00 GMT 2010
I don't understand the logic I'm seeing with libstdc++'s instantiation of
the time_put facet from a locale, and then using the facet to format a date
into an ostream. It appears that time_put's locale gets ignored. The locale
that's imbued in the ostream governs formatting of date/time localized
strings.
My example:
#include <locale>
#include <iostream>
#include <ctime>
#include <cstring>
#include <iterator>
int main()
{
std::locale l("fr_FR.utf-8");
const std::time_put<char> &t(std::use_facet<std::time_put<char> > (l));
time_t now=time(0);
struct tm tmbuf;
localtime_r(&now, &tmbuf);
const char *fmt="%B-%d-%Y";
std::cout.imbue(l);
t.put(std::ostreambuf_iterator<char>(std::cout), std::cout, ' ', &tmbuf,
fmt, fmt+strlen(fmt));
std::cout << std::endl;
return 0;
}
I couldn't get this to work until I stuck that imbue in there. I'm trying to
understand why.
If I take out the imbue call, the month name that gets formatted to cout
comes from the C/POSIX locale, even though "t" is a facet from the fr_FR
locale. I can't figure out why I instantiate a locale, get its time_put
facet, and use it to format a struct tm, when the locale gets subsequently
ignored in favor of what's imbued in the ostream.
If in the sample code, I change:
std::locale l("C");
and
std::cout.imbue(std::locale("fr_FR.utf-8"));
Then I get fr_FR locale written to cout, which suggests that the imbued
locale in the ostream governs the date/time localization.
So, it appears to me that it's completely irrelevant from which locale the
std::time_put facet gets instantiated from, it's the locale imbued into the
output stream that determines the output locale. What is the logic for this?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20100606/f82e987f/attachment.sig>
More information about the Gcc-help
mailing list