Module: Essential Tools Module Group: Internationalization Classes
Does not inherit
asString() CurrSymbol defaultLocale() global() |
imbue() locale_name() moneyAsString() monthIndex() |
of() stringToDate() stringToMoney() stringToNum() |
stringToTime() unimbue() weekdayIndex() |
#include <locale.h> #include <rw/locale.h> (Abstract base class)
RWLocale is an abstract base class. It defines an interface that formats the conversion of strings to and from dates and times. It formats dates (including day and month names), times, currency, and numbers (including digit grouping).
Note that because it is an abstract base class, there is no way to actually enforce these goals -- the description here is merely the model of how a class derived from RWLocale should act.
There are three ways to use an RWLocale object:
By passing the object to functions which expect one, such as RWDate::asString().
By specifying a "global" locale using the static member function RWLocale::global(RWLocale*). This locale is passed as the default argument to functions that use a locale.
By "imbuing" a stream with the object, so that when an RWDateTime, RWDate or RWTime is written to a stream using operator<<(), the appropriate formatting will be automatically used.
The library provides two main implementations of RWLocale. These subclasses each allow more than one locale to be active at a time, and they also support conversion from strings to other types. They include:
Class RWAnsiLocale which encapsulates the standard C++ library locale, std::locale
Class RWLocaleSnapshot which encapsulates the Standard C Library locale facility
None
enum CurrSymbol { NONE, LOCAL, INTL };
Controls how to format currency, specifying whether to use no currency symbol, the local currency symbol (such as "$"), or the international currency symbol (such as "USD").
virtual RWCString asString(long) const = 0; virtual RWCString asString(unsigned long) const = 0;
Converts the number to a string (For example, "3,456").
virtual RWCString asString(double f, int precision = 6, bool showpoint = 0) const = 0;
Converts the double f to a string. The variable precision is the number of digits to place after the decimal separator. If showpoint is true, the decimal separator appears regardless of the precision.
virtual RWCString asString(long double val, int precision = 10, Boolean showpoint = 0) const;
Converts the value of val to a string. The variable precision is the number of digits to place after the decimal separator. If showpoint is true, the decimal separator appears regardless of the precision.
virtual RWCString asString(const struct tm* tmbuf,char format, const RWZone& zone=RWZone::local()) const = 0;
Converts components of the struct tm object to a string, according to the format character. The meanings assigned to the format character are identical to those used in the C99 function strftime(). The members of struct tm should be set consistently. See Table 18 for a summary of strftime() formatting characters.
RWCString asString(const struct tm* tmbuf,const char* format, const RWZone& zone) const;
Converts components of the struct tm object to a string, according to the format string. Each format character in the format string must be preceded by %. Any characters not preceded by % are treated as ordinary characters which are returned unchanged. You may represent the special character % with "%%". The meanings assigned to the format character are identical to those used in the C99 function strftime(). The members of struct tm should be set consistently. See Table 18 for a summary of strftime() formatting characters. This function is not virtual in order to maintain link-compatibility with the previous version of the library.
const RWLocale* imbue(ios& stream) const;
Installs self in the stream argument, for later use by the operators << and >> (For example in RWDateTime, RWDate or RWTime). To retrieve the pointer from the stream, use static member RWLocale::of(). In this way, you may pass a locale transparently through many levels of control so that it is available where needed, without intruding elsewhere.
virtual const RWCString&
locale_name() const
Returns the name of the encapsulated std::locale.
virtual RWCString moneyAsString(double value,enum CurrSymbol = LOCAL) const = 0;
Returns a string containing the value argument formatted according to monetary conventions for the locale. The value argument should contain an integer representing the number of units of currency (for example, moneyAsString(1000., RWLocale::LOCAL) in a US locale would yield "$10.00"). The CurrSymbol argument determines whether to apply the local currency symbol.
virtual int monthIndex(const RWCString&) const = 0;
Interprets its argument as a full or abbreviated month name, returning values 1 through 12 to represent (respectively) January through December, or 0 for an error. Ignores leading white space.
virtual bool stringToNum(const RWCString&, double* fp) const = 0;
Interprets the RWCString argument as a floating point number. Spaces are allowed before and after the (optional) sign, and at the end. Digit group separators are allowed in the integer portion. Returns true for a valid number, false for an error. If it returns false, the double* argument is untouched. All valid numeric strings are accepted; all others are rejected. The following are examples of valid numeric strings in an English-speaking locale:
"1" " -02. " ".3" "1234.56" "1e10" "+ 19,876.2E+20"
virtual bool stringToNum(const RWCString&, long* ip) const = 0;
Interprets the RWCString argument as an integer. Spaces are allowed before and after the (optional) sign, and at the end. Digit group separators are allowed. Returns true for a valid integer, false for an error. If it returns false, the long* argument is untouched. All valid numeric strings are accepted; all others are rejected. The following are examples of valid integral strings in an English-speaking locale:
"1" " -02. " "+ 1,234" "1234545" "1,234,567"
virtual bool
stringToNum (const RWCString&, unsigned long*) const = 0;
Interprets the RWCString argument as an integer. Allows spaces before and after the (optional) sign, and at the end. Allows digit group separators. Returns true for a valid integer, false for an error. If it returns false, the long* argument is untouched. All valid numeric strings are accepted; all others are rejected.
NOTE -- If you are using the C locale, you must omit commas as thousands separators. Because the default locale is likely to be C for many English-speaking programmers, numbers with commas separating the thousands may be rejected. This is true even in a locale where English is the dominant language.
Table 18 contains examples of formatting characters used by the C99 version of strftime(). For any format that does not use all members of the struct tm, only those members that are actually used are noted [in brackets]
Format character | Meaning |
%a | Replaced by the locale's abbreviated weekday name [from tm::tm_wday] |
%A | Replaced by the locale's full weekday name [from tm::tm_wday] |
%b | Replaced by the locale's abbreviated month name |
%B | Replaced by the locale's full month name. |
%c | Replaced by the locale's appropriate date and time representation. (See the Base Definitions volume of IEEE Std 1003 1-2001, <time h>.) |
%C | Replaced by the year divided by 100 and truncated to an integer, as a decimal number [00,99] |
%d | Replaced by the day of the month as a decimal number [01,31] |
%D | Equivalent to %m /%d /%y |
%e | Replaced by the day of the month as a decimal number [1,3]; asingle digit is preceded by a space |
%F | Equivalent to %Y -%m -%d (the ISO 8601:2000 standard date format) |
%g | Replaced by the last 2 digits of the week-based year (see below) as a decimal number [00,99] |
%G | Replaced by the week-based year (see below) as a decimal number (for example,1977) |
%h | Equivalent to %b |
%H | Replaced by the hour (24-hour clock) as a decimal number [00,23] |
%I | Replaced by the hour (12-hour clock) as a decimal number [01,12] |
%j | Replaced by the day of the year as a decimal number [001,366] |
%m | Replaced by the month as a decimal number [01,12] |
%M | Replaced by the minute as a decimal number [00,59] |
%n | Replaced by a <newline> |
%p | Replaced by the locale's equivalent of either a.m. or p. m. |
%r | Replaced by the time in a. m. and p. m. notation; in the POSIX ocale this is equivalent to %I :%M :%S %p |
%R | Replaced by the time in 24-hour notation (%H :%M) |
%S | Replaced by the second as a decimal number [00,60] |
%t | Replaced by a <tab> |
%T | Replaced by the time (%H :%M :%S) |
%u | Replaced by the weekday as a decimal number [1,7], with 1representing Monday |
%U | Replaced by the week number of the year as a decimal number [00,53 ] The first Sunday of January is the first day of week 1; days in the new year before this are in week 0. |
%V | Replaced by the week number of the year (Monday as the first day of the week) as a decimal number [01,53]. If the week containing 1 January has four or more days in the new year, then it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1. Both January 4th and the first Thursday of January are always in week 1. |
%w | Replaced by the weekday as a decimal number [0,6], with 0 representing Sunday |
%W | Replaced by the week number of the year as a decimal number [00,53]. The first Monday of January is the first day of week 1; days in the new year before this are in week 0. |
%x | Replaced by the locale's appropriate date representation (See the Base Definitions volume of IEEE Std 1003 1-2001,<time h>.) |
%X | Replaced by the locale's appropriate time representation (See the Base Definitions volume of IEEE Std 1003 1-2001,<time h>.) |
%y | Replaced by the last two digits of the year as a decimal number [00,99] |
%Y | Replaced by the year as a decimal number (for example,1997) |
%z | Replaced by the offset from UTC in the ISO 8601:2000 standard format (+hhmm or -hhmm ), or by no characters if no timezone is determinable. For example, "-0430"means 4 hours 30 minutes behind UTC (west of Greenwich). |
%^Z | Replaced by the timezone name or abbreviation, or by no bytes if no timezone information exists |
%% | Replaced by % |
virtual bool stringToDate(const RWCString&, struct tm*) const = 0;
Interprets the RWCString as a date, and extracts the month, day, and year components to the tm argument. It returns true for a valid date, false otherwise. If it returns false, the struct tm argument is untouched; otherwise it sets the tm_mday, tm_mon, and tm_year members. The date entered is expected to be in the format indicated by the current local (%x). If that is not the case, the algorithm attempts to detect the different parts of the date string. Note that this function cannot reject all invalid date strings.
The following are examples of parseable date strings in an English-speaking locale:
"Jan 9, 62" "1/9/62" "January 9 1962" "09Jan62" "010962"
virtual bool stringToMoney(const RWCString&, double*, RWLocale::CurrSymbol=LOCAL) const = 0;
Interprets the RWCString argument as a monetary value. The currency symbol, if any, is ignored. Negative values may be specified by the negation symbol or by enclosing parentheses. Digit group separators are optional; if present they are checked. Returns true for a valid monetary value, false for an error. If it returns false, the double* argument is untouched; otherwise it is set to the integral number of monetary units entered (For example, cents, in a U.S. locale).
virtual bool stringToTime(const RWCString&, struct tm*) const = 0;
Interprets the RWCString argument as a time, with hour, minute, and optional second. the time string is expected to be formatted according to the locale's %X format specifier. If that is not the case, the algorithm attempts to detect the different parts of the time string. If the hour is in the range [1..12], the local equivalent of "AM" or "PM" is allowed. Returns true for a valid time string, false for an error. If it returns false, the tm argument is untouched; otherwise it sets the tm_hour, tm_min, and tm_sec members. Note that this function cannot reject all invalid time strings. The following are examples of valid time strings in an English-speaking locale:
"1:10 AM" "13:45:30" "12.30.45pm" "PM 3:15" "1430"
virtual int weekdayIndex(const RWCString&) const = 0;
Interprets its argument as a full or abbreviated weekday name, returning values 1 through 7 to represent (respectively) Monday through Sunday, or 0 for an error.
const RWLocale* defaultLocale();
Returns a pointer to a new instance of RWLocaleSnapshot("C").
static const RWLocale* global(const RWLocale* loc);
Sets the global "default" locale object to loc, returning the old object. This object is used by RWDateTime, RWDate and RWTime string conversion functions as a default locale. It is set initially to refer to an instance of a class that provides the functionality of RWLocaleSnapshot("C").
static const RWLocale& global();
Returns a reference to the present global "default" locale.
static const RWLocale* imbue(std::ios& s);
Replaces the locale installed in the identified stream with the current locale and returns the old locale.
static const RWLocale& of(ios&);
Returns the locale installed in the stream argument by a previous call to RWLocale::imbue(); if no locale was installed, returns the result from RWLocale::global().
static const RWLocale* unimbue(std::ios& s);
Removes the locale that was installed in the identified stream by a previous call to RWLocale::imbue(). If no locale was previously installed, returns the result of RWLocale::global().
© 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.