Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
818 views
in Technique[技术] by (71.8m points)

date - How to determine which day is the first in week in current locale in C

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
", 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...