Tools.h++ Manual
4-4 104011 Tandem Computers Incorporated
4
Begin by constructing a date, today’s date:
RWDate today = RWDate::now();
We can display it using ordinary “C”-locale conventions, the usual way:
cout << today << endl;
But what if you are in some other locale? Perhaps you have set your
environment variable
LANG
to "
fr
", or "
fr_FR
"
1
, because you want French
formatting. We would like the date to be displayed in your preferred local
format. First, let’s construct an
RWLocale
object:
RWLocale& here = *new RWLocaleSnapshot("");
Class
RWLocaleSnapshot
is the main implementation of the interface defined
by
RWLocale
. It extracts the information it needs from the global environment
during construction with the help of such Standard C Library functions as
strftime()
and
localeconv()
. The most straight-forward way to use this
is to pass it directly to the
RWDate
member function
asString()
2
:
cout << today.asString('x', here) << endl;
but there are more convenient ways. We can install
here
as the global default
locale so the insertion operator will use it:
RWLocale::global(&here);
cout << today << endl;
Dates
Now, suppose you also want to format a date in German, but don’t want that
to be the default. Let us construct a German locale:
RWLocale& german = *new RWLocaleSnapshot(“de”); // or, "de_DE"
Now we can format the same date for both local and German readers:
cout << today << endl
<< today.asString(‘x’, german) << endl;
Let us now suppose you want to read in a German date string. The straight-
forward way, again, is to call everything explicitly:
1. There is a standard for locale names; some vendors ignore it.
2. The function
asString()’s
first argument is a character, which may be any of the format options
supported by the Standard C Library function
strftime()
.