I was surprised to discover that the following code emits the formatted
date string uisng the default locale (C/POSIX), and I am required to
uncomment the line that imbues the "ja_JP" locale into cout in order to
get Japanese-language output.
I am surprised by this, because I thought that the text ought to vary
on the locale from which I obtained the time_put facet, rather than on
the locale used by the output stream. Is G++ (v4.03) right, or is it
something implementation-defined? Does this follow the spirit of the
Standards intentions? Is there something to quote from the Standard to
enlighten me, or other resources to help?
Also, is this method even the preferred way to print a date to an
output stream?
-----
#include <locale>
#include <iostream>
#include <ctime>
#include <utility>
#define CHAR_ARRAY_STR_END(a) ((a) + (sizeof(a)) - 1)
using namespace std;
int main(void)
{
const char format[] = "%c\n";
const char *format_end = CHAR_ARRAY_STR_END(format);
time_t secs = time(0);
std::tm *t = localtime(&secs);
const locale loc("ja_JP.utf8");
// cout.imbue(loc);
const time_put<char>& tp = use_facet<time_put<char(loc);
tp.put(cout, cout, ' ', t, format, format_end);
cout << flush;
}
-----