Module: Essential Tools Module Group: Internationalization Classes
RWLocaleSnapshotRWLocale
#include <locale.h> #include <rw/locale.h> RWLocaleSnapshot ourLocale(""); // encapsulate user's formats
The class RWLocaleSnapshot implements the RWLocale interface using Standard C Library facilities. To use it, the program creates an RWLocaleSnapshot instance. The constructor of the instance queries the program's environment (using Standard C Library functions such as localeconv(), strftime(), and, if available, vendor specific library functions) to determine the formatting conventions in effect at the moment of instantiation. When done, the locale can then be switched and another instance of RWLocaleSnapshot created.
By creating multiple instances of RWLocaleSnapshot, your program can have more than one locale active at the same time, something that is difficult to do with the Standard C Library facilities.
Note: RWLocaleSnapshot does not encapsulate character set, collation, or message information.
Class RWLocaleSnapshot contains a set of public data members initialized by its constructor with information extracted from its execution environment.
For a description of the static members of the RWLocale interface that operate on objects of type RWLocaleSnapshot, see the RWLocale class description.
None
NOTE -- To ensure that the asString() methods in this example correctly format the output, you must first install on your system any locale you request before invoking RWLocaleSnapshot().
#include <rw/tools/datetime.h> #include <rw/locale.h> #include <rw/cstring.h> #if defined(_WIN32) // french should be a valid French locale name on Windows #define FRENCH_LOCALE_NAME "french" #elif defined(__HP_aCC) #define FRENCH_LOCALE_NAME "fr_FR.utf8" #else // fr_FR should be a valid French locale name on most Unices #define FRENCH_LOCALE_NAME "fr_FR" #endif int main() { RWLocaleSnapshot us("C"); RWLocaleSnapshot french(FRENCH_LOCALE_NAME); // or vendor specific RWCString americanDate ("10 June 2025"); RWCString frenchDate ("10 Juin 2025"); RWDateTime d(frenchDate, RWDateTime::setDate, french); // OK std::cout << frenchDate << ((d.isValid()) ? " IS " : " IS NOT ") << "a valid date (french locale)." << std::endl << std::endl; RWDateTime bad = RWDateTime(frenchDate, RWDateTime::setDate); std::cout << frenchDate; std::cout << ((bad.isValid() && bad == d) ? " IS " : " IS NOT ") << "a valid date (default locale)." << std::endl << std::endl; bad = RWDateTime(americanDate, RWDateTime::setDate, french); std::cout << americanDate; std::cout << ((bad.isValid() && bad == d) ? " IS " : " IS NOT ") << "a valid date (french locale)." << std::endl << std::endl; std::cout << d << std::endl; std::cout << d.asString('x') << std::endl; std::cout << d.asString('x', french) << std::endl; return 0; }
Program output:
10 Juin 2025 IS a valid date (french locale). 10 Juin 2025 IS NOT a valid date (default locale). 10 June 2025 IS a valid date (french locale). Tue Jun 10 00:00:00 2025 06/10/25 10.06.2025
RWLocaleSnapshot(const char* localeName = 0);
Constructs an RWLocale object by extracting formats from the global locale environment. It uses the Standard C Library function setlocale() to set the named locale, and then restores the previous global locale after formats have been extracted. If localeName is 0, it simply uses the current locale. The most useful locale name is the empty string, "", which is a synonym for the user's chosen locale (usually specified by the environment variable LANG).
virtual RWCString asString(long) const; virtual RWCString asString(unsigned long) const; virtual RWCString asString(double f, int precision = 6,
bool showpoint = 0) const; virtual RWCString asString(long double val, int precision = 6, bool showpoint = 0) const; virtual RWCString asString(struct tm* tmbuf,char format, const RWZone& zone); const; virtual RWCString asString(struct tm* tmbuf,char* format, const RWZone& zone) const; virtual RWCString moneyAsString(double value,enum CurrSymbol = LOCAL) const; virtual bool stringToNum (const RWCString&, double* fp) const; virtual bool stringToNum (const RWCString&, long* ip ) const; virtual bool stringToNum (const RWCString&, unsigned long*) const; virtual bool
stringToDate (const RWCString&, struct tm*) const; virtual bool stringToTime (const RWCString&, struct tm*) const; virtual bool stringToMoney(const RWCString&, double* , RWLocale::CurrSymbol=LOCAL) const;
Note: All previous member functions are redefined from class RWLocale. These virtual functions follow the interface described under class RWLocale. They generally work by converting values to and from strings using the rules specified by the struct lconv values (see <locale.h>) encapsulated in self.
virtual const RWCString&
currency_symbol() const
Returns a string to use as the currency symbol for this locale.
virtual const RWCString&
decimal_point() const
Returns a string to use as the numerical decimal separator.
virtual const RWCString&
grouping() const
Returns a string identifying the number of digits to be included in a numerical group. A group is simply the digits between adjacent thousand's separators.
Each character of the string is an integer that specifies the number of digits in a group, starting with the right most group.
If the value is equal to 0, the previous element is used repeatedly for the remainder of the digits.
If the value is equal to CHAR_MAX, no further grouping is to be performed.
Example
"\3" \\ Specifies the United States grouping system. \\ For example, "1,000,000" "\3\2" \\ Specifies the Nepalese grouping system. \\ For example, "1,00,00,000"
virtual const RWCString&
int_curr_symbol() const
Returns a string to use as the international currency symbol.
virtual const RWCString&
locale_name() const
Returns the name of the encapsulated std::locale.
virtual const RWCString&
mon_decimal_point() const
Returns a string to use as the monetary decimal separator.
virtual const RWCString&
mon_thousands_sep() const
Returns a string to use as the monetary thousands separator.
virtual const RWCString&
mon_grouping() const
Returns a string identifying the number of digits to be included in a numerical group. A group is simply the digits between adjacent thousand's separators.
Each character of the string is an integer that specifies the number of digits in a group, starting with the right most group.
If the value is equal to 0, the previous element is used repeatedly for the remainder of the digits.
If the value is equal to CHAR_MAX, no further grouping is to be performed.
See Example under grouping().
virtual const RWCString&
positive_sign() const
Returns a string to use as the positive sign.
virtual const RWCString&
negative_sign() const
Returns a string to use as the negative sign.
virtual const RWCString&
thousands_sep() const
Returns a string to use as the numerical thousands separator.
RWCString decimal_point_; RWCString thousands_sep_; RWCString grouping_; RWCString int_curr_symbol_; RWCString currency_symbol_; RWCString mon_decimal_point_; RWCString mon_thousands_sep_; RWCString mon_grouping_; RWCString positive_sign_; RWCString negative_sign_; char int_frac_digits_; char frac_digits_; char p_cs_precedes_; char p_sep_by_space_; char n_cs_precedes_; char n_sep_by_space_; char p_sign_posn_; char n_sign_posn_;
These are defined identically as the correspondingly-named members of the Standard C Library type lconv, from <locale.h>.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.