Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/locale/LC categories"

From cppreference.com
< cpp‎ | locale
m
(changed NULL to nullptr.)
Line 52: Line 52:
 
     std::setlocale(LC_TIME, "ja_JP");      // date/time formatting will be Japanese
 
     std::setlocale(LC_TIME, "ja_JP");      // date/time formatting will be Japanese
 
     wchar_t str[100];
 
     wchar_t str[100];
     std::time_t t = std::time(NULL);
+
     std::time_t t = std::time(nullptr);
 
     std::wcsftime(str, 100, L"%A %c", std::localtime(&t));
 
     std::wcsftime(str, 100, L"%A %c", std::localtime(&t));
 
     std::wprintf(L"Number: %.2f\nDate: %Ls\n", 3.14, str);
 
     std::wprintf(L"Number: %.2f\nDate: %Ls\n", 3.14, str);

Revision as of 08:17, 18 September 2014

 
 
 
Defined in header <clocale>
#define LC_ALL      /*implementation defined*/
#define LC_COLLATE  /*implementation defined*/
#define LC_CTYPE    /*implementation defined*/
#define LC_MONETARY /*implementation defined*/
#define LC_NUMERIC  /*implementation defined*/
#define LC_TIME     /*implementation defined*/

Each of the above macro constants expand to integer constant expressions with distinct values that are suitable for use as the first argument of std::setlocale.

Constant Explanation
LC_ALL selects the entire C locale
LC_COLLATE selects the collation category of the C locale
LC_CTYPE selects the character classification category of the C locale
LC_MONETARY selects the monetary formatting category of the C locale
LC_NUMERIC selects the numeric formatting category of the C locale
LC_TIME selects the time formatting category of the C locale

Additional macro constants, with names that begin with LC_ followed by at least one uppercase letter, may be defined in <clocale>. For example, the POSIX specification requires LC_MESSAGES and the GNU C library additionally defines LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT, and LC_IDENTIFICATION.

Example

#include <cstdio>
#include <clocale>
#include <ctime>
#include <cwchar>
 
int main()
{
    std::setlocale(LC_ALL, "en_US.UTF-8"); // the C locale will be the UTF-8 enabled English
    std::setlocale(LC_NUMERIC, "de_DE");   // decimal dot will be German
    std::setlocale(LC_TIME, "ja_JP");      // date/time formatting will be Japanese
    wchar_t str[100];
    std::time_t t = std::time(nullptr);
    std::wcsftime(str, 100, L"%A %c", std::localtime(&t));
    std::wprintf(L"Number: %.2f\nDate: %Ls\n", 3.14, str);
}

Output:

Number: 3,14
Date: 月曜日 2011年12月19日 18時04分40秒

See also

gets and sets the current C locale
(function) [edit]
set of polymorphic facets that encapsulate cultural differences
(class) [edit]