RWLocale and RWZone
The Essential Tools Module solves these problems with the classes
RWLocale and
RWZone. If you have used
RWDateTime, you have already used
RWLocale, perhaps unknowingly. Every time you convert a date or time to or from a string, a default argument carries along an
RWLocale reference. Unless you change it, this is a reference to a global instance of a class derived from
RWLocale at program startup to provide the effect of a C locale.
To use
RWLocale explicitly, you can construct your own instance and pass it in place of the default. Similarly, when you manipulate times, you can substitute your own instance for the default
RWZone reference.
You can also install your own instance of
RWLocale or
RWZone as the global default. Many streams even allow you to install your
RWLocale instance in the stream so that dates and times transferred on and off that stream are formatted or parsed accordingly, without any special arguments. This is called
imbuing the stream, a process described in more detail in the next section.
In the following sections, let us look at some examples of how to localize various data using
RWLocale and
RWZone. Let us begin by constructing a date, today's date:
RWDateTime today = RWDateTime::now();
We can display it the usual way using ordinary C-locale conventions:
cout << today << endl;
But what if you're outside your home locale? Or perhaps you have set your environment variable
LANG to
fr, because you want French formatting. (Despite the existing standard for locale names, many vendors provide variant naming schemes. Check your vendor's documentation for details.) To display the date in your preferred format, construct an
RWLocaleSnapshot object:
RWLocaleSnapshot here ("");
This has the effect of creating an
RWLocaleSnapshot object which is constructed based on the information contained in the environment variables
LC_* and
LANG.