The functions to retrieve and format the locale data are as follows:
Retrieve numeric formatting information
Retrieve language and the locale information
Convert date and time to a string
Convert character string to a time structure
Convert monetary value to a string
These functions are used to query locale-specific data, such as the time format or currency symbol. The functions can also be used to format time, numeric, or monetary information according to regional conventions. For more information, see the langinfo.h(3HEAD) and mktime(3C) man pages.
Example 2-4 Obtaining the Codeset Name of a LocaleThe following code fragment shows how to obtain the codeset of the current program's locale.
#include <langinfo.h> : char *cs; cs = nl_langinfo(CODESET);
In this example, for the C locale, the cs variable points to the string "646", which is the canonical name for the US-ASCII codeset. For more information about codesets, see Converting Codesets.
Example 2-5 Querying the Affirmative Response String of a LocaleThe following code fragment shows how to set the yesstr variable to the yes/no string, which is used for the affirmative response of the current locale.
#include <langinfo.h> : char *yesstr; yesstr = nl_langinfo(YESSTR);
For example, in the es_ES.UTF-8 locale, yesstr will point to the string sí.
Example 2-6 Printing the Local TimeThe following code fragment shows how to display the current date and time formatted according to the regional conventions of the locale that is set for the environment.
#include <stdio.h> #include <locale.h> #include <time.h> : char *locale, ftime[BUFSIZ]; time_t t; locale = setlocale(LC_ALL, ""); if (locale == NULL) { /* handle error */ } if (0 != strftime(ftime, BUFSIZ, (char *)NULL, localtime(&t))) { (void) printf("%s - %s\n", locale, ftime); }