问题
How can I include into my Visual Studio 2010 C-project langinfo.h. I've tried
#include <langinfo.h>
, but it seems there is no such header file in the VS 2010 environment. I need to get the starting weekday for the locale, but I now the way only using this library.
So, the question is how to solve my problem: how to include langinfo.h, or how to get the current locale starting weekday.
回答1:
I think, you should use GetLocaleInfoEx() function. For example, to get the starting day of a week these calls might be used:
# if defined(_WIN32_WINNT_VISTA) && WINVER >= _WIN32_WINNT_VISTA && defined(LOCALE_NAME_USER_DEFAULT)
GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, wsDay, 4)
# else
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, wsDay, 4)
# endif
More info about this function can be found at http://msdn.microsoft.com/en-us/library/dd318103%28v=vs.85%29.aspx
回答2:
Here is a small example program first_weekday.c
, which gets the first day of the week. Furthermore, values and sizes of used variables are printed.GetLocalInfoEx()
stores the first day of the week in the variable week_1stday
.
However, on Windows we have: 0:Monday, ... 6:Sunday
(see: LOCALE_IFIRSTDAYOFWEEK).
Therefore a calculation is included here, in order to get 0:Sunday, 1:Monday, ...
The number for the first day of the week is stored then in first_weekday
#include <stdio.h>
#include <windows.h>
int main(
)
{
int ret;
int first_weekday;
DWORD week_1stday;
ret = GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT,
LOCALE_IFIRSTDAYOFWEEK | LOCALE_RETURN_NUMBER,
(LPWSTR) & week_1stday,
sizeof(week_1stday) / sizeof(WCHAR));
/* 0:Monday, ..., 6:Sunday. */
/* We need 1 for Monday, 0 for Sunday. */
first_weekday = (week_1stday + 1) % 7;
printf("ret = %d\n", ret);
printf("sizeof(ret) = %Iu\n", sizeof(ret));
printf("sizeof(week_1stday) = %Iu\n", sizeof(week_1stday));
printf("sizeof(WCHAR) = %Iu\n", sizeof(WCHAR));
printf("week_1stday = %lu\n", week_1stday);
printf("first_weekday = %d\n", first_weekday);
return 0;
}
See also:
GetLocaleInfoEx function
LOCALE_IFIRSTDAYOFWEEK
来源:https://stackoverflow.com/questions/11602425/langinfo-h-in-visual-studio-2010