Tools.h++ Manual
104011 Tandem Computers Incorporated 4-5
4
Sometimes you would prefer to use the extraction operator "
>>
". It must know
to expect and parse a German-formatted date. We can pass this information
along by imbuing a stream with the German locale.
The following code snippet imbues the stream
cin
with the German locale,
reads in and converts a date string from German, then displays it in the local
format.
Imbuing is useful when many values must be inserted or extracted according
to a particular locale, or when there is no way to pass a locale argument to the
point where it will be needed. By using the static member function
RWLocale::of(ios&)
, your code can discover the locale imbued in a stream.
If the stream has not yet been imbued,
of()
returns the current global locale.
1
The interface defined by
RWLocale
handles more than dates. It can also
convert times, numbers, and monetary values to and from strings. Each has its
complications. Time conversions are complicated by the need to identify the
time zone of the person who entered, or who will read, the time string. The
mishmash of Daylight Savings Time jurisdictions can make this annoyingly
difficult. Numbers are somewhat messy to format because the insertion and
extraction operators ("<<" and ">>") for them are already defined by
1. You can restore a stream to its unimbued condition with the static member function
RWLocale::unimbue(ios&)
; note that this is not the same as imbuing it with the current global locale.
RWCString str;
cout << "enter a date in German: " << flush;
str.readLine(cin);
today = RWDate(str, german);
if (today.isValid())
cout << today << endl;
german.imbue(cin);
cout << "enter a date in German: " << flush;
cin >> today; // read a German date!
if (today.isValid())
cout << today << endl;