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?
Attachment:
pgpDpSlV1MAta.pgp
Description: PGP signature