Returns a pointer to a string that contains information obtained from the program's current locale.
#include <langinfo.h> char *nl_langinfo (nl_item item);
The following constants are valid:
| Constant | Category | Description |
|---|---|---|
| D_T_FMT | LC_ TIME | String for formatting date and time |
| D_FMT | LC_TIME | String for formatting date |
| T_FMT | LC_TIME | String for formatting time |
| T_FMT_AMPM | LC_TIME | Time format with AM/PM string |
| AM_STR | LC_TIME | String that represents AM in 12- hour clock notation |
| PM_STR | LC_TIME | String that represents PM in 12-hour clock notation |
| DAY_1 | LC_ TIME | The name of the first day of the week |
| . . . | ||
| DAY_7 | LC_TIME | The name of the seventh day of the week |
| ABDAY_ 1 | LC_TIME | The abbreviated name of the first day of the week |
| . . . | ||
| ABDAY_ 7 | LC_TIME | The abbreviated name of the seventh day of the week |
| MON_ 1 | LC_TIME | The name of the first month in the year |
| . . . | ||
| MON_12 | LC_TIME | The name of the twelfth month in the year |
| ABMON_1 | LC_ TIME | The abbreviated name of the first month in the year |
| . . . | ||
| ABMON_12 | LC_ TIME | The abbreviated name of the twelfth month in the year |
| ERA | LC_ TIME | Era description strings |
| ERA_D_FMT | LC_TIME | Era date format string |
| ERA_T_FMT | LC_ TIME | Era time format |
| ERA_ D_T_FMT | LC_TIME | Era date and time format |
| ALT_DIGITS | LC_ TIME | Alternative symbols for digits |
| RADIXCHAR | LC_NUMERIC | The radix character |
| THOUSEP | LC_NUMERIC | The character used to separate groups of digits in non-monetary values |
| YESEXP | LC_MESSAGES | The expression for affirmative responses to yes/no questions |
| NOEXP | LC_MESSAGES | The expression for negative responses to yes/no questions |
| CRNCYSTR | LC_MONETARY | The currency
symbol. It is preceded by one of the following:
|
| CODESET | LC_ CTYPE | Codeset name |
If the setlocale function is called after a call to nl_langinfo, then the pointer returned by the previous call to nl_langinfo will be unspecified. In this case, the nl_langinfo function should be called again.
| x | Pointer to the string containing the requested information. If item is invalid, the function returns an empty string. |
#include <stdio.h>
#include <locale.h>
#include <langinfo.h>
/* This test sets up the British English locale, and then inquires on the */
/* data and time format, first day of the week, and abbreviated first */
/* day of the week. */
#include <stdlib.h>
#include <string.h>
int main()
{
char *return_val;
char *nl_ptr;
/* */
/* set the locale, with user supplied locale name */
/* */
return_val = setlocale(LC_ALL,"en_gb.iso8859-1");
if (return_val == NULL)
{
printf("ERROR : The locale is unknown");
exit(1);
}
printf("+-----------------------------------------------------+\n");
/* */
/* Get the date and time format from the locale. */
/* */
printf("D_T_FMT = ");
/* */
/* Compare the returned string from nl_langinfo with an empty */
/* string. */
/* */
if(!strcmp ((nl_ptr = (char *)nl_langinfo(D_T_FMT)),""))
{
/* */
/* The string returned was empty this could mean that either */
/* 1) The locale does not contain a value for this item */
/* 2) The value for this item is an empty string */
/* */
printf("nl_langinfo returned an empty string\n");
}
else
{
/* */
/* Display the date and time format */
/* */
printf("%s\n",nl_ptr);
}
/* */
/* Get the full name for the first day of the week from locale */
/* */
printf("DAY_1 = ");
/* */
/* Compare the returned string from nl_langinfo with an empty */
/* string. */
/* */
if(!strcmp ((nl_ptr = (char *)nl_langinfo(DAY_1)),""))
{
/* */
/* The string returned was empty this could mean that either */
/* 1) The locale does not contain a value for the first day of */
/* the week */
/* 2) The value for the first day of the week is an empty string */
/* */
printf("nl_langinfo returned an empty string\n");
}
else
{
/* */
/* Display the full name of the first day of the week */
/* */
printf("%s\n",nl_ptr);
}
/* */
/* Get the abbreviated name for the first day of the week from locale */
/* */
printf("ABDAY_1 = ");
/* */
/* Compare the returned string from nl_langinfo with an empty */
/* string. */
/* */
if(!strcmp ((nl_ptr = (char *)nl_langinfo(ABDAY_1)),""))
{
/* */
/* The string returned was empty this could mean that either */
/* 1) The locale does not contain a value for the first day of */
/* the week */
/* 2) The value for the first day of the week is an empty string */
/* */
printf("nl_langinfo returned an empty string\n");
}
else
{
/* */
/* Display the abbreviated name of the first day of the week */
/* */
printf("%s\n",nl_ptr);
}
}
Running the example program produces the following result:
+-----------------------------------------------------+ D_T_FMT = %a %b %d %H:%M:%S %Z %Y DAY_1 = Sunday ABDAY_1 = Sun