问题
How to determine which day is the first in week in current locale in C. In Russia monday is the first day, but my mac shows localized calendar with wrong first day. So i wonder if i can determine which day is the first in current locale. Thanks.
anatoly@mb:/Users/anatoly$ cal
Июля 2012
вс пн вт ср чт пт сб
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
回答1:
I was wrong in my first post, and ICU provides a C API.
So, if dependency on that library is acceptable for you, you can get the first of the week portably using the following snippet:
#include <stdio.h>
/* for calendar functions */
#include <unicode/ucal.h>
/* for u_cleanup() */
#include <unicode/uclean.h>
/* for uloc_getDefault() */
#include <unicode/uloc.h>
int main()
{
/* it *has* to be pre-set */
UErrorCode err = U_ZERO_ERROR;
UCalendar* cal = ucal_open(
0, -1, /* default timezone */
uloc_getDefault(), /* default (current) locale */
UCAL_DEFAULT, /* default calendar type */
&err);
if (!cal)
{
fprintf(stderr, "ICU error: %s\n", u_errorName(err));
u_cleanup();
return 1;
}
/* 1 for sunday, 2 for monday, etc. */
printf("%d\n", ucal_getAttribute(cal, UCAL_FIRST_DAY_OF_WEEK));
ucal_close(cal);
u_cleanup();
return 0;
}
Then you link the program with icu-i18n
pkg-config library.
Ah, and they have quite an extensive example printing a calendar, if you might be interested.
回答2:
With glibc, you can do:
#define _GNU_SOURCE
#include <langinfo.h>
char get_first_weekday()
{
return *nl_langinfo(_NL_TIME_FIRST_WEEKDAY);
}
Remember to call setlocale()
first. Example:
#include <stdio.h>
#include <locale.h>
int main()
{
setlocale(LC_ALL, "");
printf("%d\n", get_first_weekday());
return 0;
}
This returns 2
on my system (which means monday == DAY_2
).
Just a note: I don't think it's public API of glibc. However, this is how locale
tool bundled with it gets first weekday. cal
uses similar method as well.
Depending on a particular use, you may be interested in _NL_TIME_FIRST_WORKDAY
as well.
回答3:
Apparently on Macs you can use:
#include <ApplicationServices/ApplicationServices.h>
[..]
CFCalendarRef cal = CFCalendarCopyCurrent();
long f_day = CFCalendarGetFirstWeekday(cal); // 1=Sunday, ..
CFRelease(cal);
Source: [1].
来源:https://stackoverflow.com/questions/11409902/how-to-determine-which-day-is-the-first-in-week-in-current-locale-in-c