如何确定哪一天是C中当前语言环境中的第一周

如何确定哪一天是C中当前语言环境中的第一天。在俄罗斯星期一是第一天,但​​我的mac显示错误的第一天本地化日历。 所以我想知道我是否可以确定哪一天是当前语言环境中的第一天。 谢谢。

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 

我的第一篇文章错了, ICU提供了一个C API。

因此,如果您可以接受对该库的依赖,则可以使用以下代码段以便携式获取本周的第一个:

 #include  /* for calendar functions */ #include  /* for u_cleanup() */ #include  /* for uloc_getDefault() */ #include  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; } 

然后将程序与icu-i18n pkg-config库链接。

啊,如果您有兴趣,他们有相当广泛的例子来打印日历 。

使用glibc,您可以:

 #define _GNU_SOURCE #include  char get_first_weekday() { return *nl_langinfo(_NL_TIME_FIRST_WEEKDAY); } 

记得先调用setlocale() 。 例:

 #include  #include  int main() { setlocale(LC_ALL, ""); printf("%d\n", get_first_weekday()); return 0; } 

这在我的系统上返回2 (这意味着星期一== DAY_2 )。

请注意:我不认为它是glibc的公共API。 但是,这就是与其捆绑的locale工具在工作日的第一天。 cal使用类似的方法。

根据具体用途,您可能也对_NL_TIME_FIRST_WORKDAY感兴趣。