Tools.h++ Manual

104011 Tandem Computers Incorporated 4-9
4
In each of the previous examples,
now
is disassembled into component parts
twice, once to extract the hour and again for the minute. This is an expensive
operation. If you expect to work with the components of a time or date much,
you may be better off disassembling the time only once:
If you work with times before 1901 or after 2037,
RWTime
cannot be used,
because it does not have the range needed.
struct tm
operations with
RWLocale
are not so restricted; you can use
RWLocale
to perform conversions
for any time or date.
Numbers
Abstract class
RWLocale
provides an interface for conversions between strings
and numbers—both integers and floating point values.
RWLocaleSnapshot
implements this interface, providing the full range of capabilities defined by
the Standard C Library type
struct lconv
. This includes using appropriate
digit group separators, decimal “point”, and currency notation. On conversion
from strings it allows, and checks, the same digit group separators.
Unfortunately, the standard iostream library provides definitions for number
insertion and extraction operators which cannot be overridden, so stream
operations are clumsier than we might like.
Instead, we use RWCString functions directly:
RWTime now = RWTime::now();
struct tm tmbuf; now.extract(&tmbuf);
const RWLocale& here = RWLocale::global(); // the default global
locale
cout << here.asString(&tmbuf, 'H') << ":"
<< here.asString(&tmbuf, 'M'); << endl;
RWLocaleSnapshot french("fr");
double f = 1234567.89;
long i = 987654;
RWCString fs = french.asString(f, 2);
RWCString is = french.asString(i);
if (french.stringToNum(fs, &f) &&
french.stringToNum(is, &i)) // verify conversion
cout << f << “\t” << i << endl
<< fs << “\t” << is << endl;