tags:

views:

52

answers:

1

What are the various ways to get the name of the month corresponding to an integer value [say 0 for Jan,1 for February ,...,11 for December] from inbuilt C/C++ library, I am familiar to strftime.Any other means to do the same ?

+3  A: 
#include <langinfo.h>
#include <locale.h>
#include <stdio.h>
int main() {
    const nl_item nl_abmons[12] = {ABMON_1, ABMON_2, ABMON_3, ABMON_4,
                                   ABMON_5, ABMON_6, ABMON_7, ABMON_8,
                                   ABMON_9, ABMON_10, ABMON_11, ABMON_12};
    const nl_item nl_months[12] = {MON_1, MON_2, MON_3, MON_4, MON_5, MON_6,
                                   MON_7, MON_8, MON_9, MON_10, MON_11, MON_12};
    int i;
    setlocale(LC_ALL, "");
    for (i = 0; i < 12; i++) {
        printf("%d\t%s\t%s\n",
                i+1, nl_langinfo(nl_abmons[i]), nl_langinfo(nl_months[i]));
    }
    return 0;
}
$ cc months.c
$ LANG=ja_JP.utf8 ./a.out
1        1月    1月
2        2月    2月
3        3月    3月
4        4月    4月
5        5月    5月
6        6月    6月
7        7月    7月
8        8月    8月
9        9月    9月
10      10月    10月
11      11月    11月
12      12月    12月
$ LANG=ru_RU.utf8 ./a.out
1       Янв     Январь
2       Фев     Февраль
3       Мар     Март
4       Апр     Апрель
5       Май     Май
6       Июн     Июнь
7       Июл     Июль
8       Авг     Август
9       Сен     Сентябрь
10      Окт     Октябрь
11      Ноя     Ноябрь
12      Дек     Декабрь
$ LANG=de_DE.utf8 ./a.out
1       Jan     Januar
2       Feb     Februar
3       Mär     März
4       Apr     April
5       Mai     Mai
6       Jun     Juni
7       Jul     Juli
8       Aug     August
9       Sep     September
10      Okt     Oktober
11      Nov     November
12      Dez     Dezember

It does so happen in all implementations I know of that MON_1..MON_12 are sequential, so this could be written for (i = 0; i < 12; i++) printf("%d %s\n", i+1, nl_langinfo(MON_1+i)); with the same practical effect, but I don't see that guaranteed in documentation anywhere.

ephemient
That's nice !! But could we convert it such that it correspond to integer values ? Ofcourse I don't want to build up a big hash map for MON_{1-12} :)
Bingo