Dates
Suppose you are American and want to format a date in German, but don't want German to be the default. Construct a German locale:
RWLocaleSnapshot german_locale (GERMAN_LOCALE_NAME);
You can format the same date for both local and German readers as follows:
cout << today << endl
<< today.asString('x', german) << endl;
See the definition of
x in the
SourcePro API Reference Guide entry for
RWLocale.
Would you like to read in a German date string? Again, the straightforward way is to call everything explicitly:
RWCString str;
cout << "enter a date in German: " << flush;
str.readLine(cin);
today = RWDateTime (str, RWDateTime::setDate, german_locale);
if (today.isValid())
cout << today << endl;
NOTE: On your operating system, the name of the German locale may be different.
Sometimes, however, you would prefer to use the extraction operator >>. Since the operator must expect a German-formatted date, and know how to parse it, you 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, and displays it in the local format:
german_locale.imbue(cin);
cout << "enter a date in German: " << flush;
cin >> today; // read a German date!
if (today.isValid())
cout << today << endl;
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. Note that you can restore a stream to its unimbued condition with the static member function RWLocale::unimbue(ios&), but this is not the same as imbuing it with the current global locale.
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 the time string, or the person who will read it. The mishmash of Daylight Saving Time jurisdictions can magnify the difficulty. Numbers are somewhat messy to format because their insertion and extraction operators (
<< and
>>) are already defined by
<iostream.h>. For money, the main problem is that there is no standard internal representation for monetary values. Fortunately, none of these problems are overwhelming with the Essential Tools Module.