tags:

views:

61

answers:

1

I need a tiny standalone library in C on linux platform that will return "Friday" when provided with (2009, 11, 13) for example. I would like it to be locale aware, meaning, returning day and month names in language set by the user.

Any suggestions?

+1  A: 

You can tie together mktime and strftime to do that:

char daybuf[20];
struct tm time_str;

time_str.tm_year = YEAR - 1900;
time_str.tm_mon = MONTH - 1;
time_str.tm_mday = DAY;
time_str.tm_hour = 0;
time_str.tm_min = 0;
time_str.tm_sec = 1;
time_str.tm_isdst = -1;
if (mktime(&time_str) != -1)
    strftime(daybuf, sizeof(daybuf), "%A", &time_str);
R Samuel Klatchko
That might work but it will be influenced by the application's current locale and if the locale-specific formatting of the day name is too long, it will overflow the daybuf[].
dajobe
@dajobe - the question asked for something locale specific. Also, it won't overflow daybuf as the second parameter to strftime sets the maximum number of characters that will be written.
R Samuel Klatchko
It won't overflow the buffer but it will leave the buffer untouched in certain cases. An example failure testcase for this code is the locale ru_RU.UTF-8 and YEAR=2009,MONTH=11,DAY=30. This gives "Понедельник" ("Monday") as the result, which does not fit into 20 bytes (its storage is 23 bytes if we include the terminating zero byte).
Andrew Y