Rwlocale and rwzone, Dates, Chapter 16 – HP Integrity NonStop J-Series User Manual
Page 239: Rwlocale and, Rwzone

Click on the banner to return to the user guide home page.
©Copyright 1996 Rogue Wave Software
RWLocale and RWZone
Tools.h++ addresses these problems with the abstract classes
RWLocale
and
RWZone
. If you have
used
RWDate,
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:
RWDate today = RWDate::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
[27]
, because you want French formatting. To display the date in your preferred format,
you 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 straightforward way to
use RWLocaleSnapshot is to pass it directly to the
RWDate
member function asString()
[28]
:
cout << today.asString('x', here) << endl;
There is, however, a more convenient way. You can install here as the global default locale so the
insertion operator will use it:
RWLocale::global(&here);
cout << today << endl;